Virginia Beach Web Development | Website Developer | Mobile Dev | Mobile Video Integration | Doodersrage

Just about everything Website Development related. Virginia Beach VA headquartered with a global reach!

Convert video to iPhone friendly H264 with FFMPEG

Posted on | January 18, 2012 | 1 Comment

ffmpeg logo 300x75 Convert video to iPhone friendly H264 with FFMPEG

FFMPEG is a cross-platform solution for recording, converting, and steaming audio and video. There is even a PHP module for gathering media information. It typically cannot be installed by using a yum or apt-get repository though.

Many guides exist for installing FFMPEG on multiple platforms. This quick tutorial will be limited to its use on the CentOS platform. One installation guide for CentOS can be found here: http://www.nazly.net/installing-ffmpeg-and-ffmpeg-php-on-centos/

Once you have installed FFMPEG along with its codecs and PHP module you can now access media information from directly inside of your PHP script. A quick example of how to do so is below:

$movie = new ffmpeg_movie('/var/html/www/video/myvideo.mp4');
$width = $movie->getFrameWidth();
$height = $movie->getFrameHeight();

The above example will gather information about the selected movie, assuming the video actually exists. This is just two of the many object methods supported by the ffmpeg-php module, a complete list can be viewed here: http://ffmpeg-php.sourceforge.net/doc/api/ffmpeg_movie.php

Okay! That’s nice! But I want to compress an existing video to an iPhone compatible format! This can be done easily through BASH or whatever shell you prefer within the CentOS console or through an SSH session.

/usr/bin/ffmpeg -y -i "/var/html/www/videos/input.flv" -s 320x180 -r 30000/1001 -b 400k -bt 500k -vcodec libx264 -vpre default -acodec libfaac -ac 2 -ar 44100 -ab 128k "/var/html/www/videos/output.mp4";

The above line reads in the input.flv file then outputs the output.mp4 file.

Directives read:
-y overwrite if existing output file is found
-i input file
-s (frame size) 320×180 or 16:9 iPhone format
-r frame rate Hz 29.97 or 30000/1001
-b bitrate or 400kbps
-bt max bitrate of 500kbps
-vcodec video codec of libc264 for h264 format
-vpre video preset file of default
-acodec audio codec of libfaac for aac audio
-ac number of audio channels set to two
-ar for audio sampling frequency of 44100 (For iPhones you could also do 48000)
-ab audio bitrate of 128kbps

A complete list of ffmpeg options can be found here: http://ffmpeg.org/ffmpeg.html

Will add more ffmpeg tricks later!

Comments

One Response to “Convert video to iPhone friendly H264 with FFMPEG”

  1. Generating a thumbnail image with FFMPEG : Virginia Beach Web Development Developer | Databases | Video Games | Computer Tech Support | SEO | Doodersrage
    January 20th, 2012 @ 6:17 pm

    [...] If you would like to know how to compress a video for the iPhone or how to quickly use FFMPEG-PHP use this guide: CONVERT VIDEO TO IPHONE FRIENDLY H264 WITH FFMPEG [...]

Leave a Reply