Converting Video Formats with FFmpeg
April 28th, 2006 by Suramya Tomar in
Today's affordable digital video cameras have placed the power of digital recording within most people's reach. Unfortunately, this has been accompanied with a corresponding increase in the variety of file formats and codecs available. Some of these formats are more efficient than others, and some are less encumbered by proprietary licensing restrictions. So, having the ability to convert from one format to another is a great help, as you can decide what format you are comfortable with and use that one instead of being restricted to a specific file format.
FFmpeg is a simple and straightforward application that allows Linux users to convert video files easily between a variety of different formats. In this article, I walk you through installing FFmpeg and provide a few instructive examples to demonstrate the range of applications for which it can be used.
FFmpeg is an open-source audio and video converter that supports most industry-standard codecs and can convert from one file format to another quickly and easily. It also lets you capture video and audio from a live source and process it.
The source code for FFmpeg is available for download from the project Web site (ffmpeg.sourceforge.net/index.php) and at the time of this writing, the latest version available at the site is 0.4.9-pre1.
Once you download the file, extract it using the following command:
tar -zxf ffmpeg-0.4.9-pre1.tar.gz
This creates a new directory containing the source code for FFmpeg. To install it with the default configuration options, run ./configure from within the FFmpeg source directory. Once the configuration script finishes, compile it by issuing make. Once the compile finishes without any errors, you can install FFmpeg by running make install as root.
On the other hand, if you like to have control over what is installed and prefer customizing software installs, you can pass some command-line parameters to the configure script. To see all the options available for the installer, run the following command:
./configure --help
This command gives you multiple screens of the various settings that can be modified, and you can choose any options you like. The on-screen display does a decent job of explaining what each option does, so I will not go into a lot of detail on this.
I suggest that you enable the following options, but this is not a requirement—feel free to experiment:
--enable-mp3lame: highly recommended—you won't be able to encode MP3s without this. Needs lame to be installed already.
--enable-a52: enables GPLed A52 support, needed for decoding some VOB files.
--enable-gpl: required for the previous component; otherwise, not needed.
As I didn't have lame installed on my system, I ran the following command to configure FFmpeg:
./configure --enable-a52 --enable-gpl
Once the configuration is complete, read through the output to make sure no errors were generated. Then, run make, and go have a drink or something as this might take a little while. Once the system finishes compiling FFmpeg, run make install as root to install FFmpeg, and you are done with the installation.
Now that you have successfully installed FFmpeg, you can start experimenting with it. The first thing you have to do is choose a video file with which to experiment. As this is your first time with FFmpeg, making a backup copy of this file is highly recommended. You don't want to be responsible for ruining the only copy of a rare video.
This input file most probably has been encoded using a particular codec, but because FFmpeg supports most of the popular formats, we don't need to worry a lot about that. Formats supported by FFmpeg include MPEG, MPEG-4 (Divx), ASF, AVI, Real Audio/Video and Quicktime. To see a list of all the codecs/formats supported by FFmpeg, run the following command:
ffmpeg --formats
A detailed list of supported file formats is also available at the FFmpeg site.
FFmpeg supports a large list of command-line parameters that control various settings in FFmpeg. To get a listing of the various options available, run the following command:
ffmpeg --help
Don't let the multipage listing scare you from using FFmpeg, the basic usage is actually very simple. To convert a file with the default settings, run the following command:
ffmpeg -i InputFile OutputFile
The -i option tells FFmpeg that the filename immediately after it is the name of the file to be used as input. If this option is omitted, FFmpeg attempts to overwrite that file when it tries to create the output file. FFmpeg uses the extension of the output file to try to determine the format and codec to use, though this can be overridden using command-line parameters (more on this later).
The default settings create an output file that has radio-quality sound (64kbps bitrate) and very bad video quality (200kbps bitrate). Fortunately, these settings can be changed for each encoding, which allows you to choose the quality of each file depending on the need.
To change the audio bitrate, add -ab bitrate to the command used earlier, where bitrate is the bitrate you want to use. See www.mp3-tech.org/tests/gb for information on the sound quality the various bitrates represent. I prefer to encode files with a bitrate between 128–192kbps depending my needs, but you can put in a higher value if you so desire. Keep in mind, however, that the higher the bitrate you use, the larger the output file size will be. Also keep in mind that if your source file is encoded in a low bitrate, increasing the bitrate won't accomplish much other than increasing the output file size.
Now, getting a CD-quality audio track for the video doesn't really make sense if the video looks like it was taken using a five-year-old Webcam having a bad day. Thankfully, this problem also is easily solved by adding another parameter to the command line.
To change the video bitrate, add the -b bitrate option to the command line. The bitrate here can be any numeric value you like, and I have seen bitrates all the way up to 23,000 (DVD Rips). Although the quality of video encoded with a 23,000kbps bitrate is amazing, the resulting file size of that encoding is also very amazing (a 90-minute video is about 4GB). In my experience, most videos look pretty decent at bitrates between 1,000–1,400, but this is a personal preference, so play with the numbers until you figure out what works for you.
So, to encode a video with a 128kbps audio bitrate and 1,200kbps video stream, we would issue the following command:
ffmpeg -i InputFile.avi -ab 128 -b 1200 OutputFile.mpg
If you are creating a video CD or DVD, FFmpeg makes it even easier by letting you specify a target type. Then, it uses the target type to calculate the format options required automatically. To set a target type, add -target type; type can be vcd, svcd, dvd, dv, pal-vcd or ntsc-svcd on the command line. So, if we were creating a VCD, we would run the following command:
ffmpeg -i InputFile.mpg -target vcd vcd_file.mpg
FFmpeg also has support for encoding audio files. The command to convert audio files is the same as the command to encode video files. To convert a WAV file to a 128kbps MP3 file, issue the following command:
ffmpeg -i Input.wav -ab 128 Output.mp3
Now, the biggest selling point of FFmpeg is that you can customize it to a level that you are comfortable with. So, if all you want to do is convert from one codec to another, and you don't really care about the advanced features, you can stop reading here and still be able to encode/decode videos. On the other hand, if you like to have more control over the encoding, keep reading as we cover more of the advanced options available in FFmpeg.
There are far too many options available in FFmpeg for me to go over each of them here, so I cover some of the ones I found most interesting and leave the rest for you to explore.
There are a times when you will want to encode a video using a particular codec and file format. FFmpeg lets you choose the codec with which you want to encode by adding -vcodec codec to the command line, where codec is the name of the codec you want to use. So if we want to encode using the MPEG-4 codec at 1,200kbps video bitrate and 128kbps audio bitrate, the command looks like this:
ffmpeg -i InputFile.mpg -ab 128 -b 1200 -vcodec mpeg4 OutputFile.avi
Let's say you have recorded a video that has a lot of background noise and undesired commentary, so you decide to remove the audio component of the video completely. To accomplish this, all you have to do is add the -an option to the command line, and FFmpeg automatically removes all audio from the output. Keep in mind that using this option negates any other option that affects the audio stream.
So, in our example, to remove the audio component, we would run the following command:
ffmpeg -i InputFile.mpg -an -b 1200 OutputFile.avi
Let's say you downloaded a news video from the Net that you want to listen to on your iPod on the way to work, but in order to do that, you have to remove the video component from the output file. FFmpeg allows you to remove the video component of the file completely by adding the -vn option to the command line. Using this option negates any other option that affects the video stream.
So, in our example, to remove the video component and save the audio as a 256kbps MP3 file, we would run the following command:
ffmpeg -i InputFile.mpg -vn -ab 256 OutputFile.mp3
Many DVDs have multiple language tracks available, and you can choose in which language you want to watch the video. Having multiple audio tracks is cool if you speak multiple languages and want to be able to watch videos in multiple languages. However, if you don't speak multiple languages, the extra audio tracks are useless and are taking up disk space.
FFmpeg lets you choose which streams you want to keep and ignore the rest. The command-line parameter that allows you to map streams is called -map. So, if in our test file, stream 0 is the video stream, stream 1 is the Spanish audio stream and stream 2 is the English audio stream, and we want to keep the English audio in the output file, we would issue the following command:
ffmpeg -i InputFile.mpg -map 0:0 -map 2:1 -b 1200 OutputFile.avi
In my experience, stream 0 in most video files is usually the video stream, and the remaining streams are the audio streams available with the video.
Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer
Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.
Subscribe now!
The Latest
Newsletter
Tech Tip Videos
- Nov-04-09
- Oct-29-09
- Oct-26-09
Recently Popular
From the Magazine
December 2009, #188
If last month's Infrastrucuture issue was too "big" for you then try on this month's Embedded issue. Find out how to use Player for programming mobile robots, build a humidity controller for your root cellar, find out how to reduce the boot time of your embedded system, and if you're new to embedded systems find out the basics that go into one. You can also read about the Beagle Board, the Mesh Potato and a spate of other interestingly named items. And along with our regular columns don't miss our new monthly column: Economy Size Geek.
Delicious
Digg
StumbleUpon
Reddit
Facebook








H264??
On November 5th, 2009 Anonymous (not verified) says:
This machine Convert FLV AAC H264???/ TO any other??
mp3
On November 2nd, 2009 colix (not verified) says:
im tryin to convert a video to mp3 using command ffmpeg -i input filename -vn (ive tried -ab -b to 256 128 64 and also -sameq but nothing) output filename
soz didnt finish it....:P
On November 2nd, 2009 colix (not verified) says:
soz didnt finish it....:P yeah .... i gets me an error:
Seems stream 0 codec frame rate differs from container frame rate: 1000.00 (1000/1) -> 29.97 (30000/1001)
Input #0, flv, from '123.flv':
Duration: 00:02:51.30, start: 0.000000, bitrate: 175 kb/s
Stream #0.0: Video: flv, yuv420p, 320x240, 111 kb/s, 29.97 tbr, 1k tbn, 1k tbc
Stream #0.1: Audio: mp3, 22050 Hz, mono, s16, 64 kb/s
WARNING: The bitrate parameter is set too low. It takes bits/s as argument, not kbits/s
Output #0, mp3, to '123.mp3':
Stream #0.0: Audio: 0x0000, 22050 Hz, mono, s16, 64 kb/s
Stream mapping:
Stream #0.1 -> #0.0
Unsupported codec for output stream #0.0
:S?
command for me to find
On July 25th, 2009 Anonymous (not verified) says:
command for me to find formats is ffmpeg -formats not --formats
I do not like it
On February 10th, 2009 ruby (not verified) says:
I do not like this software. it can not work for me.
I use iMedia Converter to convert DVD and Videos on Mac.
iMedia Converter for Mac can rip DVD and convert video to sorts of video formats including AVI, FLV, MKV, MP4, 3GP, MTS, M2TS, MOD, WMV, M4V etc. It can also extract audio from video to MP3, WMA, WAV, RA, M4A, AAC, AC3, OGG, MKA, etc.
mapping right audio languages
On February 4th, 2009 typxxi (not verified) says:
I don't have any unix linux knowledge, but from my point of view this shouldn't work
ffmpeg -i InputFile.mpg -map 0:0 -map 2:1 -b 1200 OutputFile.avi
the 0:0 is the leading stream, here the video stream
you want to here in future the english stream from 2
(mpeg calls them #0.1 ... where 0 means the inputfile as far as I understand)
You sync the the second stream to the videostream.
ffmpeg -i InputFile.mpg -map 0:0 -map 0:2 -b 1200 OutputFile.avi
if you want spanish a alternate language than this should work
ffmpeg -i InputFile.mpg -map 0:0 -map 0:2 -map 0:1 -b 1200 OutputFile.avi
but this won't work in mp4 (cause mp4 contains only 1 audio stream).
I hope it will work better, 'cause it took a huge amount of time to find these things out.
File Formats
On December 14th, 2008 Random Things (not verified) says:
Exactly which file formats is FFMPEG compatible with?
ffmpeg bitrate
On August 12th, 2008 ChrisC (not verified) says:
Hi,
In my version of ffmpeg bitrate requires a following 'k' if it is kilobytes.
ffmpeg -i InputFile.avi -ab 128 -b 1200 OutputFile.mpg
becomes
ffmpeg -i InputFile.avi -ab 128k -b 1200k OutputFile.mpg
Regards,
Chris
Syntax Error
On January 7th, 2008 Anonymous (not verified) says:
Where you have "--formats" it should be "-formats".
FFMPEG
On November 11th, 2007 Anonymous (not verified) says:
Thanks a lot for the post. It helped me a lot.
I had 2 scenarios in mind and am wondering if those are possible using FFMPEG:
1) How I can output only part of a video using FFMPEG, example: the output video will start form 10 minutes into the source video and end 15 minutes before the end of the source video.
2) Whether it is possible to output 1 video from 2 source videos in the following manner: output video starts 10 minutes into the first source video and end 15 minutes before the second source video.
I hope you understand what I'm looking for.
I would appreciate any help, or suggestions.
Regards,
Shup
Question about FFMPEG
On December 28th, 2007 Anonymous (not verified) says:
Hey! Just saw your question about ffmpeg and figured I'd help with what I could. I don't know about part 2, but I know that for duration you do ffmpeg -i InputFile -ss StartTime -t Duration Outputfile. So for example, if the video was 1 hour long, you would do ffmpeg -i InputFile -ss 00:10:00 -t 00:35:00 OutputFile. Hope that was helpful!
--Rob
Cygwin installation?
On May 17th, 2007 Ron Dotson (not verified) says:
Hi,
I was trying to install FFmpeg under Cygwin, but the compilation fails with many, many warnings and a fatal error - a missing header file to define the macro "llrint" as I recall. I copied over the standard.h file from somewhere on the Cygwin installation, but the next fatal error was (as I recall) a missing library routine or something - so obviously my environment or PATH variable aren't set up correctly.
Any pointers as to where I can find out what type of libraries are supposed to be found in what locations in order to get a successful compile and link of FFmpeg?
If so I would appreciate a reply to: linuxjournal@spamex.com
[Note: The email address listed above will self destruct at the first sign of Spam. ;-]
Thank you,
Ron
an easier program ?
On April 2nd, 2007 donate car (not verified) says:
I was wondering if there's a program with the basic transf. commands that can be used by those who haven't got experience with it.
Post new comment