解决:avformat_write_header(pFormatCtx_Out, NULL);打开失败的问题

打开成功  :avio_open(&pFormatCtx_Out->pb, outFileName, AVIO_FLAG_WRITE); 

但是,avformat_write_header(pFormatCtx_Out, NULL); 写头,失败。返回 -22

[mp4 @ 02ae5a00] Using AVStream.codec.time_base as a timebase hint to the muxer is 
deprecated. Set AVStream.time_base instead.
[mp4 @ 02ae5a00] Could not find tag for codec none in stream #0, codec not currently 
supported in container

解决第一个bug:   stream->codec->time_base ,这个形式,已经不行了。直接 stream->time_base,赋值即可。

pVideoStream->time_base.den = pCodecCtx_out->time_base.den;
pVideoStream->time_base.num = pCodecCtx_out->time_base.num;

解决第二个bug: 

不能在container(如MP4、TS等封装格式)的相应流中找到编码器信息,找不到的原因可能会有很多,主要有参数设置不对,音视频流的id错误或是ffmpeg库中不支持此编码方式等。跟踪调试数据发现流中的编码器信息没有具体值,在avcodec_open2 成功之后,利用avcodec_parameters_from_context将编码器的参数赋值给AVStream中的codecpar字段,同时要把编码器复制过去。问题解决,运行成功!

avcodec_parameters_from_context(pVideoStream->codecpar, pCodecCtx_out);
pVideoStream->codec->codec = pCodecCtx_out->codec;

原题重现:

  1. 刚开始,只写了红框内容,且黄色部分是:stream->codec->time_base;
  2. 后来改成了现在黄框内的部分,解决了第一个问题。
  3. 然后添加了绿色部分的第一行,其他都注释带,依然编译不过去,因为找不到stream->codec->codec
  4. 再然后,加上绿框的第二行代码,顺利打开。
  5. 最后好奇心重的我,把下面的部分的注释都去掉了,发现也能编译过去。 

发布了417 篇原创文章 · 获赞 156 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_34732729/article/details/104681816