FFMPEG avformat_write_header 改变我的流 time_base
FFMPEG avformat_write_header changing my stream time_base
我正在使用 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
代码(为了保持 post 简短,原始版本中的所有调用都有错误检查):
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...由 ffmpeg 更改流的 time_base 并不罕见。在 https://www.ffmpeg.org/doxygen/trunk/ffmpeg_8c-source.html
查看 ffmpeg.c 源代码的第 869 行
要向@RafaelLucio 添加更多详细信息,avformat_write_header
通过设计修改流时基,并且必须在将帧点发送到编解码器上下文之前正确计算帧点,重新调整所需值到实际的流时基。一个简单的解决方案是在框架追加函数之外保留一个 pts 计数器,并在函数体中计算下一个 pts。
int64_t pts = 0;
AVCodecContext *codecCtx;
AVStream *stream;
void appendFrame(AVFrame *frame)
{
[...]
frame->pts = pts;
// Compute next pts
pts += av_rescale_q(1, codecCtx->time_base, stream->time_base);
[...]
}
我正在使用 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
代码(为了保持 post 简短,原始版本中的所有调用都有错误检查):
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...由 ffmpeg 更改流的 time_base 并不罕见。在 https://www.ffmpeg.org/doxygen/trunk/ffmpeg_8c-source.html
查看 ffmpeg.c 源代码的第 869 行要向@RafaelLucio 添加更多详细信息avformat_write_header
通过设计修改流时基,并且必须在将帧点发送到编解码器上下文之前正确计算帧点,重新调整所需值到实际的流时基。一个简单的解决方案是在框架追加函数之外保留一个 pts 计数器,并在函数体中计算下一个 pts。
int64_t pts = 0;
AVCodecContext *codecCtx;
AVStream *stream;
void appendFrame(AVFrame *frame)
{
[...]
frame->pts = pts;
// Compute next pts
pts += av_rescale_q(1, codecCtx->time_base, stream->time_base);
[...]
}