Posts tagged ‘mpeg-4’

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):

$ find / -name 'ffmpeg' -type d

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

$ 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.

$moov_atom = 'sudo qt-faststart "' . $target . '" "' . $destination . '"';