Extract audio from a video with ffmpeg (2019)

Why this may be needed?

For example, you don’t know the name of a music artist who sounds in a video. Then, you can extract audio track from the video and upload it in some services to define music.

And what if the sound in the video in a good quality? Then we can extract this audio without sacrificing quality, without re-encoding.

This is a very useful trick, especially for Linux users. Having mastered this method once, we can use it at anytime we want. It doesn’t require any huge programs. ffmpeg libraries are used in many media players, so maybe you already have it installed on your Linux system as dependencies for applications.

Note that ffmpeg is available on many systems, including Windows.

Extract audio without loss of quality

How it works? Why there is no loss of quality? The fact is that this method simply copies the audio track from the video into a separate audio file. Therefore, there is no encoding here. This method is the fastest and highest in quality.

From .mp4 videos

In .mp4 video files audio track is often presented as AAC stream. So, the file extension of the output file can be .mp4, .m4a, .3gp.

The example below saves the audio track in .m4a format, but you can use all formats listed above

ffmpeg -hide_banner -i video.mp4 -vn -c:a copy audio.m4a

What do these parameters mean?

-hide_banner in simple terms means to hide the extra information

-i determines our input file

-vn means “no video”, so the video track will not be present in the output file

-c:a copy means to copy the audio track in its codec(without re-encoding)

audio.m4a is our output file(audio track)

From .webm videos

In .webm container audio stream can be presented in opus(more modern) or in vorbis codec. But they both can use .ogg container.

So, we can confidently use the following command:

ffmpeg -hide_banner -i video.webm -vn -c:a copy audio.ogg

This will save our audio track in .ogg format. This format is supported by all modern media players.

What if there are several audio tracks in a video?

We can choose which audio track we want to save. To do it, we need to add another option. It is -map option.

ffmpeg -hide_banner -i video.mp4 -map 0:a:1 -vn -c:a copy second_audio_track.mp4

What is -map 0:a:1 means?

The “0” (zero) means first file listed by -i option, so in our case it means “video.mp4” file.

Next, “a” means audio track. So, we choose between audio tracks in this file.

Next, “1” means second audio track(NOT FIRST!) since numbering starts from ZERO. So, if we want to choose third audio track then it must be -map 0:a:2!

How to show audio codec used in a video

We may encounter another codec that is used in a video, so it may use a different format. It may be important for the correct work of the media player.

Therefore, if we are unsure of the audio codec used in a video, use the following command

ffmpeg -hide_banner -i video.webm
...
Stream #0:0: Video: vp8, yuv420p(progressive), 1280x720, SAR 1:1 DAR 16:9, 25 fps, 25 tbr, 1k tbn, 1k tbc (default)
Stream #0:1: Audio: vorbis, 44100 Hz, stereo, fltp (default)

Like this! So our audio file is presented in vorbis codec, so we can use .ogg format for it!

Extract audio track to other formats with loss of quality

If you want to convert audio tracks from video to other formats, then be sure that the loss of quality in theory will be. But in reality, it may be insignificant, especially if we use large bitrates.

ffmpeg -hide_banner -i video.mp4 -vn -b:a 320K audio.mp3

This will make the audio track in mp3 format with 320 kbps bitrate.

Or we can choose the codec manually.

ffmpeg -hide_banner -i video.mp4 -vn -c:a libmp3lame -q:a 0 audio.mp3

It matters when a container supports several types of codecs. Then you need to specify yourself. Otherwise, ffmpeg will use the default codec for the specified container format (such as .mp4 -> aac, .webm -> opus, .mkv -> aac).

What is -q:a 0 means? It enable variable bitrate mode. So, bitrate will change dynamically. -q:a must be set from 0 to 6. In the case of the libmp3lame codec seems like the lowest value means the better quality and higher bitrate. But in the case of other codecs in may differ. For example, for vorbis codec the higher value means the higher bitrate. In my opinion, it is better to use -q:a option because it increases the quality and well distributes the bitrate across the track.

Show all available audio codecs for encoding

We can find what codecs are available, use following command

ffmpeg -hide_banner -encoders | grep "A\.\."
...
A..... aac                  AAC (Advanced Audio Coding)
A..... flac                 FLAC (Free Lossless Audio Codec)
A..... libopus              libopus Opus (codec opus)
A..... libmp3lame           libmp3lame MP3 (MPEG audio layer 3) (codec mp3)
A..... libvorbis            libvorbis (codec vorbis)
...

See this second column? These are the names of codecs for use in ffmpeg. We can pass these values to the -c:a option to specify our codec. Like that

ffmpeg some_options -c:a libopus some_options output_file

In addition, here is a simple table that will help you

Container specific audio formats
Table of supported audio formats in containers

Now that’s all. Thanks for attention. Ask questions in the comments.

Leave a Reply

Your email address will not be published.