TechEarl

How to Extract Audio From a Video with ffmpeg

Pull the audio out of a video with ffmpeg: copy the stream untouched when you just want to demux (fast, lossless), or re-encode to MP3, AAC, or WAV when you need a different format. Plus how to check the source codec first and grab a single segment.

Ishan Karunaratne⏱️ 7 min readUpdated
Share thisCopied
Extract audio from a video with ffmpeg by copying the stream untouched (fast, lossless) or re-encoding to MP3, AAC, or WAV when you need a different format.

The single decision that matters when you pull audio out of a video with ffmpeg is whether to copy the audio stream untouched or re-encode it. Copying is instant and lossless because ffmpeg just lifts the existing audio bytes out of the video container and writes them to a new one:

bash
ffmpeg -i input.mp4 -vn -c:a copy output.m4a

-vn drops the video. -c:a copy takes the audio stream as-is, no quality loss, no waiting. The catch: the output container has to support the codec you are copying. An MP4's audio is almost always AAC, and AAC lives happily in a .m4a (or .aac) container, so the command above works. Try to copy that same AAC stream into a .mp3 and ffmpeg refuses, because MP3 files only hold MPEG audio.

Re-encoding is the other path. You hand ffmpeg an encoder and a target format and it rebuilds the audio:

bash
# Re-encode to MP3 at a good VBR quality
ffmpeg -i input.mp4 -vn -c:a libmp3lame -q:a 2 output.mp3

Re-encode when you need a specific format the source is not already in (MP3 for an old player, WAV for an editor), or when you want to change the bitrate or sample rate. Copy when you just want the audio that is already there. The rest of this page is the detail behind that choice.

Re-encoding from a lossy source loses quality. If the video's audio is AAC (lossy) and you re-encode it to MP3 (also lossy), you decode and re-compress, and quality drops a little even at high settings. Copy the AAC out to .m4a instead and you keep the original bytes exactly. Only re-encode when you genuinely need the other format.

Check the source codec first with ffprobe

Before you decide whether copy will work, find out what codec the video's audio actually is. ffprobe (it ships with ffmpeg) prints it:

bash
ffprobe -v error -select_streams a -show_entries stream=index,codec_name,channels,sample_rate -of default=noprint_wrappers=1 input.mp4

That lists every audio stream with its codec, channel count, and sample rate, and nothing else. A typical MP4 reports codec_name=aac. Now you know:

  • AAC copies cleanly to .m4a or .aac.
  • Opus (common in WebM and modern files) copies to .opus, .ogg, or .mka.
  • MP3 copies straight to .mp3.
  • AC-3 / E-AC-3 (movie soundtracks) copies to .ac3 or .eac3.

If the codec matches a container you are happy with, copy. If not, re-encode. The error you get when copy and container disagree is explicit, something like Could not find tag for codec ... in stream, which is ffmpeg telling you that codec cannot live in that file type.

Copy the audio stream (fast, lossless)

This is the right default whenever the source format is acceptable. Match the extension to the codec ffprobe reported:

bash
# AAC audio out of an MP4, untouched
ffmpeg -i input.mp4 -vn -c:a copy output.m4a

# Opus audio out of a WebM, untouched
ffmpeg -i input.webm -vn -c:a copy output.opus

There is no quality knob here because nothing is being re-compressed. It finishes in the time it takes to read the file. -vn is doing the real work of "audio only"; -c:a copy is doing the "do not touch it" work.

Re-encode to a target format

When you need a format the source is not in, pick an encoder. For MP3, that is libmp3lame:

bash
# VBR, quality-targeted (recommended)
ffmpeg -i input.mp4 -vn -c:a libmp3lame -q:a 2 output.mp3

# CBR, fixed bitrate, if a device needs it
ffmpeg -i input.mp4 -vn -c:a libmp3lame -b:a 192k output.mp3

-q:a 2 is variable bitrate aimed at roughly 190 kbps and is the better default than a fixed rate: it spends bits where the audio needs them. The scale runs 0 (best, biggest) to 9 (worst); 2 is the standard "transparent enough for anyone" choice. Use -b:a 192k only when something genuinely requires a constant bitrate.

For other targets, swap the encoder and extension:

bash
# AAC into an .m4a (re-encode, e.g. from a non-AAC source)
ffmpeg -i input.mkv -vn -c:a aac -b:a 192k output.m4a

# Uncompressed WAV for an editor (large, lossless container)
ffmpeg -i input.mp4 -vn -c:a pcm_s16le output.wav

A note on format choice: MP3 is the universal-compatibility option and the one to reach for when you do not know what will play the file. M4A (AAC) and Opus are better quality per kilobyte, and if your source is already one of them, copying beats any re-encode. WAV is for editing, not distribution: it is uncompressed and huge.

Extract just a segment

You rarely need the whole soundtrack. -ss seeks to a start time and -t sets a duration (or -to sets an absolute end). Put -ss before -i so the seek is fast:

bash
# 30 seconds of audio starting at 1:05, copied untouched
ffmpeg -ss 00:01:05 -t 30 -i input.mp4 -vn -c:a copy clip.m4a

# Same window, re-encoded to MP3
ffmpeg -ss 00:01:05 -to 00:01:35 -i input.mp4 -vn -c:a libmp3lame -q:a 2 clip.mp3

With -c:a copy, the cut can only land on a packet boundary, so the start may be a fraction off. If you need it frame-tight, re-encode (drop the copy) and seek with -ss after -i instead.

Pull a specific track from a multi-track file

Some files (a Blu-ray rip, a film with a director's commentary) carry several audio streams. ffprobe numbers them from zero; -map picks one:

bash
# Take the second audio stream (0:a:1), copy it out
ffmpeg -i input.mkv -map 0:a:1 -c:a copy commentary.m4a

0:a:1 reads as "input 0, audio stream index 1". Run the ffprobe command from earlier to see the index of each track before you map it. Without -map, ffmpeg picks the default audio stream, which is usually the first one.

If ffmpeg complains there is no audio stream (Output file does not contain any stream), the video is silent, there is nothing to extract, and ffprobe will show zero audio streams to confirm it.

For the full set of conversion, trimming, and remux commands in one place, see the ffmpeg command cheat sheet.

FAQ

See also

Sources

Authoritative references this article was fact-checked against.

Tagsffmpegextract audiovideo to mp3demuxlibmp3lameAACCLIffprobe

Found this useful? Pass it on.

Copied

Ishan Karunaratne

Tech Architect · Software Engineer · AI/DevOps

Tech architect and software engineer with 20+ years building software, Linux systems, and DevOps infrastructure, and lately working AI into the stack. Currently Chief Technology Officer at a healthcare tech startup, which is where most of these field notes come from.

Keep reading

Related posts

How to Crop a Video with ffmpeg

Crop a video with ffmpeg's crop filter: crop=w:h:x:y from the top-left origin, centered crops with in_w/in_h expressions, square crops for social, and cropdetect to strip black bars automatically.

How to Trim or Cut a Video with ffmpeg

Trim or cut a video from the command line with ffmpeg: the fast lossless -c copy way, the keyframe gotcha that makes your cut land early, and the frame-accurate re-encode. With -ss and -to/-t explained.