I use ffmpeg to convert between audio and video formats. Two situations I use it for are: (1) when I have a video whose audio codec is of a format my media players can’t handle (I use WD TV Live and it has trouble with certain formats), or (2) when I want to extract the audio only from a video.
For converting only the audio here’s the command I use:
1 |
c:\> ffmpeg -i "input file.xxx" -vcodec copy -acodec libmp3lame "output file.xxx" |
The syntax is pretty obvious. The input file is taken, the video codec is copied as-is to the new file, the audio codec is re-encoded to mp3. I could have used -acodec mp3
too. In the past there used to be an in-built mp3 encoder as well as the mp3 encoder provided by the LAME project (ffmpeg must be built with LAME encoder via the --enable-libmp3lame
switch for this to work) so you could choose between either via the two switches, but now there’s no in-built encoder so both --acodec mp3
and --acodec libmp3lame
do the same.
To confirm what switches ffmpeg was built with simply run the command. For example, on my machine:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
c:\> ffmpeg ffmpeg version N-68788-g37b35fe Copyright (c) 2000-2014 the FFmpeg developers built on Dec 30 2014 22:02:25 with gcc 4.9.2 (GCC) configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnu tls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable- libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-l ibrtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc -- enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --ena ble-libxvid --enable-lzma --enable-decklink --enable-zlib libavutil 54. 15.100 / 54. 15.100 libavcodec 56. 19.100 / 56. 19.100 libavformat 56. 16.102 / 56. 16.102 libavdevice 56. 3.100 / 56. 3.100 libavfilter 5. 6.100 / 5. 6.100 libswscale 3. 1.101 / 3. 1.101 libswresample 1. 1.100 / 1. 1.100 libpostproc 53. 3.100 / 53. 3.100 Hyper fast Audio and Video encoder usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}... |
Notice it was built with --enable-libmp3lame
.
When it comes to extracting just the audio from a video there’s two ways to do it: 1) you can simply extract the audio in the codec as it is, or 2) you can extract & convert to the codec you want. The latter has the disadvantage that if the original video is in a lossy audio codec, converting will result in some degradation of quality.
To check what audio codec the file is in, do the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
c:\> ffmpeg -i "input file.m4a" ffmpeg version N-68788-g37b35fe Copyright (c) 2000-2014 the FFmpeg developers built on Dec 30 2014 22:02:25 with gcc 4.9.2 (GCC) configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnu tls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable- libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-l ibrtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc -- enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --ena ble-libxvid --enable-lzma --enable-decklink --enable-zlib libavutil 54. 15.100 / 54. 15.100 libavcodec 56. 19.100 / 56. 19.100 libavformat 56. 16.102 / 56. 16.102 libavdevice 56. 3.100 / 56. 3.100 libavfilter 5. 6.100 / 5. 6.100 libswscale 3. 1.101 / 3. 1.101 libswresample 1. 1.100 / 1. 1.100 libpostproc 53. 3.100 / 53. 3.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input file.m4a': Metadata: major_brand : dash minor_version : 0 compatible_brands: iso6mp41 creation_time : 2013-06-28 19:39:02 Duration: 00:04:22.08, start: 0.000000, bitrate: 255 kb/s Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 253 kb/s (default) Metadata: creation_time : 2013-06-28 19:39:02 handler_name : SoundHandler At least one output file must be specified |
Notice it identifies the audio stream as AAC in this case. If I am happy with extracting that as it is I can do the following:
1 |
c:\> ffmpeg -i "input file.m4a" -vn -acodec copy "output.aac" |
The -vn
switch tells ffmpeg to ignore the video. The -acodec copy
switch tells it to copy the audio codec as it is. Since this is an AAC file, I assign an extension of .aac
to the output file.
However, if I didn’t want the audio stream as AAC, I would have done the following:
1 |
c:\> ffmpeg -i "input file.m4a" -vn -ac 2 -ar 44100 -ab 320k -acodec libmp3lame "output.mp3" |
Here I am converting the audio to mp3. Once again I ignore the video via -vn
. I specify the audio codec via -acodec libmp3lame
. The rest of the switches are as follows:
-ac 2
=> two channels (stereo) (note this is same as the input, so is an optional switch)-ar 44100
=> sammple rate 44100 Hz (CD quality) (note this is same as the input, so is an optional switch)-ab 320k
=> bit-rate of 320 kb/s (if I don’t specify this the bit-rate will be 128 kb/s for mp3)
Essentially, instead of just copying the audio codec you convert it. Otherwise the idea is the same. Apart from libmp3lame
(or mp3
) I could have also used the following audio codecs: vorbis
(for ogg), aac
, flac
, and wma
.
Before concluding, here’s a link to ffmpeg’s documentation (for all the command-line switches etc). Also, this is a good page on audio/ video containers and ffmpeg. The latter is a very thorough and informative page, I am sure I’ll be referring to it in the future.