avio_open成功avformat_write_header失败

1)ffmpeg 写avi时候,avio_open成功,但是写avi头时调用avformat_write_header失败

 

在解码器初始化时,先avio_open创建文件后,然后马上调用avformat_write_header函数,会出现avformat_write_header失败,返回值-22;但是如果将解码器封装成一个类,提供一个封装好的avformat_write_header函数就没有这个问题了。

原因:avformat_write_header的参数信息不正确,增加帧率控制就ok了

 DecodecContext->time_base.num=1;
 DecodecContext->time_base.den=25;

 

 //释放avDecodeFmtContext 失败
 2)avformat_free_context(avDecodeFmtContext); 

 

 if (VideoDecodeSt->codec!=NULL)
 {
  avcodec_close(VideoDecodeSt->codec);
  av_free(VideoDecodeSt->codec);
  VideoDecodeSt->codec=NULL;
 }


 if (avDecodeFmtContext!=NULL)
 {
  avformat_free_context(avDecodeFmtContext); 
  avDecodeFmtContext=NULL;
 }

 

这样是失败的,但是改为这样就ok

 if (VideoDecodeSt->codec!=NULL)
 {
  avcodec_close(VideoDecodeSt->codec);
  //av_free(VideoDecodeSt->codec);
  //VideoDecodeSt->codec=NULL;

 }


 if (avDecodeFmtContext!=NULL)
 {
  avformat_free_context(avDecodeFmtContext); 
  avDecodeFmtContext=NULL;
 }


 但是这样后,造成释放avformat_free_context(avDecodeFmtContext); 后,在重新初始化avformat_alloc_output_context2 出错,该函数返回值-22

 

原因是:

 int ret=avformat_alloc_output_context2(&avDecodeFmtContext, NULL, NULL, fileName.c_str());

 

最后的fileName.c_str());没有传递正确;

 

 

ffmpeg 转码使用的api调用顺序:

 

1) av_register_all();

2) avformat_alloc_output_context2(&oc, NULL, NULL, filename);

3)avcodec_find_encoder(codec_id);

4)avformat_new_stream(oc, *codec);

5)可选

  //防止编码延迟修改参数
  av_opt_set(c->priv_data, "preset", "superfast", 0); 
  av_opt_set(c->priv_data, "tune", "zerolatency", 0);


6) av_dump_format(oc, 0, filename, 1);

 

7) if (!(fmt->flags & AVFMT_NOFILE)) {
  ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
  if (ret < 0) {
   //fprintf(stderr, "Could not open '%s': %s\n", filename,av_err2str(ret));
   return 1;
  }
 }

 

8) ret = avformat_write_header(oc, NULL);

 

9) ret = av_interleaved_write_frame(oc, &pkt);

 

10) av_write_trailer(oc);

 

11)avcodec_close(st->codec);

 

12)if (!(fmt->flags & AVFMT_NOFILE))
 {
  /* Close the output file. */
  avio_close(oc->pb);
 }

 

13) avformat_free_context(oc);

 

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
`avformat_write_header()` 函数是用于写入音视频文件头信息的,如果它返回 "Invalid argument" 错误,通常是因为传入的参数有误或不完整。 一些可能的原因和解决办法如下: 1. 检查 AVFormatContext 结构体是否正确初始化。在调用 `avformat_alloc_output_context2()` 创建输出格式上下文后,需要设置 `AVFormatContext` 的一些参数,如 `AVCodec`, `AVStream`, `AVOutputFormat` 等。如果这些参数没有正确设置,就会导致 `avformat_write_header()` 函数报错。你可以通过调用 `av_dump_format()` 打印 `AVFormatContext` 结构体的详细信息,以检查它是否正确初始化。 2. 检查 AVStream 结构体是否正确设置。在输出音视频流之前,需要设置每个 `AVStream` 的一些参数,如编解码器、帧率、分辨率等。如果这些参数没有正确设置,就会导致 `avformat_write_header()` 函数报错。你可以通过调用 `avcodec_parameters_from_context()` 或 `avcodec_parameters_copy()` 将编解码器参数从 `AVCodecContext` 复制到 `AVStream` 中。 3. 检查输出文件是否已经打开。在调用 `avformat_write_header()` 之前,需要通过调用 `avio_open()` 或 `fopen()` 等函数打开输出文件。如果输出文件没有正确打开,就会导致 `avformat_write_header()` 函数报错。 4. 检查输出文件是否已经写入。在调用 `avformat_write_header()` 之前,需要保证输出文件还没有写入任何数据。如果输出文件已经写入了数据,就会导致 `avformat_write_header()` 函数报错。你可以通过调用 `avio_seek()` 将文件指针移动到文件开头,以确保文件没有写入任何数据。 希望这些解决方案能够帮助你解决问题。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值