Wednesday, 8 February 2012
Facebook StumbleUpon Twitter Google+ Pin It

Video Conversion With FFMPEG

Video Conversion With FFMPEG


There are many tools and programs about to convert and to manipulate video for the web. Most of these make use of the freely available ffmpeg library to do the conversions and manipulations.

When used with PHP, the ffmpeg library is typically called using the exec() or system() functions. This provides the fastest method of video conversion without reading the whole video, or multiple video's into memory.

Convert Video to flv

The most commonly requested feature that is asked of web content developers is to convert existing video's to another format. As flash has found its home in providing players, it has become most common to convert video's into flash video's. The ffmpeg library can convert most any existing video format, to any other format, but for the purpose of this tutorial, the video files will be converted to flash flv files.

Using the command line, the conversion from an mpeg video, to a flash video would be done like this:
ffmpeg -i video.avi -ar 22050 -ab 32 -f flv -s 320x240 video.flv

From this command line, it is simple to wrap this with the PHP exec() function to produce the result with PHP.


/*** convert video to flash ***/
exec("ffmpeg -i video.avi -ar 22050 -ab 32 -f flv -s 320x240 video.flv");

  • -i Input file name

  • -ar Audio sampling rate in Hz

  • -ab Audio bit rate in kbit/s

  • -f Output format

  • -s Output dimension

Convert Video To JPG Sequence

Another common requested feature is to convert a video into a series of JPEG images. Once again the exec function can be used to wrap the following command line input.
ffmpeg -i video.mpg -an -r 10 -y -s 320x240 video%d.jpg

exec("ffmpeg -i video.mpg -an -r 10 -y -s 320x240 video%d.jpg");
  • -i Input file name

  • -an Disable audio

  • -r fps

  • -y Overwrite file

  • -s Output dimension

Convert Every n seconds to JPEG

The method above graps a whole lotta frames and creates many jpg images. This is good if the goal is to create some sort of animation, but in real world practice, a frame every "n" seconds is generally required. To grab a frame every five seconds, some simple math is required. Every five seconds will be one fifth (1/5) or 0.2, so to grab ever 0.2 frame, this code is used.
ffmpeg -i movie.mpg -r 0.2 -sameq -f image2 thumbs%02d.jpg

Convert Specified Frame To JPG

ffmpeg -i video.mpg -an -ss 00:00:03 -t 00:00:01 -r 1 -y -s 320x240 video%d.jpg
  • -ss Record start time

  • -t Record end time last for
So if you want to save frame 4 (00:00:04) -ss 00:00:03 -t 00:00:01. Note: it is count from 00:00:00. Even you want to save one jpg, you still need to use %d for naming, it is strange that I grab one frame for one second, it will return two identical jpg files for me

Watermark With Image Overlay

In this example, an image is used as an overlay on the video to create a watermark effect. The image used in this uses a transparent PNG to achieve the effect.
ffmpeg -i movie.mpg -vhook '/usr/lib/vhook/ -f overlay.png -m 1 -t 222222' -an mm.flv
  • -i Input video file

  • -vhook Path to

  • -f Path to overlay image

  • -m Mode

  • -t Threshold
The movie is converted as seen in earlier examples, and adds the image overlay.png over the top. The threshold is a hex figure the same as used with html codes.

Add Timestamp To Video

Adding the date and time to an image is done with the vhook and specifying a font to write onto the image. The font can be a system font and all that is needed is the path to it. This provides options for fancy text etc.
ffmpeg -r 29.97 -s 320x240 -i movie.mpg -vhook '/usr/lib/vhook/ -c white -F FreeSans.ttf/12 -x 0 -y 0 -t %A-%D-%T' timestamp.flv
  • -r

  • -s Video Dimensions

  • -i Input file

  • -c Color of text

  • -F Path to font

  • -x X co-ordinate

  • -y Y co-ordinate

Rip MP3 From Video

A number of sites have emerged that capture video data and rip the sound track from them and convert it to MP3. This is done with the audio codec and is simply done as follows
ffmpeg -i movie.flv -vn -acodec copy movie.mp3

I would rather see a sermon than hear one any day. I'd rather have you walk with me, than merely point the way. The eye is a more ready pupil than ever was the ear, good advice is often confusing, but example is always clear.

No comments: