brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ffmpeg -movflags +faststart moves the MP4 index to the front for web playback

Ffmpeg
~/tips/tip-ffmpeg-movflags-faststart

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

Rewrites an MP4 so the index is at the start and playback begins before the download ends.

A video on a web page that has to download completely before it plays is a classic annoyance. For MP4 the cause is often where the index sits in the file, and ffmpeg can move it.

The ffmpeg formats documentation describes the faststart flag of the MP4 muxer as a second pass that moves the index, the moov atom, to the beginning of the file. The syntax in its own examples has a plus sign in front of the flag:

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

With -c copy the audio and video are copied as they are, so the run is quick and nothing is re-encoded. You only get a new file with the index in front. A player can then start as soon as the first bytes arrive, instead of waiting for the end of the file where the index used to sit.

Use a new output name and keep the original until you have played the result. The flag belongs to the output, so it can also go on any other command that writes an MP4.

The caveat is in the docs: the extra pass can take a while, and it does not work in various situations, such as fragmented output. That is why faststart is not enabled by default. If you create fragmented MP4 for streaming, this flag is not the one you need. The documentation also mentions a separate tool called qt-faststart that does the same job.

Further reading: FFmpeg Formats Documentation

More Ffmpeg tips ffmpeg -stream_loop 2 -i in.mp4 -c copy out.mp4 ffmpeg -i in.mp4 \ ffmpeg -i video.mp4 -i video.en.srt -map 0 -map 1 -c copy -c:s mov_text -metadata:s:s:0 language=eng video-subs.mp4 ffmpeg -i film.mkv -map 0:v -map 0:a:0 -c:v copy -c:a aac -b:a 192k -ac 2 film-stereo.mkv ffmpeg -i talk.mp4 -vn -c:a copy talk.m4a All Ffmpeg tips (38) All topics

Link