FFMPEG avformat_write_header更改我的流time_base

ces*_*hon 3 video ffmpeg

我正在将ffmpeg 2.1.3与libavcodec 55.39.101混合使用(由于使用了google-chrome PNACL端口项目中的可用版本,因此强制使用该版本)。我所有的镜架时间似乎都很糟糕。他们尝试在播放视频时立即将其全部渲染。

我将流时基设置为1/25,但是在调用avformat_write_header之后,它的值为-18082736/1。在每帧中,当我打印流time_base时,它表示1/12800,而编解码器的time_base始终正常(1/25)。

av_format_write_header之前和之后的控制台日志:

before avformat_write_header stream time_base: 1/25
after avformat_write_header ret 0 stream time_base: -18082736/1
Run Code Online (Sandbox Code Playgroud)

代码(缩写为简短起见,原始版本中的所有调用均进行了错误检查):

AVCodecContext *codecContext;
AVCodec * codec = avcodec_find_encoder(codec_id);  
myOutputStream->stream = avformat_new_stream(outputFormatContext, *codec);
myOutputStream->stream->id = outputFormatContext->nb_streams-1;
codecContext = myOutputStream->stream->codec;
codecContext->codec_id = codec_id;
codecContext->bit_rate = 400000;
codecContext->width    = width;
codecContext->height   = height;
myOutputStream->stream->time_base = (AVRational){ 1, 25 };
codecContext->time_base       = myOutputStream->stream->time_base;
codecContext->gop_size      = 12; 
codecContext->pix_fmt       = AV_PIX_FMT_YUV420P;
AVDictionary *opt = NULL;
av_dict_copy(&opt, opt_arg, 0);
ret = avcodec_open2(codecContext, codec, &opt);
av_dict_free(&opt);
myOutputStream->frame = alloc_picture(codecContext->pix_fmt, codecContext->width, codecContext->height);
  myOutputStream->tmp_frame = alloc_picture(AV_PIX_FMT_YUV420P, codecContext->width, codecContext->height);

//before: printing g_outputContext->stream time_base here
ret = avformat_write_header(g_outputContext, &opt);
//after: printing g_outputContext->stream time_base here
Run Code Online (Sandbox Code Playgroud)

如果在最后一个视频上运行ffmpeg -i,我会得到这个信息(为什么持续时间为零?):

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test4.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    encoder         : Lavf55.19.104
  Duration: 00:00:00.05, start: 0.000000, bitrate: 99549 kb/s
    Stream #0:0(und): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 800x600 [SAR 1:1 DAR 4:3], 463106 kb/s, 12800 fps, 12800 tbr, 12800 tbn, 25 tbc (default)
    Metadata:
      handler_name    : VideoHandler
Run Code Online (Sandbox Code Playgroud)

Raf*_*cio 5

在编码后和写入文件之前,您需要操作数据包的pts。ffmpeg更改流的time_base并不罕见。在https://www.ffmpeg.org/doxygen/trunk/ffmpeg_8c-source.html上检查ffmpeg.c源代码的第869行