FFMPEG avformat_write_header changing my stream time_base

192 篇文章 ¥19.90 ¥99.00
本文探讨了在使用FFMPEG时遇到的`avformat_write_header`函数如何改变流的时间基准,深入分析了这一过程及其对编码的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am muxing video using ffmpeg 2.1.3 with libavcodec 55.39.101 (forced to that version because is the one available in google-chrome PNACL port project). all my frames seems to have bad the time. they try to be rendered all at once at the beggining of the video when playing it.

I am setting the stream time base to 1/25, but just after calling avformat_write_header, it has the value of -18082736/1. in each frame, when I print the stream time_base it says 1/12800, while the time_base of codec is always ok (1/25).

console log before and after av_format_write_header:

before avformat_write_header stream time_base: 1/25
after avformat_write_header ret 0 stream time_base: -18082736/1

the code (abreviated to keep the post short, all calls in the original version had error checking):

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

If I run ffmpeg -i on the final video, I got this (why duration is zero?):

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
share improve this question
 

1 Answer

You need to manipulate the pts of the packet after encoding and before writing to the file... Its not unusual to have the time_base of the stream changed by ffmpeg. Check line 869 of the ffmpeg.c source code at https://www.ffmpeg.org/doxygen/trunk/ffmpeg_8c-source.html




http://stackoverflow.com/questions/29850246/ffmpeg-avformat-write-header-changing-my-stream-time-base
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值