Web Development Bug Report

MP4 video waits for the full download before playing: moov atom at the end and no range support

Short product videos took 20 seconds to start on mobile even though the first frame was tiny. The MP4 index sat at the end of each file, and our download endpoint ignored Range requests, so the browser had to fetch everything first. Faststart plus 206 responses fixed it.

Aarav Mehta Aarav Mehta @aaravmehta · November 18, 2026 · 8 min read · 349 Views
584-mp4-slow-start-moov-atom-faststart

Our e-commerce site shows a short product video on each product page, usually 30 to 90 seconds, 20 to 60 MB. A performance review flagged them: on mobile connections the spinner ran for 15 to 25 seconds before the first frame appeared, while the poster image loaded instantly. Real user monitoring showed that the time from clicking play to the playing event grew almost linearly with file size, which is not what streaming video should look like.

In Chrome DevTools, the Network panel for one video showed a single request:

GET /media/products/8812/demo.mp4   200   video/mp4   48.2 MB   19.4 s

The playing event fired a few hundred milliseconds after that request finished. The browser was downloading the whole file before showing anything.

Reproducing it

With network throttling set to a fast mobile profile, every product video behaved the same. A video uploaded by our own team through a different tool started almost immediately and showed a very different request pattern: a small first request, then several requests answered with 206 Partial Content.

What we ruled out

  • CDN caching. The slow pattern repeated on cache hits. Speed of delivery was not the problem; the amount delivered before playback was.

  • preload settings. We tried preload="metadata" and preload="auto". With metadata the browser still had to fetch most of the file before it could even show duration.

  • Bitrate. The slow and fast files had similar bitrates.

Root cause

Two problems stacked on each other. First, the product videos came from a recording tool that writes the MP4 index, the moov box, at the end of the file. A player cannot decode anything until it has read moov. Second, these files were served through an application endpoint that checked permissions and then streamed the file with 200 OK, ignoring the Range header. Without range support the browser could not jump to the end to read moov first, so it had to download every byte in order.

What is happening under the hood

An MP4 file is a sequence of boxes (also called atoms). The important top-level ones are:

Box

Contents

ftyp

File type and compatible brands

moov

The index: tracks, codecs, timing, and the byte offset of every sample

mdat

The actual audio and video data

Recorders write mdat as they go, because they do not know the final sample table until recording stops. Then they append moov. That layout is fine for local playback, where seeking to the end is free. Over HTTP it means the index is the last thing to arrive.

Browsers handle this with range requests: they ask for the start of the file, find that moov is not there, then request the last bytes with a Range header, read the index, and go back to mdat. That costs extra round trips but works, as long as the server answers with 206 Partial Content and a Content-Range header. If the server ignores Range and always sends 200 with the whole body, the browser has no way to skip ahead.

Moving moov before mdat, called faststart, removes the extra round trips entirely: the first response contains the index, and playback starts as soon as the first few seconds of mdat arrive.

Environment

Component

Version / value

Files

MP4, H.264 + AAC, moov after mdat

App endpoint

PHP 8.3 controller streaming files after an authorization check

Web server

nginx 1.26 in front of the app

CDN

Pull-through, caching by URL

Browsers

Chrome 141 on Android 15, Safari 26 on iOS 26

Minimal reproduction

Check where moov sits. ffprobe's trace log prints each top-level box with its offset:

ffprobe -v trace demo.mp4 2>&1 | grep -E "type:'(ftyp|moov|mdat)'" | head -3
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5583f0] type:'ftyp' parent:'root' sz: 32 8 48211940
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5583f0] type:'mdat' parent:'root' sz: 48102214 40 48211940
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5583f0] type:'moov' parent:'root' sz: 109686 48102254 48211940

mdat before moov means the index is at the end. Then check range handling:

curl -s -o /dev/null -D - -H "Range: bytes=0-1023" https://shop.example.com/media/products/8812/demo.mp4
HTTP/2 200
content-type: video/mp4
content-length: 48211940

A server that supports ranges answers 206 with content-range: bytes 0-1023/48211940.

Who is affected

Any site serving MP4 files written by cameras, screen recorders, phones or encoders that do not move the index, especially when files go through an application endpoint instead of the web server's static file handler. Static file serving in nginx and object storage like S3 support ranges by default, so the second half of the problem usually comes from custom download code.

The fix

1. Faststart every published file. This is a lossless remux, no re-encoding:

ffmpeg -i demo.mp4 -c copy -movflags +faststart demo.fast.mp4

ffmpeg writes the file, then runs a second pass that moves moov to the front. After that the trace shows ftyp, moov, mdat in that order. We added the flag to the upload worker, so every new video is processed once on upload, and ran a one-off job over the existing library.

2. Let the web server do range requests. Instead of streaming bytes from PHP, the controller now does the authorization check and hands the file to nginx with X-Accel-Redirect. nginx then serves it from an internal location with full range support:

location /protected-media/ {
    internal;
    alias /srv/media/;
    # static file handler: Range, 206 and Accept-Ranges come for free
    add_header Cache-Control "private, max-age=3600";
}
// MediaController::show (excerpt)
$this->authorize('view', $video);
return response('', 200, [
    'Content-Type'     => 'video/mp4',
    'X-Accel-Redirect' => '/protected-media/' . $video->path,
]);

Public product videos skip the app entirely now and are served as static files by the CDN.

3. Keep preload="metadata". With faststart it now fetches only the first part of the file, enough for duration and the first frame, without downloading the whole video on page load.

Alternatives and trade-offs

  • Fragmented MP4. -movflags frag_keyframe+empty_moov writes a small moov at the start and data in fragments. Useful for live or still-growing files; slightly less compatible with some older players than a faststart file.

  • Adaptive streaming (HLS or DASH). The right choice for long videos and varying networks: the player picks quality per segment. It adds a packaging step and more files to manage; for 60-second clips we found faststart MP4 enough.

  • Implementing Range in the app. Possible, but parsing ranges correctly, including multiple ranges and edge cases, is easy to get wrong. Let the web server or object storage do it.

How to test the fix

ffprobe -v trace demo.fast.mp4 2>&1 | grep -E "type:'(ftyp|moov|mdat)'" | head -3
curl -s -o /dev/null -D - -H "Range: bytes=0-1023" https://shop.example.com/media/products/8812/demo.mp4 | head -3
... type:'ftyp' parent:'root' ...
... type:'moov' parent:'root' ...
... type:'mdat' parent:'root' ...
HTTP/2 206
content-range: bytes 0-1023/48211940

In DevTools with the same throttling, the first frame now appears after about 1.2 seconds, and the Network panel shows a series of 206 responses instead of one large 200.

Confirming in production

Real user monitoring tracks the time from the play click to the playing event. The 75th percentile on mobile dropped from 14.8 seconds to 1.6 seconds, and it no longer grows with file size. CDN egress for product videos also fell by about a third, because visitors who watch only the first ten seconds no longer download the whole file.

Regression guard

  • The upload worker refuses to publish a video until a trace check confirms moov comes before mdat.

  • An HTTP check in our synthetic monitoring requests bytes=0-1023 for a sample video every hour and alerts on any status other than 206.

  • A code review rule flags controllers that return video bodies directly instead of using X-Accel-Redirect or a signed storage URL.

Security implications

Moving to X-Accel-Redirect keeps the authorization check in the app, and the internal directive stops anyone from requesting /protected-media/ directly. Make sure the path you pass is built from a stored value, never from user input, or a crafted path could expose other files. Range support itself adds no exposure, but if you cache protected files at a CDN, cache them only with private, per-user or signed URLs.

Prevention checklist

  • Run -movflags +faststart on every MP4 you publish.

  • Check moov placement with ffprobe -v trace in the upload pipeline.

  • Serve video through the web server, CDN or object storage, not by streaming bytes from app code.

  • Confirm Range requests return 206 with Content-Range.

  • Use preload="metadata" for videos below the fold.

References

Add a label

#ingress-nginx #ffmpeg #mp4 #http-range #video
Written by
Aarav Mehta @aaravmehta

I build AI features into products: retrieval over company documents, agents that call internal tools, and voice interfaces. Before that I was a backend engineer, which shapes how I approach LLM systems: as distributed systems with an unusually unpredictable component in the middle. My posts focus on the engineering around the model: context design, permissions for tools, cost and latency budgets, evaluation and the security of third-party extensions. When something fails, I show the trace, the root cause and the test we added, because "the model got it wrong" is rarely the full explanation.

Advertisement

Comments

0

No comments yet. Be the first to comment!

Sign In to post a comment.