Placing “moov atom” at the beginning of an MPEG-4 video with FFMpeg

According to Atomic Parsley’s article on media meta data, MPEG-4 files consist of atoms (or boxes). One of these important atoms is called the moov atom, and this moov atom contains mdat data which is important in communicating seek points. So long story short, after encoding a number of MP4 files I noticed that the entire video had to download before playing via Safari. The issues was that ffmpeg was placing the moov atom at the end of the mp4 file, so Safari could not determine seek points or other important pieces of information to allow the video to play before the entire file had downloaded (including the moov atom).

Fortunately ffmpeg is now packaged with a solution; qt-quickstart. I followed these steps to get qt-quickstart installed and integrated into my encoding process for mp4 files, and now videos play and can be ‘seek-ed’ before they have fully downloaded. Note, this assumes a fairly recent build of ffmpeg.

First, find your ffmpeg directory (I couldn’t remember where I installed it, so I just did a quick search for an ffmpeg directory):

1
$ find / -name 'ffmpeg' -type d

Next, install qt-quickstart by browsing to the ffmpeg folder returned above and running the following commands:

1
2
3
$ cd /usr/local/src/ffmpeg/
$ sudo make tools/qt-faststart
$ sudo checkinstall --pkgname=qt-faststart --pkgversion="$(date +%Y%m%d%H%M)-svn" --backup=no --deldoc=yes --fstrans=no --default install -D -m755 tools/qt-faststart /usr/local/bin/qt-faststart

Now it’s as simple as calling qt-quickstart via PHP; one thing to note is that qt-quickstart will generate a new file, so you may want to delete your old file (in this case, $target) after you run the qt-quickstart command.

1
2
$moov_atom = 'sudo qt-faststart "' . $target . '" "' . $destination . '"';
exec($moov_atom);

3 Comments

  1. Doug Fox says:

    Thank you so much! I am just getting started with making our archival videos playable online and your straightforward instructions for this nifty feature of ffmpeg worked like a charm. Saved me hours of work. Cheers!

  2. Brian Robinson says:

    Thank you so much! I have been messing with mp4′s for the past couple of days off and on trying to figure out why some of the ones encoded with megui and ran through MP4Box on a Windows machine would stream load across my home network to my new Kindle Fire and the newly encoded ones on my Arch Linux machine would fail. Turns out the Fire needs the moov atom at the beginning in order to play the files without downloading the whole thing.

  3. Mark says:

    Thank you very much this saved me alot of time…

    regards!

Leave a Reply