Extract audio from a video with FFmpeg

Add -vn to drop the video and -c:a copy to keep the audio exactly as it is, in a file type that fits its codec (.m4a for the AAC inside most MP4 files). Re-encode with libmp3lame when something needs MP3. Every command below ran on the same 2-minute clip.

Copy Stream copy

Lossless and instant. The extension has to match the codec, AAC in most MP4 files.

ffmpeg -i in.mp4 -vn -c:a copy out.m4a

120.0 s of AAC, no video. The audio packets are the source's, byte for byte.

Audio
AAC 165 kb/s, packets unchanged
Run time
80 ms
Output
2.5 MB
MP3 Re-encode

For players and tools that only take MP3. Re-encodes, so it is a second lossy copy.

ffmpeg -i in.mp4 -vn -c:a libmp3lame -q:a 2 out.mp3

MP3 at 169 kb/s average, 48 kHz stereo. Slightly larger than the copied AAC.

Bitrate
169 kb/s VBR, 48 kHz
Run time
923 ms
Output
2.5 MB

Proof

What came out

Waveforms drawn from the output files. The film mix is quiet, so the peaks sit well below full scale. One lane per channel.
-vn -c:a copy out.m4a Same audio, no video
Stereo waveform of out.m4a, two lanes, 2 minutes of dialogue and music
2.5 MB Both channels, 120.0 s, the source's AAC packets unchanged.
-map 0:a:0 First track, stereo AAC
Waveform of the first audio track of tracks.mkv, two lanes for stereo
2 channels Audio tracks count from 0, so 0:a:0 is the first one.
-map 0:a:1 Second track, mono AC-3
Waveform of the second audio track of tracks.mkv, one lane for mono
1 channel One lane: the mono mix. The channel count tells you which track you got.

Footage: Sintel, © Blender Foundation, CC BY 3.0. Second audio track (mono AC-3) made by us.

Run it

Run it on Rendobar

Send the same command from your code. The job fetches each input from its URL and returns the output file's URL when it finishes.

job.ts
import { createClient } from "@rendobar/sdk";
const rb = createClient({ apiKey: process.env.RENDOBAR_API_KEY });
const job = await rb.jobs.run({
type: "ffmpeg",
inputs: {
"in.mp4": "https://cdn.rendobar.com/examples/extract-audio/in.mp4",
},
params: {
command: "ffmpeg -i in.mp4 -vn -c:a libmp3lame -q:a 2 out.mp3",
},
});
console.log(job.output.file.url);

Install with npm i @rendobar/sdk. jobs.run() submits and waits, so it returns the finished job in one call.

terminal
curl https://api.rendobar.com/jobs \
-H "Authorization: Bearer $RENDOBAR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"type": "ffmpeg",
"inputs": {
"in.mp4": "https://cdn.rendobar.com/examples/extract-audio/in.mp4"
},
"params": {
"command": "ffmpeg -i in.mp4 -vn -c:a libmp3lame -q:a 2 out.mp3"
}
}'

Returns immediately with a job id. Poll GET /jobs/{id} or register a webhook rather than blocking on the request.

Every variant

Pick the command for your case

Each one ran on the same input. The line under it is what came out. Run times are the best of three runs with FFmpeg 8.0 on one desktop machine, so read them as relative.

  1. Extract the audio to WAV

    Re-encode
    ffmpeg -i in.mp4 -vn -c:a pcm_s16le out.wav

    16-bit PCM, 48 kHz stereo. Uncompressed, so 9.2 times the size of the copied AAC.

    131 ms run 23.0 MB 120.00 s long

  2. Extract 16 kHz mono WAV for speech-to-text

    Re-encode
    ffmpeg -i in.mp4 -vn -ac 1 -ar 16000 -c:a pcm_s16le out.wav

    One channel at 16 kHz, 16-bit. A sixth of the stereo 48 kHz WAV.

    130 ms run 3.8 MB 120.00 s long

  3. Convert to MP3 at a fixed bitrate

    Re-encode
    ffmpeg -i in.mp4 -vn -c:a libmp3lame -b:a 192k out.mp3

    Constant 192 kb/s. Use -b:a only when something asks for a fixed rate.

    1.1 s run 2.9 MB 120.00 s long

  4. Extract the audio to FLAC

    Re-encode
    ffmpeg -i in.mp4 -vn -c:a flac out.flac

    Lossless FLAC of the decoded AAC. It cannot restore what AAC removed, and it is 7 times larger.

    229 ms run 17.5 MB 120.00 s long

  5. Extract 30 seconds of audio from 1:05

    Re-encode
    ffmpeg -ss 65 -i in.mp4 -t 30 -vn -c:a libmp3lame -q:a 2 out.mp3

    1:05 to 1:35 as MP3, exactly 30.0 s.

    287 ms run 616 KB 30.00 s long

  6. Extract the second audio track

    Stream copy
    ffmpeg -i tracks.mkv -map 0:a:1 -c:a copy out.ac3

    The mono AC-3 track at 96 kb/s, copied. 0:a:1 means the second audio track.

    77 ms run 720 KB 60.00 s long

  7. Extract every audio track into one file

    Stream copy
    ffmpeg -i tracks.mkv -map 0:a -c:a copy out.mka

    Both tracks, AAC stereo and AC-3 mono, copied into one Matroska audio file.

    83 ms run 2.0 MB 60.03 s long

Gotchas

Where it goes wrong

Why does -c:a copy fail when I save as .mp3?

Copy keeps the codec. An MP4 almost always holds AAC, and an .mp3 file can only hold MP3.

ffmpeg -i in.mp4 -vn -c:a copy out.mp3

Fails with Invalid audio stream. Exactly one MP3 audio stream is required. and leaves a 0-byte out.mp3 behind.

Read the codec first, then pick the extension: .m4a for AAC, .ac3 for AC-3, .mka for anything. Or re-encode to MP3.

ffprobe -v error -select_streams a -show_entries stream=codec_name -of csv=p=0 in.mp4

aac, so the copy goes into .m4a.

Which audio track does FFmpeg take when a file has several?

Without -map, only one. FFmpeg picks the track with the most channels.

ffmpeg -i tracks.mkv -vn -c:a copy out.mka

Only the stereo AAC track came out, 1.3 MB. The mono AC-3 track was left behind.

List the tracks, then name the one you want with -map 0:a:N (audio tracks count from 0) or take all of them with -map 0:a.

ffprobe -v error -select_streams a -show_entries stream=index,codec_name,channels -of csv=p=0 tracks.mkv

1,aac,2 and 2,ac3,1. The first number is the stream index in the file, not the audio track number.

Should I save copied AAC as .aac or .m4a?

Use .m4a. Both hold the same audio, but a raw .aac file has no index, so tools estimate its length.

ffmpeg -i in.mp4 -vn -c:a copy out.aac

The same 5,626 audio packets as out.m4a, but ffprobe reports 119.297 s instead of 120.000 s.

Related: Convert audio formats, Remove audio, Normalize audio, Trim a video.

Your command. Our infrastructure.

Bring your own commands. Run them on scalable CPU and GPU infrastructure, and pay only for compute time.