FFmpeg: Extract audio from video in original format or convert it to MP3 or Ogg Vorbis

This article explains how to use FFmpeg Extract audio stream from video without re-encoding (maintaining the original format) or converting the resulting audio file to CBR (constant bit rate) or VBR (variable bit rate) MP3 or Ogg Vorbis.To use the FFmpeg examples / commands below, you need to have FFmpeg installed on your system. (Optional) Also install libmp3lame for converting to mp3, and libvorbis for converting the extracted audio to Ogg Vorbis.
To extract audio from a video and maintain the original audio format, the first thing you need to do is to check what the original audio format is so you can use it to know which file extension to use. ffprobe (Part of the FFmpeg package):

ffprobe myvideo.mp4

Replace myvideo.mp4 With the video file name (or the path if the video is not in the current folder). Near the end of the output of this command, you should see the audio stream, for example:

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)
HANDLER_NAME : VideoHandler
DURATION : 00:04:19.440000000
Stream #0:1(eng): Audio: vorbis, 44100 Hz, stereo, fltp (default)
DURATION : 00:04:19.467000000

You might 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, you can use the following methods to extract audio from the video without re-encoding:

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

Replace myvideo.mp4 With video file name / path, and audio.ogg And the name and extension to be used for the audio output file name.
In this command -i Used to specify the path and file name of the input video, -vn Skip the inclusion of the video stream, and -acodec copy Used to copy the original audio (without re-encoding). Extract only a portion of the audio from the 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 from 2 minutes into a 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 cannot contain a video stream, to extract audio from the video and secretly convert it to mp3, you can use it (skip to add -vn):

ffmpeg -i myvideo.mp4 audio.mp3

FFmpeg usually automatically detects the output format for the output file from the input extension, so there is no need to specify the format / codec in most cases. However, if you do want to specify a codec, add -codec:a libmp3lame Convert to mp3:

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

Extract audio from video, convert the extracted audio to mp3, and specify a constant audio bit rate (CBR) quality:

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

Replace BITRATE with the following available CBR options: 8k, 16k, 24k, 32k, 40k, 48k, 64k, 80k, 96k, 112k, 128k, 160k, 192k, 224k, 256k or 320k. Extract audio from video, convert audio to mp3 with variable bit rate encoding:

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

Specify QUALITY inside 0-9 Scope, where 0 it’s the best, 9 The worst thing is 4 Is the default. The following table lists each FFmpeg VBR option: Available hereExtract audio from video, use Ogg as result container (Vorbis should be detected automatically as audio format), and use variable bit rate (VBR):

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

of QUALITY Range is -1.0 to 10.0With 10.0 Is the highest quality, and 3 The default value (the target is 112kbps). See this page Details on setting the quality of Ogg Vorbis VBR.
In rare cases, FFmpeg cannot automatically detect the codec, please specify libvorbis Use -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. View this batch processing example (adjusted according to your actual needs). In this example, all mp4 videos in the current folder are converted to mp3 with a variable bit rate profile value of 3. 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 FFmpeg document For more advanced usage.