Mux H.264 and AAC into MP4 Using FFmpeg

Multiplexing (muxing) separate video and audio tracks into a single container is a fundamental task in video processing. This guide provides a direct, step-by-step tutorial on how to combine a raw H.264 video stream and an AAC audio stream into a standard MP4 file using the command-line tool FFmpeg. By copying the streams instead of re-encoding them, this process is extremely fast and preserves the original quality of both inputs.

The Basic Muxing Command

To combine your H.264 video and AAC audio without re-encoding, use the following FFmpeg command in your terminal:

ffmpeg -i input.h264 -i input.aac -c:v copy -c:a copy output.mp4

Command Breakdown

Handling Video Framerate Issues

Raw H.264 streams do not contain container metadata, which means they lack explicit framerate information. FFmpeg will often assume a default framerate (usually 25 fps). If your original video was recorded at a different framerate (such as 30 fps or 60 fps), the audio and video will be out of sync in the output MP4.

To fix this, you must explicitly declare the input framerate using the -r option before the video input:

ffmpeg -r 30 -i input.h264 -i input.aac -c:v copy -c:a copy output.mp4

Replace 30 with the actual framerate of your raw H.264 stream (e.g., 24, 29.97, 60).

Fixing Audio-Video Sync Offset

If your audio and video are perfectly matched in duration but start at different times, you can introduce a time delay to the audio stream using the -itsoffset parameter.

To delay the audio by 1.5 seconds:

ffmpeg -i input.h264 -itsoffset 1.5 -i input.aac -c:v copy -c:a copy output.mp4