Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FFMPEG avformat_write_header changing my stream time_base

Tags:

video

ffmpeg

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
like image 425
cesarpachon Avatar asked Jan 09 '23 12:01

cesarpachon


1 Answers

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

like image 192
Rafael Fontes Avatar answered Mar 23 '23 16:03

Rafael Fontes