Skip to content
systemcoding.dev
Media

Wrap a raw .h264 elementary stream into MP4

Wrap a raw Annex B .h264 file into MP4 without re-encoding using ffmpeg 7.1, and give it the frame rate the raw stream does not carry.

1 min read
Summarize in AI

A raw .h264 file is a bare stream of NAL units with start codes and no timestamps. FFmpeg 7.1 can put it in an MP4 container with a stream copy, but you have to say how fast the frames go, because nothing in the file does.

Shell
ffmpeg -f h264 -framerate 30 -i in.h264 -c copy out.mp4
Text
Input #0, h264, from 'in.h264':
  Duration: N/A, bitrate: N/A
  Stream #0:0: Video: h264 (High), yuv420p(progressive), 1280x720, 30 fps, 30 tbr, 1200k tbn
Output #0, mp4, to 'out.mp4':
  Stream #0:0: Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1280x720, 30 fps, 30 tbr, 15360 tbn
Stream mapping:
  Stream #0:0 -> #0:0 (copy)

-framerate goes before -i because it is an option of the raw demuxer. Leave it out and FFmpeg assumes 25 fps, so a 30 fps recording plays 17 percent slow with no error. The MP4 muxer reads the SPS and PPS from the stream and rewrites the NAL units from start codes into the length-prefixed form MP4 requires, so you do not need a bitstream filter in this direction.

When not to use it: the result is only as good as the raw stream. If it has variable frame timing, such as a camera that drops frames, constant -framerate will drift against any audio you add later. Use the original timestamps from the camera container when you have them.

Share

More snippets

Media

List ALSA devices with aplay -l

List the playback cards and devices ALSA sees with aplay -l from alsa-utils 1.2, and turn the output into a hw:card,device name.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.