FFmpeg: How to Extract Audio From a Video File

ffmpeg logo and soundwave icon

In this article we’ll show you how to use FFmpeg to extract raw audio from a video file, and also how to re-encode audio using different ffmpeg audio encoders.

What is FFmpeg?

In our daily computing life, we make use of software and programs without realizing that we are using a particular different program used by our main program.

One very good example of this description is FFMPEG. This tool powers many widespread media-based applications, such as: iTunes, Youtube and the famous video player VLC.

FFMPEG is a very fast tool used for converting and streaming video and audio, and also used in grabbing from live video or audio sources. FFMPEG is actually a command line utility (CLI) used mainly by software developers and media industry professionals, to perform operations on media files such as format conversion, encoding, resizing, concatenation, and compression.

This project has a lot of useful features, one of them is extracting audio from video ;we all have been in that situation when we have some video file, but we only need its audio (for example : a video clip) . That’s exactly what we will be discussing in the next paragraphs

Basic FFmepg commands consist of four elements:

ffmpeg [Input File] [Flags/Actions] [Output File]

How to Install FFMPEG

To install FFMPEG project, we first go to our terminal, and type the following update command to download package information from all configured sources:

sudo apt update

After making sure that the first command got executed correctly, Type the below given command and hit Enter to install the project:

sudo apt install ffmpeg

After executing the preceding command. FFMPEG will get installed onto our system, and also add the ffmpeg binary to the path variable. So now we can use the ffmpeg command in the command line.

One final step left is to verify that FFMPEG is actually installed. Let’s type ffmpeg to check the ffmpeg configuration.

ffmpeg
Output
ffmpeg version 4.4.2-0ubuntu0.22.04.1 Copyright (c) 2000-2021 the FFmpeg developers
Note: Another way to display the version of ffmpeg, is by adding the -version command line switch.

Getting Started with FFMPEG

For this tutorial we are going to work on a short clip from a cartoon series. Let’s start by downloading the MPEG-4 media file (link: https://ia802304.us.archive.org/28/items/cn_episodes/%20k.mp4) by following the below given command:

wget https://ia802304.us.archive.org/28/items/cn_episodes/%20k.mp4
Output
--2022-06-11 11:51:03--  https://ia802304.us.archive.org/28/items/cn_episodes/%20k.mp4
Resolving ia802304.us.archive.org (ia802304.us.archive.org)... 207.241.228.44
Connecting to ia802304.us.archive.org (ia802304.us.archive.org)|207.241.228.44|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4346697 (4.1M) [video/mp4]
Saving to: ' k.mp4'

Change the file name to video.mp4. Check the below command:

mv ' k.mp4' video.mp4
ls
Output
video.mp4

Checking Media Files Information

It’s always recommended to check and familiarize ourselves with basic metadata about video files. We can achieve this by using the -i command line argument without specifying an output.

ffmpeg -i video.mp4
Output
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 1
    compatible_brands: isomavc1
    creation_time   : 2012-09-04T13:47:56.000000Z
  Duration: 00:01:54.99, start: 0.000000, bitrate: 302 kb/s
  Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 720x576 [SAR 1:1 DAR 5:4], 269 kb/s, 25 fps, 25 tbr, 100 tbn, 50 tbc (default)
    Metadata:
      creation_time   : 2012-09-04T13:47:56.000000Z
      vendor_id       : [0][0][0][0]
  Stream #0:1(und): Audio: aac (HE-AAC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 30 kb/s (default)
    Metadata:
      creation_time   : 2012-09-04T13:47:57.000000Z
      handler_name    : GPAC ISO Audio Handler
      vendor_id       : [0][0][0][0]

Note: We can achieve similar results by using ffprobe which is one of FFMPEG main executable, this command is used for getting the characteristics of media files containers and streams. Follow the below command:

ffprobe video.mp4

After we’ve checked the media file information, notice that the audio stream format is aac, which will be our container format for the final file

Note: As with any command line interface, the user should specify the path of input or output files depending on their working directories. In the examples given in this article, it is assumed that the user has navigated to the working directory to execute the FFMPEG commands.

Extracting Audio from Video

There are two common operations and very useful ways so we can extract audio from an input video file. We achieve this by extracting raw audio (without re-encoding it), or by re-encoding it.

Note: In FFMPEG commands, any file not preceded by the -i command line switch is treated as an output file. ffmpeg uses as many inputs and outputs as you provide. We can also specify the output file name using the -y command line option to overwrite output files without asking for conformation.

Extracting an Audio Out of a Video File Without Re-encoding

We can understand this part as removing only the video and leaving out the audio. To do that we can use the -vn command line flag to remove the video, then copy the result (audio) from the source to the destination folder without re-encoding it, using the -acodec copy command switch. Let’s check the below mentioned command for better understanding.

ffmpeg -i video.mp4 -vn -acodec copy audio.aac

Note: If we executed the ls command utility we can notice that our extracted audio file is under the name audio.acc as we specified it in proceeding command.

ls
Output
audio.aac video.mp4

We can also use the -map flag to select all audio streams or to select a specific part of the audio.

Extracting an Audio Out of a Video File With Re-encoding

FFMPEG is able to encode to a wide variety of audio formats. The following table list some lossy audio compression formats with their appropriate encoders that FFMPEG can use:

  • Dolby Digital : ac3
  • Dolby Digital Plus : eac3
  • TrueHD 0xFBA : truehd
  • MP2 : libtwolame, mp2
  • Windows Media Audio 1 : wmav1
  • Windows Media Audio 2 : wmav2
  • AAC LC : libfdk_aac, aac
  • HE-AAC : libfdk_aac
  • Vorbis : libvorbis, vorbis
  • MP3 : libmp3lame, libshine
  • Opus : libopus

We can choose the encoder based on the produced quality by checking the following list:

libopus > libvorbis >= libfdk_aac > libmp3lame >= eac3/ac3 > aac > libtwolame > vorbis > mp2 > wmav2/wmav1

Note: The greater than or equal to symbol (>=) is used to represent: better or same quality.

In case we didn’t prefer the audio container format or we want to compress it, we simply choose our favorite encoder Based on the quality produced that FFMPEG can use. To achieve that, we will have to set the encoder after the -acodec audio option. Check the following for better understanding.

ffmpeg -i video.mp4 -acodec libshine audio2.mp3
Output
. . .
Output #0, mp3, to 'audio2.mp3':
  Metadata:
    major_brand     : isom
    minor_version   : 1
    compatible_brands: isomavc1
    TSSE            : Lavf58.76.100
  Stream #0:0(und): Audio: mp3, 48000 Hz, stereo, s16p, 128 kb/s (default)
    Metadata:
      creation_time   : 2012-09-04T13:47:57.000000Z
      handler_name    : GPAC ISO Audio Handler
      vendor_id       : [0][0][0][0]
      encoder         : Lavc58.134.100 libshine
size=    1798kB time=00:01:54.98 bitrate= 128.1kbits/s speed=12.2x
video:0kB audio:1797kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.028368%

To control the quality of the audio output, we have to add the -aq q (-q:a) to set the audio quality (0 to 6), 0 being high quality and 6 being low quality. Check the below given command to understand.

ffmpeg -i video.mp4 -acodec libshine -q:a 0

Note: We set the -q:a to 0 for the highest quality possible.

Conclusion

In this how to article, we’ve walked you through the different steps of extracting audio from video files, and also we showed you how to use ffmpeg audio encoders to get a different container format with a better or lower quality. We hope these commands may come in handy for you.

 

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
Bash Source Command
Read More

Bash Source Command

The Bash source command reads and executes commands from a specific file as an argument within the current…
Bash Append to File
Read More

Bash Append to File

There are various ways to append text to a file in bash. In computing, append means to add…