ZBLOG

ffmpeg播放器解码后打印pts和dts,如果写代码

要使用 FFmpeg 解码视频并打印 PTS(Presentation Timestamp)和 DTS(Decoding Timestamp),你可以参考以下的 C 代码示例。这个示例将打开一个视频文件,解码每一帧,并打印出相应的 PTS 和 DTS 值。

首先,请确保你的环境中已安装 FFmpeg 库。

以下是一个简单的示例代码:

#include <stdio.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>

int main(int argc, char *argv[]) {
    if (argc < 2) {
        printf("Usage: %s <input file>\n", argv[0]);
        return -1;
    }

    const char *input_filename = argv[1];

    // 注册所有的文件格式和编解码器
    av_register_all();

    AVFormatContext *fmt_ctx = NULL;

    // 打开输入文件
    if (avformat_open_input(&fmt_ctx, input_filename, NULL, NULL) < 0) {
        fprintf(stderr, "Could not open source file %s\n", input_filename);
        return -1;
    }

    // 检查流信息
    if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
        fprintf(stderr, "Could not find stream information\n");
        avformat_close_input(&fmt_ctx);
        return -1;
    }

    // 找到第一个视频流
    int video_stream_index = -1;
    
    for (unsigned int i = 0; i < fmt_ctx->nb_streams; i++) {
        if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
            video_stream_index = i;
            break;
        }
    }

    if (video_stream_index == -1) {
        fprintf(stderr, "Could not find a video stream\n");
        avformat_close_input(&fmt_ctx);
        return -1;
    }

    AVCodecParameters *codecpar = fmt_ctx->streams[video_stream_index]->codecpar;

    // 查找解码器
    AVCodec *codec = avcodec_find_decoder(codecpar->codec_id);
    
    if (!codec) {
        fprintf(stderr, "Unsupported codec!\n");
        avformat_close_input(&fmt_ctx);
        return -1;
    }

    // 创建解码上下文
    AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
    
    if (!codec_ctx) {
        fprintf(stderr, "Could not allocate codec context\n");
        avformat_close_input(&fmt_ctx);
        return -1;
   }

   // 从流参数复制到上下文
   if (avcodec_parameters_to_context(codec_ctx, codecpar) < 0) {
       fprintf(stderr, "Failed to copy codec parameters to decoder context\n");
       avcodec_free_context(&codec_ctx);
       avformat_close_input(&fmt_ctx);
       return -1;
   }
   
   // 打开解码器
   if (avcodec_open2(codec_ctx, codec, NULL) < 0) {
       fprintf(stderr, "Failed to open codec\n");
       avcodec_free_context(&codec_ctx);
       avformat_close_input(&fmt_ctx);
       return -1;
   }

   AVPacket pkt;
   AVFrame *frame = av_frame_alloc();

   while (av_read_frame(fmt_ctx, &pkt) >= 0) {
       if (pkt.stream_index == video_stream_index) {
           // 解码视频帧
           int response = avcodec_send_packet(codec_ctx, &pkt);

           if (response >= 0) {
               while (response >= 0) {
                   response = avcodec_receive_frame(codec_ctx, frame);

                   if (response == AVERROR(EAGAIN) || response == AVERROR_EOF)
                       break;

                   if (response < 0) {
                       fprintf(stderr, "Error while receiving frame from decoder\n");
                       break;
                   }

                   // 打印PTS和DTS
                   printf("Frame %d: PTS=%lld DTS=%lld\n", 
                           frame->coded_picture_number,
                           frame->pts,
                           frame->pkt_dts);

               }
           }
       }
       
       av_packet_unref(&pkt); // 清理包结构体以准备下一个包处理
   }

   // 清理资源
   av_frame_free(&frame);
   avcodec_free_context(&codec_ctx);
   avformat_close_input(&fmt_ctx);

   return 0;
}

编译和运行

确保你已安装了 FFmpeg 的开发库,然后用如下命令编译代码:

gcc your_code.c -o ffmpeg_player -lavformat -lavcodecvideocontent \
- lavutil 

运行程序时提供你要解码的视频文件作为参数:

./ffmpeg_player input_video.mp4

注意事项

  • 确保正确链接 FFmpeg 的库。
  • 在使用时,请根据需要处理错误并进行适当的清理。
  • 如果需要处理音频或者其他功能,可以相应地扩展这个基础框架。


    内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://sdn.0voice.com/?id=5631

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?