Ch. 16 · Containers in Depth

한국어日本語
6 min

faststart: Why moov Needs to Come First

For an encoder, writing moov at the end of the file is the natural thing to do. Here is why it has to be moved to the front for instant playback on the web, and how.

While an encoder is writing an MP4, it doesn't yet know how many frames there will be or how many bytes each one takes. The sample table can only be completed after everything has been written, so FFmpeg and similar tools write moov at the end of the file by default. The file structure then ends up in the order ftyp → mdat → moov.

Default output: moov at the end ftyp mdat moov With faststart: moov at the front ftyp moov mdat
Two layouts, depending on where moov sits

Why it has to be at the front

Think of playing an MP4 on the web while it downloads progressively. If moov is at the end, the player has to fetch the tail of the file first to get the index. Some players do that with an HTTP range request (Range) when the server supports it, but that means more requests and a potentially later start. If moov is at the front, the index is in the first few KB received, so playback can start right away. For this reason it is generally recommended to move moov to the front for web MP4s.

Move moov to the front without re-encoding (or apply it while encoding)
# For a file you already made: copy the streams as they are and only move moov to the front
ffmpeg -i input.mp4 -c copy -movflags +faststart output.mp4

# Apply it in one step while encoding
ffmpeg -i input.mov -c:v libx264 -crf 23 -c:a aac -movflags +faststart output.mp4

FFmpeg's +faststart is known to work by first writing the file as usual, then moving the moov it built at the end to the front and writing the file one more time. So for large files there is extra copying time after it finishes. The video content (the bitstream) doesn't change, so quality is not affected.

Table-of-contents analogy

If the table of contents (moov) is at the very back of a book, you have to open the end of the book first to find out where each chapter is. faststart moves the table of contents to the front of the book. The body text (mdat) stays as it is.

In the field: when to care

If you upload an mp4 to your own site and play it with <video>, it is safest to apply faststart before uploading. By contrast, in approaches that split the video into pieces, like HLS and DASH, each piece has its own structure, so this problem shows up in a different form (the initialization segment). We cover it in the next lesson.

faststartfaststart

A layout that moves the moov box to the front of the file so playback and seeking can start even while the file is still downloading. In FFmpeg it is -movflags +faststart.

Progressive downloadProgressive download

Playing a file while it is being downloaded over HTTP. It is strongly affected by where the index sits.

Range requestRange request

An HTTP way to request only part of a file. Used for seeking and reading the end of a file.

Check yourself

What changes in an MP4 with faststart applied?