ffmpeg函数封装

static int open_input_file_and_get_contexts(const char* input_file_name,
           AVFormatContext** input_format_context,
           AVCodecContext** input_video_codec_context,
           AVCodecContext** input_audio_codec_context,
           int* video_index,
           int* audio_index)
{
 int idx_v = -1, idx_a = -1;
 AVCodecContext* codec_ctx_v = NULL;
 AVCodecContext* codec_ctx_a = NULL;
 AVCodec* codec_v = NULL;
 AVCodec* codec_a = NULL;

 AVDictionary* options = NULL;
 av_dict_set(&options, "rtsp_transport", "tcp", 0);

 avformat_open_input(input_format_context, input_file_name, NULL, &options);
 avformat_find_stream_info(*input_format_context, NULL);

 for (int i = 0; i < (*input_format_context)->nb_streams; ++i)
 {
  if (AVMEDIA_TYPE_AUDIO == (*input_format_context)->streams[i]->codec->codec_type)
  {
   idx_a = i;
  }
  else if (AVMEDIA_TYPE_VIDEO == (*input_format_context)->streams[i]->codec->codec_type)
  {
   idx_v = i;
  }
  else
  {
   ;
  }
 }

 if (idx_v >= 0)
 { 
  //codec_v = avcodec_find_decoder((*input_format_context)->streams[idx_v]->codecpar->codec_id);
  //codec_ctx_v = avcodec_alloc_context3(codec_v);
  //avcodec_parameters_to_context(codec_ctx_v, (*input_format_context)->streams[idx_v]->codecpar);

  codec_ctx_v = (*input_format_context)->streams[idx_v]->codec;
  codec_v = avcodec_find_decoder((*input_format_context)->streams[idx_v]->codec->codec_id);

  avcodec_open2(codec_ctx_v, codec_v, NULL);

  *input_video_codec_context = codec_ctx_v;
  *video_index = idx_v;
 }
  

 if (idx_a >= 0)
 {
  //codec_a = avcodec_find_decoder((*input_format_context)->streams[idx_a]->codecpar->codec_id);
  //codec_ctx_a = avcodec_alloc_context3(codec_a);
  //avcodec_parameters_to_context(codec_ctx_a, (*input_format_context)->streams[idx_a]->codecpar);

  codec_ctx_a = (*input_format_context)->streams[idx_a]->codec;
  codec_a = avcodec_find_decoder((*input_format_context)->streams[idx_a]->codec->codec_id);

  avcodec_open2(codec_ctx_a, codec_a, NULL);

  *input_audio_codec_context = codec_ctx_a;
  *audio_index = idx_a;
 }

 return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FFmpeg是一个开源的跨平台多媒体处理库,能够处理各种格式的音频和视频文件。FFmpeg在实际的应用中,通常使用它提供的各种API对音视频进行编解码、过滤、转换等处理。下面是FFmpeg函数调用的基本流程: 1. 初始化FFmpeg库:使用av_register_all函数进行初始化,注册FFmpeg支持的所有编码器、解码器、过滤器等组件。 2. 打开输入文件或设备:使用avformat_open_input函数打开输入文件或设备,并读取文件或设备的基本信息和流信息。 3. 查找音视频流:使用avformat_find_stream_info函数查找音视频流,并获取流的基本信息。 4. 打开编解码器:使用avcodec_find_decoder函数查找音视频流的编解码器,并使用avcodec_open2函数打开编解码器。 5. 解码音视频数据:使用avcodec_send_packet函数将压缩数据送入解码器,使用avcodec_receive_frame函数接收解码后的数据。 6. 过滤音视频数据:使用avfilter_graph_create_filter函数创建过滤器,并使用av_buffersrc_add_frame函数将解码后的数据送入过滤器,使用av_buffersink_get_frame函数从过滤器中获取过滤后的数据。 7. 编码音视频数据:使用avcodec_send_frame函数将原始数据送入编码器,使用avcodec_receive_packet函数接收编码后的数据。 8. 封装音视频数据:使用avformat_write_header函数写入输出文件或设备的头部信息,使用av_interleaved_write_frame函数将编码后的数据写入输出文件或设备。 9. 释放资源:使用avformat_close_input、avcodec_close、avfilter_graph_free、avformat_free_context等函数释放资源。 以上是FFmpeg函数调用的基本流程,具体实现时还需根据具体需求进行函数调用和参数设置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值