Thursday, December 31, 2009

Converting video & audio files using ffmpeg in GNU/Linux

SkyHi @ Thursday, December 31, 2009

A few days ago I downloaded a video file (.flv) from a website. I wanted to convert the video from .flv to .avi (I know it’s not a free format, but I needed to). I searched over the Internet and found out about FFmpeg. FFmpeg is a command line tool used to convert multimedia files between formats. Not only it converts video files but it also converts audio files.

To make sure FFmpeg is already  installed on your GNU/Linux distribution, open the package manager you use and search for ffmpeg. Install it if it’s not already installed. I usually use the Konsole (Kubuntu’s Terminal), so I typed:

$ sudo apt-get install ffmpeg

Ok, now we have the program, but how does it work?

To begin with, open up a terminal. I understand that lots of people are afraid , bored or don’t like using it, but once you get the hang of it, you can perform many useful tasks.

The generic syntax is:

$ ffmpeg [[infile options][`-i' infile]]… {[outfile options] outfile}…

You have to have in mind that options that precede the infile are applied to the the infile and options that are between the infile and outfile are applied to the outfile.

In my case, I had a .flv file and I wanted to convert it to .avi. So the way to do it is:

$ ffmpeg -i infile.flv outfile.avi

Done! Simple as that! You will find the .avi file in the same folder!

Keep in mind that by default, FFmpeg tries to convert as losslessly as possible, attempting to keep the best quality for the output file. It uses the same audio and video parameters for the outputs as the one specified for the inputs.

If you want to see what formats and codecs are available on your PC  just type:

$ ffmpeg -formats

FFmpeg supports .3gp, .avi, .mp3, .mp4, .mvi, .rm , .mkv among many others.

  • Some video options that you might find useful are:

-b bitrate where bitrate is the video bitrate in bit/s. The default is  200kb/s

-r fps where fps is the frame rate in Hz. The default value is 25Hz.

-s size where size is the frame size. The default in FFmpeg is the same as the source. The format is ‘wxh’. wxh is equal to 160×128. You can use both either wxh or 160X128 (see video options link below for all sizes)

-aspect aspect where aspect is the aspect ratio with values 4:3, 16:9 or 1.3333 or 1.7777.

-vcodec codec where codec is the name of the video codec that you want the FFmpeg to use.

  • Some audio options that you might find useful:

-ar frequency where frequency is the audio sampling frequency. The default value is  41000KHz

-ab bitrate where bitrate is the audio bitrate in bit/s. The default is 64k.

-acodec codec where codec is the name of the audio codec that you want the FFmpeg to use.

More details can be found here:

  1. Main Options
  2. Audio Options
  3. Video Options

Following are some examples mentioned on ffmpeg documentation.

Example 1:

ffmpeg -i input.avi -b 64k output.avi

With the -b 64k option we set the outfile bitrate to 64kbit/s

Example 2:

ffmpeg -r 1 -i input.m2v -r 24 output.avi

Using the -r 1 we force the frame rate of the input file (valid for raw formats only) to 1 fps and with the -r 24 we set the the frame rate of the output file to 24 fps.

Example 3:

ffmpeg -i test.wav -acodec mp3 -ab 64k test.mp3

In this exampe we can see the use of FFmpeg for audio files. We convert a .wav file to .mp3. As said before using -acodec mp3 we force FFmpeg to use the mp3 audio codec to create the output file. The -ab 64k tells ffmpeg to use an audio bitrate of 64k.

Example 4:

ffmpeg -i video.avi -ar 22050 -ab 32 -s 640×480 video.mp4

Using the -ar we set the audio sampling rate to 22050 Hz. The -ab as i said sets the audio bitrate to 64k and the -s sets the frame size to 640×480. Here instead of 640×480 we could have used vga like this:

ffmpeg -i video.avi -ar 22050 -ab 32 -s vga video.mp4

Remember to see the video options link for all available sizes.

Conclusion: With FFmpeg you can manipulate and convert video and audio files between a variety of formats, fast and easy, with just a simple command. Remember not to hesitate to use the terminal!

6 Responses to “Converting video & audio files using ffmpeg in GNU/Linux”

  1. tuxhelper said:

    convert .flv to .mpg
    ffmpeg -i yourfile.flv -ab 56 -ar 22050 -b 500 -s 320×240 yourfile.mpg

    convert .3gp to .avi
    ffmpeg -i file.3gp -f avi -acodec mp3 *.avi

    extract audio .3gp to .mp3
    ffmpeg -y -i *.3gp -ac 1 -acodec mp3 -ar 22050 -f wav *.mp3

    Convert wma to ogg
    ffmpeg -i *.wma -acodec vorbis -aq 100 *.ogg

    Convert wma to mp3
    ffmpeg -i *wma -acodec mp3 -aq 100 *.mp3

    If you have a lot of files to convert run this command in the directory where your wma files reside
    convert wma to ogg
    for i in *.wma; do ffmpeg -i $i -acodec vorbis -aq 100 ${i%.wma}.ogg; done

    convert wma to mp3
    for i in *.wma; do ffmpeg -i $i -acodec mp3 -aq 100 ${i%.wma}.mp3; done