日本語

How to Extract or Remove Audio from a Video with FFmpeg

​​FFmpeg​AAC,ffprobe,MP3,video editing

To extract only the audio from a video with FFmpeg, add -vn (no video). To remove the audio and keep only the video, add -an (no audio). When you copy the stream you keep with -c:a copy or -c:v copy, the job finishes instantly without re-encoding. The copied data was identical to the original video. This is useful when you want a video's BGM or speech as an m4a, MP3, or WAV file, or when you want to mute a video before posting it to social media.

Note

The test environment was Ubuntu 24.04 with FFmpeg 6.1.1 (the Ubuntu package). The test video was a 30-second MP4 (H.264 + AAC) made from FFmpeg's test pattern (testsrc2) and a constant-pitch tone (sine). It was not shot with a camera or microphone. I have not tested FFmpeg 7 or later, inputs other than MP4 (such as MKV or MOV), audio codecs other than AAC, or playback in video players.

Diagram showing that -vn writes an audio-only file and -an writes a video-only file from the original video
-vn and -an select which streams go into the output

Inside a video, video and audio are separate streams

In video files such as MP4, the video and audio are stored as separate data streams. When I checked the test video with ffprobe, it had two streams: video (H.264) and audio (AAC).

text
$ ffprobe -v error -show_entries stream=index,codec_type,codec_name,bit_rate,duration:stream_tags=language:format=duration,size,bit_rate -of compact=p=0 in.mp4
index=0|codec_name=h264|codec_type=video|duration=30.000000|bit_rate=736573|tag:language=und
index=1|codec_name=aac|codec_type=audio|duration=30.000000|bit_rate=128019|tag:language=jpn
duration=30.000000|size=3276429|bit_rate=873714

-vn and -an are options that keep the video or audio stream, respectively, out of the output (the stream selection description in the official documentation source).

-c copy is called stream copy. It does not decode or encode. It just takes the compressed data out of one container and puts it into another. The stream copy description in the official documentation also says it is very fast and does not lower image or sound quality.

Extracting only the audio

Keep AAC and write m4a (no re-encoding)

If the audio in the original video is AAC, you can copy it as-is into an m4a container.

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

The commands in this article were actually run with -hide_banner. Except in the error example, I also added -loglevel error, and for commands that write files I added -y. The job took 0.069 seconds (the real value from the time command). The output file had one AAC audio stream, and its length and bitrate were the same as the original video's audio.

text
$ ffprobe -v error -show_entries stream=index,codec_type,codec_name,bit_rate,duration:format=format_name,duration,size -of compact=p=0 audio.m4a
index=0|codec_name=aac|codec_type=audio|duration=30.000000|bit_rate=128019
format_name=mov,mp4,m4a,3gp,3g2,mj2|duration=30.000000|size=486852

To confirm the contents are really identical, I compared MD5 hashes of the audio data using FFmpeg's streamhash output format. The original video and the extracted m4a produced the same value.

text
$ ffmpeg -i in.mp4 -map 0:a -c copy -f streamhash -hash md5 -
0,a,MD5=a96cc02d36cfe57aa389462704c9388b
$ ffmpeg -i audio.m4a -map 0:a -c copy -f streamhash -hash md5 -
0,a,MD5=a96cc02d36cfe57aa389462704c9388b

Naming the output .m4a does not remove video without -vn

When I left out -vn and only changed the output file name to .m4a, the result still contained video. No error appeared. In other words, the file extension of the output name alone does not remove the video.

text
$ ffmpeg -i in.mp4 -c:a copy audio-novn.m4a; echo exit=$?
exit=0
$ ffprobe -v error -show_entries stream=index,codec_type,codec_name -of compact=p=0 audio-novn.m4a
index=0|codec_name=h264|codec_type=video
index=1|codec_name=aac|codec_type=audio

When you want an audio-only file, always add -vn, regardless of the output format.

Convert to MP3 or WAV (re-encoding)

Copying the AAC audio into a .mp3 file with -c:a copy stopped with an error. An MP3 file can only hold MP3 audio. The MP3 output code in FFmpeg does not accept audio in other formats.

text
$ ffmpeg -i in.mp4 -vn -c:a copy audio-copy.mp3; echo exit=$?
(入力の情報は省略)
[mp3 @ 0x55ad458e8740] Invalid audio stream. Exactly one MP3 audio stream is required.
[out#0/mp3 @ 0x55ad458d7400] Could not write header (incorrect codec parameters ?): Invalid argument
Error opening output file audio-copy.mp3.
Error opening output files: Invalid argument
exit=234

A 0-byte audio-copy.mp3 was left behind. It has no content, so delete it if you don't need it.

To make an MP3, re-encode with libmp3lame. -q:a sets a variable bitrate (VBR) with a fixed quality level (official documentation for libmp3lame). I used 2 in the test.

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

To make a WAV, the output was uncompressed (pcm_s16le) even though I did not specify a codec.

bash
ffmpeg -i in.mp4 -vn audio.wav

The results were as follows:

Output Method Time (real) Size Audio
audio.m4a Copy 0.069 seconds 486,852 bytes AAC, 128 kb/s, 30.000 s
audio.mp3 Conversion (encoded to MP3) 0.291 seconds 144,796 bytes MP3, 38 kb/s (VBR), 30.048 s
audio.wav Conversion (written uncompressed) 0.155 seconds 5,763,150 bytes PCM, 1,536 kb/s, 30.016 s

The MP3 and WAV files were slightly longer than the original 30 seconds. MP3 uses VBR, so its bitrate changes with the sound. The test audio is a constant-pitch tone, so this 38 kb/s figure is not a useful guide for music or speech files.

Removing the audio and keeping only the video

Copy the video with -c:v copy, and use -an to leave out the audio.

bash
ffmpeg -i in.mp4 -an -c:v copy video-only.mp4

The job took 0.060 seconds. The output contained only a video stream, with 900 frames over 30 seconds, the same as the original video. The MD5 of the video data was also the same as the original.

text
$ ffprobe -v error -show_entries stream=index,codec_type,codec_name,nb_frames,duration:format=duration,size -of compact=p=0 video-only.mp4
index=0|codec_name=h264|codec_type=video|duration=30.000000|nb_frames=900
duration=30.000000|size=2773219
$ ffmpeg -i in.mp4 -map 0:v -c copy -f streamhash -hash md5 -
0,v,MD5=211df759369178ec159cabe3fe72d36a
$ ffmpeg -i video-only.mp4 -map 0:v -c copy -f streamhash -hash md5 -
0,v,MD5=211df759369178ec159cabe3fe72d36a

Forgetting -c:v copy re-encodes the video

When I added only -an and left out -c:v copy, the video was decoded and then re-encoded with H.264 (libx264). The time rose from 0.060 seconds to 2.323 seconds, and the video MD5 changed to a different value (16f56445496a6500fe6589331b38fc2c).

Method Time (real) Size Video MD5
-an -c:v copy 0.060 seconds 2,773,219 bytes Same as original
-an only 2.323 seconds 2,649,684 bytes Different from original

Selecting and extracting or removing one of two audio tracks

Some videos contain two audio tracks, such as Japanese and English. I tested with a video that had Japanese (jpn, a 440 Hz tone) and English (eng, an 880 Hz tone) audio tracks.

text
$ ffprobe -v error -show_entries stream=index,codec_type,codec_name:stream_tags=language -of compact=p=0 in-2audio.mp4
index=0|codec_name=h264|codec_type=video|tag:language=und
index=1|codec_name=aac|codec_type=audio|tag:language=jpn
index=2|codec_name=aac|codec_type=audio|tag:language=eng

With -vn -c:a copy alone, only the first audio track (jpn) went into the output. According to the automatic stream selection description in the official documentation, when -map is not specified, FFmpeg picks one audio stream: the one with the most channels, or the lower-numbered one if they are equal. Both tracks in the test were stereo, so the first one was chosen.

To choose a different audio track, specify it with the -map option. 0:a:1 means "the second audio stream (counting from 0) of the first input file." Once you write -map, video is no longer selected automatically, so you don't need -vn.

bash
# Extract only the second audio track
ffmpeg -i in-2audio.mp4 -map 0:a:1 -c:a copy second.m4a
# Extract both audio tracks
ffmpeg -i in-2audio.mp4 -map 0:a -c:a copy both.m4a
text
$ ffprobe -v error -show_entries stream=index,codec_type:stream_tags=language -of compact=p=0 second.m4a
index=0|codec_type=audio|tag:language=eng
$ ffprobe -v error -show_entries stream=index,codec_type:stream_tags=language -of compact=p=0 both.m4a
index=0|codec_type=audio|tag:language=jpn
index=1|codec_type=audio|tag:language=eng

To remove the audio, -an -c:v copy dropped both tracks and left only the video. To remove just one track and keep the others, select everything with -map 0, then exclude the track you don't want by prefixing it with -, as in -map -0:a:0.

bash
ffmpeg -i in-2audio.mp4 -map 0 -map -0:a:0 -c copy drop-first.mp4
text
$ ffprobe -v error -show_entries stream=index,codec_type:stream_tags=language -of compact=p=0 drop-first.mp4
index=0|codec_type=video|tag:language=und
index=1|codec_type=audio|tag:language=eng

The first track (jpn) was removed, and the video and the second track (eng) remained.

​Posted by 86