This post contains a number of useful commands for ffmpeg, a tool for converting video file formats. This post shows how to convert AVI to MPEG, MPEG to FLV, how to extract sound from a video, and how to get still frames from a video.

Video information

You can have ffmpeg read a video file and show information about it like so:

$ ffmpeg -i video.avi

This will tell you the video length, bitrate, and video and audio stream formats, e.g.

Input #0, flv, from '38.flv':
 Duration: <strong>00:03:23.07</strong>, start: 0.000000, bitrate: <strong>401 kb/s</strong>
 Stream #0.0: Video: <strong>flv</strong>, yuv420p, 320x262, 337 kb/s, 25 tbr, 1k tbn, 1k tbc
 Stream #0.1: Audio: <strong>mp3</strong>, 22050 Hz, mono, s16, 64 kb/s

Getting a still from a video

In order to get a single frame from a video and save it as a JPEG so that you can use it as a preview, do:

$ ffmpeg -i video.avi -an -ss 00:01:15 -r 1 -vframes 1 image.jpg

This will record 1 frame (vframes=1) at a framerate of 1 (r=1), at position 1:15 , ignoring audio (-an). You can record multiple stills this way: set the number of frames (vframes) to the number of stills you want, and specify a framerate. If you set r=10, stills will be recorded every 10 seconds. You will need to specify a naming system for the output files:

ffmpeg -i video.avi -an -ss 00:00:00 -r 10 -vframes 50 image%d.jpg

You can specify the frame size (resolution) for your stills:

ffmpeg -i video.avi -an -ss 00:00:00 -r 10 -vframes 50 <strong>-s 640x480</strong> image%d.jpg

Or fiddle with the aspect ratio:

ffmpeg -i video.avi -an -ss 00:00:00 -r 10 -vframes 50 <strong>-aspect 5:3</strong> image%d.jpg

Extracting sound from a video file

If you want to extract the sound from a video file and save it off as an mp3, do:

ffmpeg -i video.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 sound.mp3

Here, we use -vn to disable video. We set the audio sampling rate to ar=44.1 kHz, number of channels to ac=2, and the bitrate to ab=192 bps. The result is saved to mp3 (we force mp3 format using the -f flag).

Convert video formats

Nothing could be easier in ffmpeg (ignoring bags of flags to finetune the output). Converting AVI to MPEG is done like this:

ffmpeg -i video.avi video.mpg

And the other way around:

ffmpeg -i video.mpg video.avi

Or AVI to FLV:

ffmpeg -i video.avi video.flv

The flag you’ll probably need is:

ffmpeg -i video.avi -s 320x240 video.flv

This will change the movie resolution to 320×240, which will be handy when you plan to show your video online.

Full documentation for ffmpeg is here.