Aug 10

Using FFmpeg to convert videos to FLV

Posted By Ahmed El-Kilani On 10 Aug 2007 No Comments »

This post is to show how to programatically convert most famous media types to .flv, which is suitable for flash players and streaming over the internet for its small size.
The most suitable tool to do this job for us is the free open source FFmpeg. The tool is a very fast video and audio converter. It can also grab from a live audio/video source. The command line interface is designed to be intuitive, in the sense that FFmpeg tries to figure out all parameters that can possibly be derived automatically. You usually only have to specify the target bitrate you want. FFmpeg can also convert from any sample rate to any other, and resize video on the fly with a high quality polyphase filter.

To make use of the ffmpeg executable. I have written a simple class that calls this command-line executable and pass all necessary data and settings with its arguments.

The tool:

Convert video
Arguments:
-i : media file name to convert
-ar : audio frequency
-ab: audio bitrate
-f: output format
-y: output file

example:
ffmpeg.exe -i input.avi -ar 44100 -ab 32 -f flv -y output.flv

Grab thumbnail from movie files
Arguments to grab thumbnail image from the movie file:
-ss: skip time (time to be skipped before snapshot image should be grabbed)
-t: period of time to take images within
-s: size of thumbs
-r: frames per second
-f: imge format (for jpg use image2)
example:
ffmpeg.exe -i input.avi -ss 00:00:05 -t 00:00:01 -s 100x70 -r 1 -f image2 -y output.jpg

Note: ffmpeg can grab all frames of the given movie, -ss will skip the given time and -t will allow ffmpeg to grab only images within this time.To grab only one image in a time, give -t a value of 1 second and -r a value of 1 frame /sec
so only in a second there is an image.

The class has two main methods, one is for converting movie types and another for grabbing thumbs.

Points of interests:

-The class will format the previous command line and call the exe invisibly, using System.Diagnostics.Process class.
-An important line :

process.WaitForExit();

This will force the thread to sync (wait) until the convertion is completed, this will allow us to make sure that everything is ok before going to the next step.

example:

FFMpegWrap wrap = new FFMpegWrap("c:\\vid.mpg")
wrap.ExeLocation @"D:\FFMpeg"
wrap.Convert("c:\\vid.flv", Format.FLV);


Files are available in the attachments
Download FFmpeg open source project

FFMpegWrapper.rar (69.07 KB)