Posts tagged ‘FFmpeg’

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

Speed up image extraction with ffmpeg

A product requirement for a media management application I am building is to automatically extract a user-defined number of images from an uploaded video file. The user can request for up to 20 images to be extracted from a given video. My initial implementation was taking almost 5 minutes to complete a 20 image extraction from a 7 minute video.

1
2
3
4
5
6
7
8
9
10
11
12
13
private function generateThumbnails( $video, $destination, $duration, $thumbnail_count ){

    $duration = (int)$duration;
    $interval = floor( $duration / $thumbnail_count );

    $c = 1;
    while( $c <= $thumbnail_count ){
        $offset = $interval * $c;
        exec("ffmpeg -i \"{$video}\" -ss " . $offset . " -y -vcodec mjpeg -vframes 1 -an -f rawvideo -s 720x404 " . $destination . "thumb_" . $c . ".jpg");
        $c++;
    }

}

Turns out ffmpeg will encode the video to the -ss parameter if you supply the -i parameter before the -ss parameter. So the simple fix was to call -ss before -i. The 20 image extraction now takes less than a second.

1
2
3
4
5
6
7
8
9
10
11
12
13
private function generateThumbnails( $video, $destination, $duration, $thumbnail_count ){

    $duration = (int)$duration;
    $interval = floor( $duration / $thumbnail_count );

    $c = 1;
    while( $c <= $thumbnail_count ){
        $offset = $interval * $c;
        exec("ffmpeg -ss " . $offset . " -i \"{$video}\" -y -vcodec mjpeg -vframes 1 -an -f rawvideo -s 720x404 " . $destination . "thumb_" . $c . ".jpg");
        $c++;
    }

}

Getting FFmpeg running properly on Debian Lenny

We have been using FFmpeg quite a bit lately for a number of projects.  One such project has us encoding uploaded videos into a variety of formats including mpeg4 for iOS devices.  So we fired up FFmpeg, to run a test transcoding with the following command:

1
ffmpeg -i test.mp4 -acodec libfaac -ab 128kb -vcodec mpeg4 -b 1200kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s 320x180 -title X final_video.mp4

We were immediately greeted by an Unknown encoder ‘mpeg4′ error.  There are apparently some licensing issues between ftp-masters and FFmpeg.  Not being a lawyer, I cannot advise on the legality of the following workaround, so this is for informational purposes only.

Turns out we can get ‘full’ installs of FFmpeg from the debian-multimedia repo.  This was as simple as first opening our sources list:

1
$ nano /etc/apt/sources.list

Adding http://www.debian-multimedia.org lenny main, installing the debian multimedia keyring:

1
$ aptitude install debian-multimedia-keyring

Run an upgrade $ aptitude upgrade, and you should be set.  Running that same FFmpeg command output our video.