Skip to content
systemcoding.dev
Media

Extract Audio From a Video File With FFmpeg

Pull the audio track out of a video with FFmpeg, either by copying it unchanged or by converting it to MP3, AAC, Opus or WAV, with commands for each case.

6 min read
Summarize in AI
Flat illustration of video frames passing through a codec, with the FFmpeg, GStreamer and C logos.

You have a video and you want only the sound: a lecture to play on a phone, a song from a concert recording, a clean track for a podcast edit. FFmpeg does this in one command, and there are two ways to do it that give different results. One copies the audio exactly as stored. The other converts it to a new format. This post shows both on FFmpeg 7.1, how to find out which codec your file holds so you choose correctly, and how to deal with files that have more than one audio track.

First, look at what is in the file

The choice depends on the audio codec inside the video. Inspect it with ffprobe:

Shell
ffprobe -v error -select_streams a -show_entries stream=index,codec_name,channels,sample_rate -of csv=p=0 video.mp4
Text
1,aac,2,48000

Your numbers will differ. This file has one audio stream at index 1, codec aac, stereo, 48,000 Hz. -select_streams a picks audio streams only, and -of csv=p=0 prints plain comma-separated values without a label prefix.

Option one: copy the audio unchanged

If the audio is AAC and you want an AAC file, copy it:

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

The options mean:

  • -vn drops all video streams, so the output has audio only.

  • -c:a copy copies the compressed audio packets without decoding them.

  • audio.m4a is the container. M4A is MP4 with audio only, and it holds AAC.

This is instant and lossless, because nothing is re-encoded. The restriction is that the container must accept the codec. AAC fits in .m4a and .mp4. It does not fit in .mp3 or .wav. If the codec is Opus, use .opus or .ogg. If it is MP3, use .mp3.

Here is a small table for the common cases:

Codec in the videoCopy to this extension
aac.m4a (or .aac raw)
mp3.mp3
opus.opus or .ogg
vorbis.ogg
ac3.ac3
flac.flac

If you give copy a container that cannot hold the codec, FFmpeg reports an error about the codec not being supported in that output format, and you either change the extension or convert.

Option two: convert to another format

To get an MP3, you must encode, because the audio in a video is rarely already MP3:

Shell
ffmpeg -i video.mp4 -vn -c:a libmp3lame -q:a 2 audio.mp3

libmp3lame is the MP3 encoder, and it must be present in your build; check with ffmpeg -hide_banner -encoders | grep mp3lame. The -q:a 2 setting is variable bit rate quality on a scale from 0 (best) to 9 (worst). The FFmpeg MP3 encoding guide gives level 2 an average of about 190 kb/s, which is a good default for music. For a fixed rate, use -b:a 192k in place of -q:a.

More conversions you will use:

Shell
ffmpeg -i video.mp4 -vn -c:a aac -b:a 160k audio.m4a
ffmpeg -i video.mp4 -vn -c:a libopus -b:a 96k audio.opus
ffmpeg -i video.mp4 -vn -c:a pcm_s16le -ar 44100 audio.wav
ffmpeg -i video.mp4 -vn -c:a flac audio.flac

The first re-encodes to AAC at 160 kb/s. The second makes Opus at 96 kb/s, which is excellent for speech and fine for music at that rate. The third writes uncompressed 16-bit WAV at 44,100 Hz, where -ar sets the sample rate. The fourth writes lossless FLAC, which is smaller than WAV with identical audio.

Converting from a lossy source such as AAC to another lossy format such as MP3 stacks two rounds of loss. For archives, copy or use FLAC or WAV, and accept the larger file. For listening on a phone, a high bit rate in the new format is fine.

What the run looks like

For the MP3 command, FFmpeg shows:

Text
Stream mapping:
  Stream #0:1 -> #0:0 (aac (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
size=    3481KiB time=00:02:14.40 bitrate= 212.2kbits/s speed=96.4x

The mapping line says the input audio stream (#0:1) became the only output stream (#0:0). The numbers differ for your file. Since video is skipped, the speed is very high.

Several audio tracks

Films and recordings often hold more than one track, such as two languages. Without guidance, FFmpeg picks the one with the most channels. List the audio streams, then choose:

Shell
ffprobe -v error -select_streams a -show_entries stream=index:stream_tags=language -of csv=p=0 movie.mkv
Text
1,eng
2,spa

To extract the Spanish track, which is the second audio stream, use a stream specifier with -map:

Shell
ffmpeg -i movie.mkv -map 0:a:1 -c:a copy spanish.m4a

0:a:1 means input 0, audio streams, the stream at audio position 1. Audio positions count from zero, so position 1 is the second audio stream, and it need not be the same as the stream index shown by ffprobe (here that is 2). Mixing the two numbering schemes is the most common mistake. The map post in this cluster explains specifiers in full.

To save every audio track to its own file, run one command per -map, or map several outputs in one command:

Shell
ffmpeg -i movie.mkv -map 0:a:0 -c:a copy eng.m4a -map 0:a:1 -c:a copy spa.m4a

Extract only part of the audio

Combine the trim options from the earlier post:

Shell
ffmpeg -ss 90 -i video.mp4 -t 60 -vn -c:a libmp3lame -q:a 2 clip.mp3

That writes one minute of audio starting at 1:30. Because audio frames are short, even -c:a copy cuts close to the exact point.

Common problems

  • "Output file does not contain any stream": the input has no audio. Check with ffprobe.

  • A huge WAV: uncompressed PCM at 44,100 Hz stereo 16-bit takes about 10 MB per minute. Use FLAC if size matters and quality must stay exact.

  • Wrong extension for copy: FFmpeg's error says the codec is not supported in the container. Pick an extension from the table above.

  • Silence: a file with a 5.1 track may sound quiet if you downmix without care. Add -ac 2 to convert to stereo; FFmpeg's default downmix handles the channel mix for you.

We keep the copy approach for archival work and the MP3 or Opus approach for anything a person will listen to on the move. Re-encoding a track we only wanted to store ended up as a lesson the first time, when a transcript service rejected a lossy-on-lossy file that a plain copy would have passed.

Normalizing and checking what you extracted

Once you have the file, listen to the start and end, then check the length. A copy from a video with an edit list can begin a few milliseconds off, and ffprobe will report a duration that differs from the video's by that amount. This is normal and inaudible.

If the extracted audio is quiet, do not boost it with a plain gain until you have measured it. The volumedetect filter reports the mean and maximum level without writing a file:

Shell
ffmpeg -i video.mp4 -vn -af volumedetect -f null -

The report lists mean_volume and max_volume in dB. A max_volume of 0.0 dB means the track already touches full scale, and more gain would clip it. A max_volume of -12 dB leaves room. The loudness normalization post in this cluster uses the loudnorm filter, which is the better tool for matching the level of several files.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.