Nyaruko
Nyaruko

Reputation: 4499

How can I really pick up the correct format when storing incoming stream?

I want to re-mux a incoming h264 stream. But how could I pick the correct AVOutputFormat for the AVFormatContext?

Currently I used:

AVOutputFormat* fmt = av_guess_format(NULL, "xxx.avi", NULL);

// Open the context
//---------------------------------------------------------------------
outFormatCtx = ffmpeg::avformat_alloc_context();

//Set the output format
//----------------------------------------------------------------------
outFormatCtx->oformat = fmt;

And everything works fine. However, if I change the first line to: av_guess_format("h264",NULL, NULL); the recorded stream cannot be played because of bad header/tailer.

Is there a more smart way of picking the correct AVOutputFormat to be used?

Upvotes: 0

Views: 421

Answers (1)

Ronald S. Bultje
Ronald S. Bultje

Reputation: 11184

What are you using to play it back, and what incoming stream are you remuxing? This is important, because you're trying to write a raw annexb H264 stream.

  • not all players support this. E.g. VLC supports it, but Windows Media Player won't.
  • it requires the H264 packets to actually be in annexb format. If you're remuxing e.g. a mp4 file, the H264 packets will be in mp4 format, and you' need a bitstream filter to convert them (-bsf h264_mp4toannexb).

So, if you're not using a player of which you explicitly know that it supports raw annexb H264, use a different player. If you're remuxing an input of which you're not sure if it's in MP4 or annexb format, try -bsf h264_mp4toannexb.

There is also the possibility that you didn't actually want to write a raw annexb H264 stream. If all the above is cryptic to you and you're thinking I'm talking nonsense, this is probably the case. In that case, don't use "h264" as format (first argument) in your call to av_guess_format() (since it's primarily a video codec, not a container format), but use "mp4" or "avi" or "matroska" (the container format) instead.

Upvotes: 3

Related Questions