FFmpeg: Extract Audio From Video In Original Format Or Converting It To MP3 Or Ogg Vorbis

This article explains how to use FFmpeg to extract the audio stream from a video, either without re-encoding (keeping the original format), or converting the resulting audio file to CBR (constant bitrate) or VBR (variable bitrate) MP3 or Ogg Vorbis.

To use the FFmpeg examples / commands below, you'll need to have FFmpeg installed on your system. Optionally also install libmp3lame for converting to mp3, and libvorbis for converting the extracted audio to Ogg Vorbis.

To extract the audio from a video, and keep the original audio format, the first thing you'll want to do is check what's the original audio format so you know what file extension to use, by using ffprobe (part of the FFmpeg package):

ffprobe myvideo.mp4

Replace myvideo.mp4 with the video filename (and path if the video is not in the current folder). Near the end of this command's output you should see the audio stream, e.g.:

...............
Duration: 00:04:19.47, start: 0.000000, bitrate: 1261 kb/s
Stream #0:0: Video: h264 (Main), yuv420p(tv, bt709, progressive), 1280x720 [SAR 1:1 DAR 16:9], 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
Metadata:
HANDLER_NAME : VideoHandler
DURATION : 00:04:19.440000000
Stream #0:1(eng): Audio: vorbis, 44100 Hz, stereo, fltp (default)
Metadata:
DURATION : 00:04:19.467000000

You may also like: How To Download A YouTube Playlist And Convert It To MP3 Using youtube-dl (Command Line)

Now that you know the original audio format, extract the audio from the video without re-encoding it using:

ffmpeg -i myvideo.mp4 -vn -acodec copy audio.ogg

Replace myvideo.mp4 with the video filename/path, and audio.ogg with the name you want to use for the audio output filename, and the extension.

In this command, -i is used to specify the path and filename of the input video, -vn skips the inclusion of the video stream, while -acodec copy is for copying the original audio (without re-encoding).

Extract only a portion of the audio from a video, using the same audio format used by the original source:

ffmpeg -i myvideo.mp4 -ss 00:00:00 -t 00:00:00.0 -vn -acodec copy audio.ogg

-ss is the start of the extracted audio timestamp, and -t is the audio duration.

For example, to extract 30 seconds of audio starting 2 minutes into the video, and use the same audio codec as the original source:

ffmpeg -i myvideo.mp4 -ss 00:02:00 -t 00:00:30.0 -vn -acodec copy audio.ogg

Since mp3 can't contain a video stream, to extract audio from video and covert it to mp3 it's enough to use (skip adding -vn):

ffmpeg -i myvideo.mp4 audio.mp3

FFmpeg normally audo-detects the output format from the extension you enter for the output files, so specifying the format/codec is not needed in most cases. But if you do want to specify the codec, add -codec:a libmp3lame to convert to mp3:

ffmpeg -i myvideo.mp4 -codec:a libmp3lame audio.mp3

Extract audio from video, converting the extracted audio to mp3, and specifying a constant audio bitrate (CBR) quality:

ffmpeg -i myvideo.mp4 -b:a BITRATE audio.mp3

Replace BITRATE with these available CBR options: 8k, 16k, 24k, 32k, 40k, 48k, 64k, 80k, 96k, 112k, 128k, 160k, 192k, 224k, 256k, or 320k.

Extract audio from video, converting the audio to mp3 with variable bitrate encoding:

ffmpeg -i myvideo.mp4 -codec:a libmp3lame -q:a QUALITY audio.mp3

Specify the QUALITY in the 0-9 range, where 0 is best, 9 is worst, and 4 is the default value. A table presenting each FFmpeg VBR option is available here.

Extract audio from a video, using Ogg as the resulting container (it should auto-detect Vorbis as the audio format), with variable bitrate (VBR):

ffmpeg -i myvideo.mp4 -vn -q:a QUALITY audio.ogg

The QUALITY range is -1.0 to 10.0, with 10.0 being the highest quality, and 3 being default (with a target of 112kbps). See this page for details on setting the Ogg Vorbis VBR quality.

In the rare case in which FFmpeg fails to auto-detect the codec, specify libvorbis using -codec:a, like this:

ffmpeg -i myvideo.mp4 -vn -codec:a libvorbis -q:a QUALITY audio.ogg

In some cases you may want to batch convert multiple videos to audio. Check out this example of batch processing (adapt this to your exact needs) in which all mp4 videos in the current folder are converted to mp3 with a variable bitrate profile value of 3, with the file name the same as the original video but with mp3 as the file extension:

for i in *.mp4; do ffmpeg -i "$i" -codec:a libmp3lame -q:a 3 "${i%.*}.mp3"; done

See the FFmpeg documentation for more advanced usage.