May 29

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!

Share

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

  1. tuxhelper says:

    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

  2. Surja says:

    Thanks for this useful guide. Helpful indeed :)

  3. simon says:

    Thanks for this useful tutorial

  4. Gregory Maxwell says:

    You really don’t want to use “-acodec vorbis”, instead you should use “-acodec libvorbis”. The first uses the ffmpeg internal vorbis encoder, which currently delivers very poor quality.

  5. Rob says:

    I’ve never dealt much with ffmpeg from the command line, I usually opt for WinFF which is a GUI frontend for ffmpeg.

  6. g0d4 says:

    Thank you guys for the useful additions and tips!
    WinFF seems very simple and practical btw. I will have a look at it!

  7. ross chilvers says:

    will ffmpeg recognise input files *.mpg4

  8. g0d4 says:

    Judging from the supported formats and codecs listed here: http://en.wikipedia.org/wiki/Ffmpeg#Codecs.2C_formats_and_protocols_supported
    it shouldn’t have any problem handling the mpeg4 format.

  9. Roman says:

    I can’t convert flv to 3gp. Can you help me?

  10. g0d4 says:

    @Roman
    Can you paste the command you are using and the error you get?

  11. Pete says:

    @tuxhelper

    Could you clarify what you’re doing that makes ffmpeg effect all files in the folder?

    for i in *.wma; do ffmpeg -i $i -acodec vorbis -aq 100 ${i%.wma}.ogg; done

    I understand the ffmpeg -i $i -acodec vorbis -aq 100 part, but how to the other parts of this work?

  12. Pete says:

    @tuxhelper

    After playing with that a bit, I have a few thoughts to share. My main goal is to take flv files that I’ve ‘borrowed’ from youtube with a stream ripper and convert them to mp3 so I can make mix cd’s and show off for my friends at work. I am a beginner linux user, and the only reason why I’ve learned anything is because linux tends to provide you with plenty of brick walls. For this, I love it.

    First, if any of the file names have spaces, it won’t work (or it didn’t for me).

    Next, if I’ve interpreted this correctly, which I think I have since I used it to convert three flv files to mp3 as a test, than this should be a little clearer for command line newbies like myself to understand.

    for i in *.INPUT; do ffmpeg -i $i OTHER ${i%.INPUT}.OUTPUT; done

    INPUT represents the file type you start with. OUTPUT represents the file type you want to convert it to, and OTHER represents any additional commands you want to tell ffmpeg to do along the way.

    For example, I tested this by taking 3 random flv files and dropping them into a folder. Since they all had long screwey file names, I renamed them 1.flv, 2.flv and 3.flv. Then I ran this adapted from your suggestion:

    for i in *.flv; do ffmpeg -i $i -ab 128k ${i%.wma}.mp3; done

    The option in the middle (-ab 128k) isn’t really needed, but since the default quality is 64k, I decided to up that to something that sounds less like getting a call from someone in a crowded bar and more like music.

    The result is that the 3 flv files in the working directory were converted to mp3 at 128k. Obviously in this case, the video was discarded. The original files are intact, and the new files created are named 1.flv.mp3 etc.

    So ripping you line apart,

    for i in *.wma; do ffmpeg -i $i -acodec vorbis -aq 100 ${i%.wma}.ogg; done

    [for i in *.wma; do ffmpeg -i $i] tells the system to perform the ffmpeg command on all *.wma files in the working directory. [-acodec vorbis -aq 100] Are the particular options for ffmpeg to use. In your case, you select the codec and I think -aq 100 means audio quality 100% but I’m not sure. In my example, I just chaged the bitrate. [${i?.wma}.ogg tells the system how to name the new files. Poop.wma will become Poop.wma.ogg.

    It took a little playing around to get you info to work for ripping audio out of flv files, but that’s mainly because I don’t really know much about the command line yet. Again, the one thing that really snagged up the process was the filenames

    If there are any spaces in the filenames, it seems like it won’t work.

    Thanks again for the help, and I hope my experience with this may help someone else.

  13. g0d4 says:

    @Pete

    Am glad this post could be of help to you. Am even gladder that you shared your experience and added all these useful additions to it.

    thank you for your comment!

  14. Alex says:

    A workaround is to replace spaces in names by another character. Here is a simple command to replace spaces by (for instance) underscores:

    for i in *.wma; do mv “$i” `echo $i | tr ‘ ‘ ‘_’`; done

    Cheers,
    A.

  15. Pete says:

    Another after thought. Since youtube puts everything up at 64k, by setting ffmpeg to 128k does nothing but make the files twice the size.

  16. lunku says:

    To fix the problem with spaces in the filenames, you can quote the names, as follows:
    for i in *.wma; do ffmpeg -i “$i” -acodec vorbis -aq 100 “${i%.wma}.ogg”; done

  17. Yura says:

    hi,
    just downloaded the latest version of ffmpeg, but when i’m trying convert flv to mp3 i have “unknown encoder mp3″ in result???

  18. tetris4 says:

    @Yura

    Hi, try using “-acodec libmp3lame” instead of “-acodec mp3″ and see if it works.

  19. khan says:

    hi i am using ffmpeg for conversion .
    file wont be stored if their is any white space in folder
    like New folder
    let me know if u have solution for this

    Thanks

  20. g0d4 says:

    @khan
    Sorry for running a bit late, vacations =)
    AFAIK the white space “bug” is a known situation in the bash world. I think the best possible solution is to just avoid naming folders like that.
    Sorry for not being able to help more on this one. Don’t know if there is any specific workaround for ffmpeg, maybe someone else can help.

  21. Sen says:

    Is it possible to use ffmpeg to convert a standard video file to high definition video file?

    Also will there be changes in commaond line commands for windows and linux. if yes, could you please lets know what will be command for linux for converting any kinda video file to flv.

    Currenlty we have the command as below, but its working in windows and not working in linux. Also if we add other video options and audio options its not working, but this command converts mp4 formats to flv in linux server.

    ffmpeg -i uploads/videos/$oname1 -sameq uploads/live/$flv_filename

    please help us……….

  22. NutMotion says:

    Thks. Very helpful.

    Also re Tetris (old comment):
    “Hi, try using “-acodec libmp3lame” instead of “-acodec mp3″ and see if it works.”

    Thks to you too. I can confirm it solved this same “Unknown encoder ‘mp3′” problem for me.

  23. tetris4 says:

    @Sen
    Care to explain what do you mean by “convert a standard video file to high definition”.
    The command to convert a file to flv is exactly the same as above, just reverse the order:
    $ ffmpeg -i infile.avi outfile.flv
    Just run it in the folder where infile.avi is placed and you will be ok, as stated in the article.
    As for windows, I can’t be of much help..

    @NutMotion
    You are welcome! Glad it helped! :)

  24. Lenildo Barboza says:

    To avoid the spaces problem, try this:
    for i in “*.wma”; do ffmpeg -i $i -aq 100 “${i%.wma}.mp3″; done

  25. sumitavo biswas says:

    Hi friends, i am facing a issue..i have a requirement to convert a .3gp file to .wav file. We have to use ffmpeg for the same, but currently the transcoded file(.wav file) do not play, it is of 0 bytes…can anyone help me how to do it? Following is the stacktrace

    $/opt/csw/bin/ffmpeg -i sound2.3gp -acodec pcm_mulaw -ar 8000 -ac 1 -f wav 3gp.wav
    FFmpeg version SVN-r21156-snapshot, Copyright (c) 2000-2010 Fabrice Bellard, et al.
    built on Feb 12 2010 01:57:40 with gcc 4.3.4
    configuration: –prefix=/opt/csw –enable-shared –disable-static –enable-ffplay –enable-runtime-cpudetect –enable-libtheora –enable-libvorbis –enable-libx264 –enable-libxvid –enable-libmp3lame –enable-gpl –enable-postproc –enable-avfilter –enable-avfilter-lavf –bindir=/opt/csw/bin/sparcv8 –libdir=/opt/csw/lib –incdir=/opt/csw/include
    libavutil 50. 7. 0 / 50. 7. 0
    libavcodec 52.47. 0 / 52.47. 0
    libavformat 52.46. 0 / 52.46. 0
    libavdevice 52. 2. 0 / 52. 2. 0
    libavfilter 1.15. 0 / 1.15. 0
    libswscale 0. 8. 0 / 0. 8. 0
    libpostproc 51. 2. 0 / 51. 2. 0
    [amr @ 442c0]max_analyze_duration reached
    [amr @ 442c0]Estimating duration from bitrate, this may be inaccurate
    Input #0, amr, from ‘sound2.3gp':
    Duration: N/A, bitrate: N/A
    Stream #0.0: Audio: samr / 0x726D6173, 8000 Hz, 1 channels
    File ‘3gp.wav’ already exists. Overwrite ? [y/N] y
    Output #0, wav, to ‘3gp.wav':
    Stream #0.0: Audio: pcm_mulaw, 8000 Hz, 1 channels, s16, 64 kb/s
    Stream mapping:
    Stream #0.0 -> #0.0
    Decoder (codec id 73728) not found for input stream #0.

  26. sachin says:

    Hi guys,
    i’ve a issue. i’ve version ( [0] => FFmpeg 0.6.1 [1] => libavutil 50.15. 1 / 50.15. 1 [2] => libavcodec 52.72. 2 / 52.72. 2 [3] => libavformat 52.64. 2 / 52.64. 2 [4] => libavdevice 52. 2. 0 / 52. 2. 0 [5] => libavfilter 1.19. 0 / 1.19. 0 [6] => libswscale 0.11. 0 / 0.11. 0 [7] => libpostproc 51. 2. 0 / 51. 2. 0 ) but this is not convering .avi to .ogg file.

    what should i do?

  27. tetris4 says:

    @sumitavo biswas
    Why are you using that specific “pcm_mulaw” codec? Looks like your issue is there.

    @sachin
    Can’t understand much of this ouput, please share the terminal command you are using and it’s output.

  28. Victor says:

    Hey guys,
    I want to extract 30 secs out of a 5 min audio file. How can I do this with ffmpeg?

    Thanks in advance

  29. g0d4 says:

    Try using this command

    ffmpeg -ss 0 -t 30 -i file.mp3 file.wav

    where the arguments are

    -ss 0 – Start at 0 seconds
    -t 30 – Capture 30 seconds (from 0, so 0:00 – 0:30).
    file.mp3 – Input file
    file.wav – output file

  30. Victor says:

    Thanks @g0d4! Works like a charm

  31. g0d4 says:

    @Victor
    Glad it helped! :)

  32. Appalbarry says:

    Ok, Converted three mp4 files to AVI – easy enough from the command line.

    BUT the resulting AVI is heavily pixelated.

    The mp4 properties says:
    Dimensions: 624 x 352
    Codec: H.264 / AVC
    Framerate: 24 frames per second
    Bitrate: 1067 kbps

    Any suggestions?

    Great resource otherwise.

  33. g0d4 says:

    I did not face anything like this before.. What is the command that you are using? You can also take a look here: http://www.linuxquestions.org/questions/linux-software-2/how-to-reduce-improve-pixelated-result-from-ffmpeg-648971/

  34. Maneesh.B says:

    Dear ubuntu Developers,
    When i a play this commands in my computer
    for conversion, 3gp format is not working. because a lot of mobile phone does not support with the wrong bit rate.
    How can we knew the correct bit rate and how to a play those.

  35. Sadaf Chohan says:

    hello guys
    i am trying to convert the video to .ogg format and the command am using is
    ffmpeg -i *.wma -acodec vorbis -aq 100 *.ogg
    i have following configuration over my VPS server
    ffmpeg
    ffmpeg-php version 0.6.0-svn
    ffmpeg-php built on Oct 4 2012 04:36:57
    ffmpeg-php gd support enabled
    ffmpeg libavcodec version Lavc52.35.0
    ffmpeg libavformat version Lavf52.38.0
    ffmpeg swscaler version SwS0.7.1
    on executing command the file generated but of 0 bytes … any suggestions ?

  36. Tarik Hadi says:

    Thank you the very relevant and helpful guide. I’m using ffmpeg on my Mac Terminal and have been to do some incredible video and audio stuff with it.

  37. Tan says:

    is it possible to change mime-type: application/ogg to audio/ogg with ffmpeg?

  38. tetris4 says:

    @Sadaf Chohan
    Did you try using another codec or another format, to check if it’s a general ffmpeg problem?

    @Tarik Hadi
    Thank you for your kind words.

    @Tan
    Am not sure what you are trying to achieve, but maybe this helps: http://stackoverflow.com/questions/10985953/mime-type-for-transcoded-stream

  39. felface says:

    none of this works for me cos anything i try to put in it just says -bash: ffmpeg:command not found

  40. tetris4 says:

    @felface
    You first need to install ffmpeg from your distribution’s repositories.

  41. Felface says:

    I have downloaded and opened the file in the mac terminal what next

  42. tetris4 says:

    Oh you are running this on a Mac system? Am not able to offer help on that cause I have very little experience there. Maybe you could try searching on the web about installing ffmpeg on OSX?

  43. jesper says:

    i have no idea what to do, i am new to linux, and dont really understand commands that well, i have tried to put in the command but it keeps saying Unable to find a suitable output format for ‘pipe:’ and i dont know what that means

  44. tetris4 says:

    @jesper
    can you provide a paste of the command you are using, including the error output you get?

  45. masaad says:

    flv to 3gp:-
    ffmpeg -i input.flv -s qcif -vcodec h263 -r 10 -b 180 -qscale 0 -ab 64 -acodec libfaac -ac 1 -ar 22050 output.3gp

  46. Yanto says:

    @masaad: Thanks for the command. I need to convert .flv to .3gp format.

    I tried this command and success:

    ffmpeg -i input.flv -s qcif -vcodec h263 -r 10 -b 180k -qscale 0.1 -ab 64 -acodec libfaac -ac 1 -ar 22050 output.3gp

    -b needs to be in [value]k format and -qscale has to be more than 0.0.

  47. Chrianjeevi says:

    hi all

    I am converting files from any format to mp4, following are some of the preferred formats ( flv,webm,wmv,avi,m4v ) which i am using, and i tried many of the codec/linux commands with different ffmpeg linux version but failed. please do the needful help

  48. tetris4 says:

    Hello Chrianjeevi, one would need to know the errors you are getting to try to help more.

Leave a Reply

preload preload preload