Make an MP4 that plays everywhere with FFmpeg

Encode with -c:v libx264 -profile:v high -pix_fmt yuv420p -c:a aac -movflags +faststart. That gives 8-bit 4:2:0 H.264, the kind Chrome hands to the hardware decoder in our test, AAC audio, and the index (the moov box) at the front so playback can start before the download ends. If the file is already H.264 and AAC, -c copy -movflags +faststart moves the index without re-encoding.

Fix in place Stream copy

The file is already H.264 and AAC and only needs to start faster. No re-encode.

ffmpeg -i in.mp4 -c copy -movflags +faststart out.mp4

Same size, same packets: the stream MD5 is identical before and after. The moov box moved from byte 12,591,177 to byte 40.

moov box
byte 12,591,177 to byte 40
Media data
Unchanged, same MD5
Run time
100 ms
Output
12.6 MB
Full encode Re-encode

The source is a camera or editor file, 10-bit, 4:2:2 or 4:4:4, PCM audio, or you are not sure.

ffmpeg -i in.mov -c:v libx264 -crf 23 -profile:v high -pix_fmt yuv420p -c:a aac -b:a 128k -movflags +faststart out.mp4

H.264 High at level 3.1, 8-bit 4:2:0, AAC at 128 kb/s, moov at byte 40. From a 23.9 MB MOV of 4:2:2 10-bit video and PCM audio.

Video
H.264 High, 8-bit 4:2:0
Audio
AAC 128 kb/s
Run time
3.0 s
Output
9.4 MB

Proof

Where the index sits

Both files drawn to scale, 12.6 MB wide. The moov box holds the index a player needs before it can show a frame. It is 22 KB, drawn wider than scale so you can see it.
Default FFmpeg MP4 Index at the end
Byte layout of in.mp4: ftyp, then 12.6 MB of mdat, then the moov box at the very end
byte 12,591,177 A player has to read the last 22 KB of the file before it can show the first frame.
-c copy -movflags +faststart Index first
Byte layout after -movflags +faststart: ftyp, the moov box, then the 12.6 MB of mdat
byte 40 The first 22 KB of the download is the whole index. The media data after it is byte for byte what it was.

Footage: Sintel, © Blender Foundation, CC BY 3.0. Cut to 30 s and re-encoded into the test formats by us.

Run it

Run it on Rendobar

Send the same command from your code. The job fetches each input from its URL and returns the output file's URL when it finishes.

job.ts
import { createClient } from "@rendobar/sdk";
const rb = createClient({ apiKey: process.env.RENDOBAR_API_KEY });
const job = await rb.jobs.run({
type: "ffmpeg",
inputs: {
"in.mov": "https://cdn.rendobar.com/examples/web-playable-mp4/in.mov",
},
params: {
command: "ffmpeg -i in.mov -c:v libx264 -crf 23 -profile:v high -pix_fmt yuv420p -c:a aac -b:a 128k -movflags +faststart out.mp4",
},
});
console.log(job.output.file.url);

Install with npm i @rendobar/sdk. jobs.run() submits and waits, so it returns the finished job in one call.

terminal
curl https://api.rendobar.com/jobs \
-H "Authorization: Bearer $RENDOBAR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"type": "ffmpeg",
"inputs": {
"in.mov": "https://cdn.rendobar.com/examples/web-playable-mp4/in.mov"
},
"params": {
"command": "ffmpeg -i in.mov -c:v libx264 -crf 23 -profile:v high -pix_fmt yuv420p -c:a aac -b:a 128k -movflags +faststart out.mp4"
}
}'

Returns immediately with a job id. Poll GET /jobs/{id} or register a webhook rather than blocking on the request.

Every variant

Pick the command for your case

Each one ran on the same input. The line under it is what came out. Run times are the best of three runs with FFmpeg 8.0 on one desktop machine, so read them as relative.

  1. Write a fragmented MP4 for streaming or a pipe

    Stream copy
    ffmpeg -i in.mp4 -c copy -movflags +frag_keyframe+empty_moov out.mp4

    12 fragments, one per keyframe, behind a 1,258-byte moov with no sample table. It also writes to stdout, where +faststart fails.

    97 ms run 12.6 MB 30.08 s long

  2. Fix "width not divisible by 2"

    Re-encode
    ffmpeg -i odd.mp4 -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -c:v libx264 -crf 23 -pix_fmt yuv420p -c:a aac -movflags +faststart out.mp4

    853x481 became 852x480, H.264 High 4:2:0. Without the scale, libx264 stops with "width not divisible by 2 (853x481)".

    576 ms run 1.3 MB 6.02 s long

  3. Make a web MP4 in H.265

    Re-encode
    ffmpeg -i in.mov -c:v libx265 -crf 28 -tag:v hvc1 -pix_fmt yuv420p -c:a aac -b:a 128k -movflags +faststart out.mp4

    H.265 Main, 8-bit 4:2:0, tagged hvc1, AAC, moov at byte 36. 51% of the H.264 file's size.

    6.4 s run 4.8 MB 30.00 s long

  4. Make a small MP4 for phones

    Re-encode
    ffmpeg -i in.mov -vf scale=-2:360 -c:v libx264 -crf 26 -pix_fmt yuv420p -c:a aac -b:a 96k -movflags +faststart out.mp4

    844x360, H.264 High at level 3.0, AAC 96 kb/s, moov first. A third of the size of the 1280 version.

    1.7 s run 3.1 MB 30.00 s long

Gotchas

Where it goes wrong

How do I check whether the moov atom is at the start?

Ask ffprobe for its trace log and keep the top-level boxes. Each line ends with the box size, its offset in the file and the file size:

ffprobe -v trace -i in.mp4 2>&1 | grep "parent:'root'"

ftyp at 8, free at 40, mdat at 48, moov at 12,591,177. The moov comes after the media, so the file is not fast-start.

Run it on the faststart output and the order flips: ftyp at 8, moov at 40, then free and mdat. If moov comes before mdat, the file starts fast.

My MP4 already plays in Chrome. Why bother with yuv420p?

Because desktop Chrome forgives a lot. Chrome 157 on Windows played 5 s cuts of all four test files in a plain video tag: 8-bit 4:2:0, 10-bit 4:2:2, 4:4:4 and the MOV with PCM audio. Its own media APIs tell a different story:

  • H.264 High 4:2:0: canPlayType says "probably", and mediaCapabilities reports it power efficient, so the hardware decodes it.
  • High 4:2:2 and High 4:4:4: "maybe", supported but not power efficient. That is software decoding.
  • PCM in MP4 (ipcm): canPlayType returns an empty string and Media Source Extensions refuse it, and MSE is what streaming players feed.

Re-encoding does not fix this by itself. Without -pix_fmt yuv420p, libx264 kept the 10-bit 4:2:2 of in.mov and wrote High 4:2:2. Adding -profile:v high without it stops the encode with "high profile doesn't support 4:2:2". Set both.

yuv420p vs yuv444p compares the pixel formats on file size and on why the others fail in QuickTime and Safari.

faststart or fragmented MP4?

Use +faststart for a file people download or stream from a URL. It rewrites the finished file once to move the index, so it needs to seek in its output.

Use +frag_keyframe+empty_moov when the output is a pipe or a live upload. Writing to stdout with +faststart failed with "muxer does not support non seekable output". The fragmented version wrote all 12.6 MB to the pipe.

Related: Convert to MP4, Encode H.264 and H.265, Compress a video, Resize a video.

Your command. Our infrastructure.

Bring your own commands. Run them on scalable CPU and GPU infrastructure, and pay only for compute time.