‘ffmpeg’ is a powerful multimedia framework (a set of tools for handling multimedia file) that is being used by hundreds (if not thousands) of audio & video applications (editors, encoders, players, streaming apps … you name it!).
It supports multiple operating system platforms, and if you use GNU/Linux, then applications such as; VLC, MPLayer, OpenShot & Kdenlive (video editors) and even ‘Gstreamer’ (another multimedia framework) supports using ‘ffmpeg’ for playback (these are of course just a handful to mention).
Anyway, as an Ubuntu user, if you wanted extract the audio track of a multimedia file (without having to encode it) and okay with playing with the command-line a bit, then ‘ffmpeg’ can easily do that for you. Not just one multimedia format, but with ‘ffmpeg’ you can extract the audio track (s) of almost all popular multimedia container formats too!.
Let’s first install ‘ffmpeg’ on your Ubuntu computer. For that, open your Terminal and enter the below commands (one after the other).
This PPA (thanks to ‘Jon’) will give you the latest stable version of ‘ffmpeg’ and has packages for Ubuntu 12.10, 12.04, 11.10, 11.04 and 10.10 (as of writing this post).
sudo apt-add-repository ppa:jon-severinsson/ffmpeg
sudo apt-get update
sudo apt-get install ffmpeg
First things first …
Any multimedia file, though it seems like a single file (AVI, MKV, MP4 …), inside it, it holds each multimedia content (also called ‘streams’) separately. Take a movie with a subtitle for instance. The video, audio and the subtitle tracks are stored inside a single file (called the ‘container format’), separately.
That is why we can extract any of those tracks without having to re-encode them. And before we can extract an audio track of a multimedia file, there are two things that one has to figure out first.
*. Audio Codec -- We have to figure out the audio codec of the audio track inside the multimedia file (mentioned above).
*. Use proper output format -- This is the last and the crucial step. Because if you extract an OGG Vorbis audio file and it put it inside a MP3 container format (.mp3), then you will run into to troubles while playing (or ‘ffmpeg’ will give you an error and exit).
Luckily, once you have figured out the audio codec of the audio track, you should be able guess its proper container format using the audio codec (more below).
How to use it ? …
Below is a general use of ‘ffmpeg’ for analizing the input (source) file for getting the audio codec information.
ffmpeg -i input-file
Below is the command for extracting the audio track (you only have to change the colored text codes).
ffmpeg -i input-file -acodec: copy -vn output-file.extension
Let’s do an example.
Assuming that I have a multimedia file called ‘demo.avi’ and want to extract the audio file inside it, but because I don’t know its used audio codec, I’ll use the first command laid out above to find it.
ffmpeg -i demo.avi
This will give an output on the Terminal window. Then I’ll scroll down to find the field that displays the ‘Stream’ output info (usually near the end of the output) as it lists the Audio information (shown below).
As you can see in this example, it is a MP3 track. Therefore, as you well know, the output file’s should have the ‘.mp3′ extension. If it is anything other than MP3, and you don’t know what extension you should use as the output file, then simply use the codec name as the extension.
For example, if it’s the ‘aac’ codec then use the output.aac. If its the ‘wma’ then use output.wma. But there are rare occasions where this won’t work and you have to know the output format (such as with ‘vorbis’ codec, as you have to the ‘.ogg‘ extension).
Below is a simple list of few popular codec names and extension names that you should be using.
Codec Name Extension
Note: Whenever you are in doubt (don’t know what extension to use), then you can simply use the ‘.mka’ extension (‘output.mka‘). Because ‘MKA’ container format can store a huge number of audio codecs. If you choose that however, then some players might not be able to play the audio track. So please be aware of that.
Let me come back to the topic …
So as for this example, since the audio track of ‘demo.avi’ is actually a MP3 file, I can extract it into a MP3 file (‘new.mp3) by using the below command.
ffmpeg -i demo.avi -acodec: copy -vn new.mp3
When everything is finished, it will store the extracted audio in the same directory where the source file is located.
Dealing with multiple audio track …
If you have more than a single audio track, then you can also extract a selected one quite easily as well. And the only additional information that we need here is the track number of that particular audio track. Again, let me give you an example.
Let’s say that I have a MKV file called ‘demo-2′ that has two audio tracks (one ‘mp3′ and the other is ‘aac’). And let’s say that I only want to extract the ‘aac’ track. Then first, for finding out its track number I’ll use the same command as shown before.
ffmpeg -i demo-2.mkv
‘ffmpeg’ shows the first track as ‘Stream #0:0′, the second one ‘Stream #0:1′, third one ‘Stream #0:2′ etc. And according to the above ‘demo-2′ file’s output, the ‘aac’ track’s number is ’0:0′ (shown below), because it is the first track.
So to extract it to a file called ‘new.aac‘, I’ll use pretty much the same command as with extracting a single file, but will pass an additional argument to called ‘map’, followed by the track number of the audio track. Here is the command.
ffmpeg -i demo-2.mkv -map 0:0 -acodec: copy -vn new.aac
Again, the audio track will be extracted in the same location where the input file is located.
Below is the general command.
ffmpeg -i input-file -map track-number -acodec: copy -vn output-file.extension
Note: I’m sure ‘ffmpeg’ has nothing to do with it, but sometimes while extracting ‘vorbis’ (‘ogg’) files inside MKV files into an OGG (.ogg) format, ‘Gstreamer’ based players such as ‘Totem’ and ‘Rhythmbox’ fail to play it properly. However, other more powerful players such as MPLayer, VLC etc plays them without any issues.
As a fix, you can extract those ‘vorbis’ audio files into the above mentioned ‘.mka’ (audio container of ‘Matroska’ format).
Well, that’s pretty much it. Good luck.