ZBLOG

C++ MP4ffmpeg解码

在 C++ 中使用 FFmpeg 解码 MP4 文件的基本步骤如下。首先,确保你已经安装了 FFmpeg 库并正确配置了开发环境。

下面是一个简单的示例代码,演示如何使用 FFmpeg 解码 MP4 视频:

1. 引入必要的头文件

extern "C" {
    #include <libavformat/avformat.h>
    #include <libavcodec/avcodec.h>
    #include <libswscale/swscale.h>
}

2. 初始化和解码流程

int main(int argc, char *argv[]) {
    // 检查命令行参数
    if (argc < 2) {
        fprintf(stderr, "Usage: %s <input_file>\n", argv[0]);
        return -1;
    }

    const char *input_filename = argv[1];
    
    // 注册所有组件
    av_register_all();

    AVFormatContext *pFormatCtx = nullptr;

    // 打开输入文件并获取格式上下文
    if (avformat_open_input(&pFormatCtx, input_filename, nullptr, nullptr) != 0) {
        fprintf(stderr, "Could not open source file %s\n", input_filename);
        return -1; 
    }
    
    // 获取流信息
    if (avformat_find_stream_info(pFormatCtx, nullptr) < 0) {
        fprintf(stderr, "Could not find stream information\n");
        avformat_close_input(&pFormatCtx);
        return -1;
    }

    // 查找视频流索引
    int video_stream_index = -1;
    for (unsigned int i = 0; i < pFormatCtx->nb_streams; i++) {
        if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
            video_stream_index = i;
            break;
        }
    }

    if (video_stream_index == -1) {
        fprintf(stderr, "Didn't find a video stream\n");
        avformat_close_input(&pFormatCtx);
        return -1;
    }

    AVCodecParameters *pCodecParameters = pFormatCtx->streams[video_stream_index]->codecpar;

    // 找到解码器并打开它
    AVCodec *pCodec = avcodec_find_decoder(pCodecParameters->codec_id);
    
   if (!pCodec) {
       fprintf(stderr, "Unsupported codec!\n");
       avformat_close_input(&pFormatCtx);
       return -1; 
   }
   
   AVCodecContext *pCodecCtx = avcodec_alloc_context3(pCodec);

   if (!pCodecCtx) {
       fprintf(stderr, "Could not allocate codec context\n");
       avformat_close_input(&pFormatCtx);
       return -1;
   }

   // 从编码参数复制设置到编码上下文中
   if (avcodec_parameters_to_context(pCodecCtx, pCodecParameters) < 0) {
       fprintf(stderr, "Failed to copy codec parameters to decoder context\n");
       avcodec_free_context(&pCodecCtx);
       avformat_close_input(&pFormatCtx);
       return -1;
   }

   // 打开解码器
   if (avcodec_open2(pCodecCtx, pCodec, nullptr) < 0) {
      fprintf(stderr, "Could not open codec\n");
      avcodec_free_context(&pCodecCtx);
      avformat_close_input(&pFormatCtx);
      return -1; 
   }

   AVFrame *frame = av_frame_alloc();
   AVPacket packet;

   while (av_read_frame(pFormatCtx, &packet) >= 0) {
       if (packet.stream_index == video_stream_index) {
           // 解码一帧数据
           int response = avcodec_send_packet(pCodecCtx, &packet);

           while (response >= 0) {               
               response = avcodec_receive_frame(pCodecCtx, frame);               
               if (response == AVERROR(EAGAIN) || response == AVERROR_EOF)
                   break;               
               else if (response < 0){
                   fprintf(stderr,"Error while receiving a frame from the decoder\n");                 
                   goto end;
               }
              
               printf("Decoded frame: %d\n", pCodecCtx->frame_number);           
           }
       }
      
       av_packet_unref(&packet); //释放当前包资源       
   }

end:
   av_frame_free(&frame);     
   avcodec_free_context(&pCodecCtx);  
   avformat_close_input(&pFormatCtx);

   return 0;
}

注意事项

  • 确保你的系统上安装了 FFmpeg,并且在编译时链接了相应的库,例如 -lavformat -lavcodec -lavutil 等。
  • 上面的示例代码未处理音频流。如果需要音频解码,可以参照类似的方法处理音频流。
  • 错误处理逻辑可以根据实际需求进行扩展。
  • 请参考 FFmpeg 的官方文档,以了解更多关于 API 的详细信息。

希望这个例子能帮助你开始使用 C++ 和 FFmpeg 解码 MP4 视频!如果你有任何问题,请随时问我。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?