h264 stream muxing: duration of resulting file is shorter than recording time

152 Views Asked by At

I am muxing H264 stream from v4l device to avi container using the approach outlined in the following stackoverflow question

The resulting files are playable, but for (lets say) 30 second recording the resulting file is only 10 seconds length. In other words, once I press 'Start recording' button until I press 'Stop' recording it is 30 seconds that have elapsed but file is only 10 seconds length (as shown in Windows Media player). Muxing starts immediately once I pressed the 'Start recording' button.

Any ideas on how could I approach this problem?

1

There are 1 best solutions below

0
user466540 On

The problem was with the fps parameter:

AVStream *pst = avformat_new_stream(fc, 0);
  vi = pst->index;

  AVCodecContext *pcc = pst->codec;
  _M;
  avcodec_get_context_defaults3(pcc, AVMEDIA_TYPE_VIDEO);
  pcc->codec_type = AVMEDIA_TYPE_VIDEO;

  pcc->codec_id = codec_id;
  pcc->bit_rate = br;
  pcc->width = w;
  pcc->height = h;
  pcc->time_base.num = 1;
  int fps = 30; // problem here
  pcc->time_base.den = fps;

As it turned out, H264 stream produces frames with 13 fps. Once I made fps = 13, file duration become aligned with the expected time.