video - FFMPEG avformat_write_header 改变我的流 time_base

标签 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

代码(为使帖子简短而进行了缩写,原始版本中的所有调用都进行了错误检查):

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

如果我在最终视频上运行 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

最佳答案

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

关于video - FFMPEG avformat_write_header 改变我的流 time_base,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29850246/

相关文章:

javascript - Flash 支持哪些视频格式?

ios - 如何在 iOS 中编码流式视频?

c++ - OpenCV 无法连接到视频流 - 缺少某些编解码器?

ffmpeg - 如何使用 FFMPEG 创建具有恒定持续时间的 m3u8 播放列表和 mpeg-ts block ?

android - Android 上的 HTML5 <video> 元素无法播放

python - 如何定位视频子集的时间戳

silverlight - Silverlight 视频播放器可以播放 Azure blob 中的 wmv 吗?

java - 如何从 FFmpegStream 获取视频大小

python - OpenCV + Python 使用 UDP 协议(protocol)通过网络传输 H264 视频

ffmpeg - 使用 ffmpeg 设置视频缩略图的问题