616

I tried the following command to extract audio from video:

ffmpeg -i Sample.avi -vn -ar 44100 -ac 2 -ab 192k -f mp3 Sample.mp3

but I get the following output

libavutil     50.15. 1 / 50.15. 1
libavcodec    52.72. 2 / 52.72. 2
libavformat   52.64. 2 / 52.64. 2
libavdevice   52. 2. 0 / 52. 2. 0
libavfilter    1.19. 0 /  1.19. 0
libswscale     0.11. 0 /  0.11. 0
libpostproc   51. 2. 0 / 51. 2. 0
SamplE.avi: Invalid data found when processing input

Can anyone help, please?

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
user1269669
  • 6,177
  • 3
  • 14
  • 5
  • This is a question about a task on a computer, where does it say it has to be about a programming? – dvdmn Aug 08 '23 at 17:58

13 Answers13

847

To extract the audio stream without re-encoding:

ffmpeg -i input-video.avi -vn -acodec copy output-audio.aac
  • -vn is no video.
  • -acodec copy says use the same audio stream that's already in there.

Read the output to see what codec it is, to set the right filename extension.

llogan
  • 121,796
  • 28
  • 232
  • 243
Paul Irish
  • 47,354
  • 22
  • 98
  • 132
  • 135
    and instead of running ffmpeg a first time just to check which audio stream it is, use ffprobe input-video.avi – FlorianB Dec 02 '16 at 06:45
  • 47
    If you only extract audio from a video stream, the length of the audio may be shorter than the length of the video. To make sure this doesn't happen, extract both audio AND video with the same call to ffmpeg, e.g. "ffmpeg -i vid.avi -map 0:a audio.wav -map 0:v onlyvideo.avi – BlenderBender Aug 11 '17 at 14:01
  • 5
    It would be great to expand this answer with a command that also encapsulates the raw AAC into a M4A container. – Gras Double Jun 05 '18 at 10:47
  • 35
    In this precise case, just replace ".aac" with ".m4a" in the destination path. ffmpeg is smart enough to guess your intent, and it encapsulates the result into an M4A container :) – Gras Double Jun 05 '18 at 11:03
  • 2
    FWIW, I have crafted a PHP script, so that I just specify the input video file, and it automatically determines the extension of the output audio file: https://superuser.com/questions/1301901/ffmpeg-how-to-determine-output-extension-automatically-ca-copy/1336570#1336570 – Gras Double Jul 22 '18 at 07:08
  • 2
    `Unable to find a suitable output format for 'acodec'`... Opps that was due to `acodec` instead of `-acodec`. – Chris Stryczynski May 31 '19 at 12:17
  • 2
    @FlorianB how is running ffmpeg an issue? Isn't `ffmpeg` without output argument an alias for `ffprobe` anyway? – seeker_of_bacon Jan 14 '20 at 21:00
  • incredibly fast! in my case: `size=16713kB time=00:23:08.13 bitrate=98.6kbits/s speed=2.2e+03x video:0kB audio:16268kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 2.734343%` – quantme Mar 23 '20 at 16:02
  • Does someone have an idea of how to do using c API? – waqas akram Jul 06 '20 at 15:26
  • Or if you prefer MP3 where size matters use: `ffmpeg -i inputVid.avi -b:a 192k outputAu.mp3` – Pouria Moosavi Nov 27 '20 at 08:05
  • I have an opposite issue. I ran `ffmpeg -i input.mp4 -ss 00:01:57 -c copy -t 00:51:05 output.mp4` but output was only audio? I also tried output.avi, same thing. Can someone tell me where I'm going wrong? – JJGabe Jan 19 '21 at 15:14
  • UPDATE: I ended up needing to change the order of the arguments to get the video. I used `ffmpeg -ss 00:01:57 -i input.mp4 -t 00:51:05 -c copy output.mp4` and it worked great (took only a few seconds). I don't have an explanation as to why I needed to change the order from @Paul Irish's answer – JJGabe Jan 19 '21 at 16:12
  • To quote superuser.com: The basic command is: ffmpeg -i filename.mp4 filename.mp3 Only -i flag is necessary. Just tested it ... also works with .ts to .aac – Torben Vesterager Apr 09 '21 at 09:01
  • @paul-irish: Does this method by any chance change the audio length from the video length? I saw Jeremy's answer here and I got an audio with length `1min 2 seconds` - whereas video was only `1 minutes` - I don't want that, since later on I want to merge the audio back to a modified video – aspiring1 Jun 14 '22 at 07:46
375

To encode a high quality MP3 or MP4 audio from a movie file (eg AVI, MP4, MOV, etc) or audio file (eg WAV), I find it's best to use -q:a 0 for variable bit rate and it's good practice to specify -map a to exclude video/subtitles and only grab audio:

ffmpeg -i sample.avi -q:a 0 -map a sample.mp3

If you want to extract a portion of audio from a video use the -ss option to specify the starting timestamp, and the -t option to specify the encoding duration, eg from 3 minutes and 5 seconds in for 45 seconds:

ffmpeg -i sample.avi -ss 00:03:05 -t 00:00:45.0 -q:a 0 -map a sample.mp3
  • The timestamps need to be in HH:MM:SS.xxx format or in seconds.

  • If you don't specify the -t option it will go to the end.

  • You can use the -to option instead of the -t option, if you want to specify the range, eg for 45 seconds: 00:03:05 + 45 = 00:03:50

Working example:

  1. Download ffmpeg
  2. Open a Command Prompt (Start > Run > CMD) or on a Mac/Linux open a Terminal
  3. cd (the change directory command) to the directory with the ffmeg.exe, as depicted.
  4. Issue your command and wait for the output file (or troubleshoot any errors)

Windows enter image description here

Mac/Linux enter image description here enter image description here

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • 7
    Here's a guide for the `-q:a` values: https://trac.ffmpeg.org/wiki/Encode/MP3 Zero is the highest-quality VBR setting, which typically results in average bitrates in the upper part of the sane CBR range (where 320 kbit/s is the maximum, probably a bit beyond the point at which lossless compression becomes more appropriate). – Evgeni Sergeev Sep 15 '16 at 08:32
  • 6
    And what does `-map a` do? – Iulian Onofrei Sep 22 '18 at 13:47
  • 6
    Caveat: this is re-encoding the audio with a lossy compression, which will degrade quality slightly. If the original is already compressed, you will degrade the quality twice, which may or may not be noticeable. – Davidmh Oct 08 '18 at 10:00
  • 2
    This command works for me, when the one above with `-vn` does not. – Mmm Apr 01 '20 at 22:38
  • 2
    Re-encoding is not extraction, so this does not answer the question. – Bachsau Aug 21 '20 at 01:00
  • @Bachsau I clearly explain its variable bit rate re-encoding and my answer also addressed extracting portions. 130 people benefitted (with enough rep to vote) from this answer so far and you're welcome to post your own answer. – Jeremy Thompson Aug 21 '20 at 08:33
  • I want to get .mp3 file from .mp4 file ,use the cmd ,it worked, detail: `ffmpeg -i test.mp4 -q:a 0 -map a novideo.mp3` – zhangjin Sep 27 '20 at 03:35
  • @IulianOnofrei `-map a` includes audio only. – FFmpegEnthusiast Jun 19 '23 at 21:20
114

Extract all audio tracks / streams

This puts all audio into one file:

ffmpeg -i input.mov -map 0:a -c copy output.mov
  • -map 0:a selects all audio streams only. Video and subtitles will be excluded.
  • -c copy enables stream copy mode. This copies the audio and does not re-encode it. Remove -c copy if you want the audio to be re-encoded.
  • Choose an output format that supports your audio format. See comparison of container formats.

Extract a specific audio track / stream

Example to extract audio stream #4:

ffmpeg -i input.mkv -map 0:a:3 -c copy output.m4a
  • -map 0:a:3 selects audio stream #4 only (ffmpeg starts counting from 0).
  • -c copy enables stream copy mode. This copies the audio and does not re-encode it. Remove -c copy if you want the audio to be re-encoded.
  • Choose an output format that supports your audio format. See comparison of container formats.

Extract and re-encode audio / change format

Similar to the examples above, but without -c copy. Various examples:

ffmpeg -i input.mp4 -map 0:a output.mp3
ffmpeg -i input.mkv -map 0:a output.m4a
ffmpeg -i input.avi -map 0:a -c:a aac output.mka
ffmpeg -i input.mp4 output.wav

Extract all audio streams individually

This input in this example has 4 audio streams. Each audio stream will be output as single, individual files.

ffmpeg -i input.mov -map 0:a:0 output0.wav -map 0:a:1 output1.wav -map 0:a:2 output2.wav -map 0:a:3 output3.wav

Optionally add -c copy before each output file name to enable stream copy mode.


Extract a certain channel

Use the channelsplit filter. Example to get the Front Right (FR) channel from a stereo input:

ffmpeg -i stereo.wav -filter_complex "[0:a]channelsplit=channel_layout=stereo:channels=FR[right]" -map "[right]" front_right.wav
  • channel_layout is the channel layout of the input. It is not automatically detected so you must provide the layout name.
  • channels lists the channel(s) you want to extract.
  • See ffmpeg -layouts for audio channel layout names (for channel_layout) and channel names (for channels).
  • Using stream copy mode (-c copy) is not possible to use when filtering, so the audio must be re-encoded.
  • See FFmpeg Wiki: Audio Channels for more examples.

What's the difference between -map and -vn?

ffmpeg has a default stream selection behavior that will select 1 stream per stream type (1 video, 1 audio, 1 subtitle, 1 data).

-vn is an old, legacy option. It excludes video from the default stream selection behavior. So audio, subtitles, and data are still automatically selected unless told not to with -an, -sn, or -dn.

-map is more complicated but more flexible and useful. -map disables the default stream selection behavior and ffmpeg will only include what you tell it to with -map option(s). -map can also be used to exclude certain streams or stream types. For example, -map 0 -map -0:v would include all streams except all video.

See FFmpeg Wiki: Map for more examples.


Errors

Invalid audio stream. Exactly one MP3 audio stream is required.

MP3 only supports 1 audio stream. The error means you are trying to put more than 1 audio stream into MP3. It can also mean you are trying to put non-MP3 audio into MP3.

WAVE files have exactly one stream

Similar to above.

Could not find tag for codec in stream #0, codec not currently supported in container

You are trying to put an audio format into an output that does not support it, such as PCM (WAV) into MP4.

Remove -c copy, choose a different output format (change the file name extension), or manually choose the encoder (such as -c:a aac).

See comparison of container formats.

Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument

This is a useless, generic error. The actual, informative error should immediately precede this generic error message.

llogan
  • 121,796
  • 28
  • 232
  • 243
  • How do you do the "Extract all audio streams individually" case, when you don't know exactly how many audio streams there are? I need a generalizable way to extract all audio streams individually. – Raleigh L. Mar 13 '23 at 21:50
58

Seems like you're extracting audio from a video file & downmixing to stereo channel.
To just extract audio (without re-encoding):

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

To extract audio & downmix to stereo (without re-encoding):

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

To generate an mp3 file, you'd re-encode audio:

ffmpeg -i in.mp4 -vn -ac 2 out.mp3

-c (select codecs) & -map (select streams) options:

-c:a -> select best supported audio (transcoded)
-c:a copy -> best supported audio (copied)
-map 0:a -> all audio from 1st (audio) input file (transcoded)
-map 0:0 -> 1st stream from 1st input file (transcoded)
-map 1:a:0 -> 1st audio stream from 2nd (audio) input file (transcoded)
-map 1:a:1 -c:a copy -> 2nd audio stream from 2nd (audio)input file (copied)

FFmpegEnthusiast
  • 193
  • 1
  • 10
Zimba
  • 2,854
  • 18
  • 26
13

ffmpeg -i sample.avi will give you the audio/video format info for your file. Make sure you have the proper libraries configured to parse the input streams. Also, make sure that the file isn't corrupt.

smp
  • 985
  • 5
  • 10
  • now,i got correct result when i tried the same comment with a .mkv vedio.why it is not working for .avi,is there any ffmpeg command to convert .avi – user1269669 Mar 29 '12 at 15:44
  • 1
    AVI or MKV refers to the container format which can hold lots of different audio/video formats. You are not looking to convert AVI audio, you are looking to convert the audio track within the AVI file, which you haven't specified as of yet. Use the command I listed above to see what kind of audio track is within the AVI file. If it is MP3 you'll need to compile FFMPEG with an MP3 library like LAME http://lame.sourceforge.net/ – smp Mar 29 '12 at 21:00
12

The command line is correct and works on a valid video file. I would make sure that you have installed the correct library to work with mp3, install lame o probe with another audio codec.

Usually

ffmpeg -formats

or

ffmpeg -codecs

would give sufficient information so that you know more.

meduz
  • 3,903
  • 1
  • 28
  • 40
Álvaro
  • 2,872
  • 4
  • 20
  • 25
11

To encode mp3 audio ffmpeg.org shows the following example:

ffmpeg -i input.wav -codec:a libmp3lame -qscale:a 2 output.mp3

I extracted the audio from a video just by replacing input.wav with the video filename. The 2 means 190 kb/sec. You can see the other quality levels at my link above.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
11

For people looking for the simpler way to extract audio from a video file while retaining the original video file's parameters, you can use:

ffmpeg -i <video_file_name.extension> <audio_file_name.extension>

For example, running:

ffmpeg -i screencap.mov screencap.mp3

extracts an mp3 audio file from a mov video file.

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
9

Here's what I just used:

ffmpeg -i my.mkv -map 0:3 -vn -b:a 320k my.mp3

Options explanation:

  • my.mkv is a source video file, you can use other formats as well
  • -map 0:3 means I want 3rd stream from video file. Put your N there - video files often has multiple audio streams; you can omit it or use -map 0:a to take the default audio stream. Run ffprobe my.mkv to see what streams does the video file have.
  • my.mp3 is a target audio filename, and ffmpeg figures out I want an MP3 from its extension. In my case the source audio stream is ac3 DTS and just copying wasn't what I wanted
  • 320k is a desired target bitrate
  • -vn means I don't want video in target file
Artem Vasiliev
  • 2,063
  • 1
  • 24
  • 21
6

Creating an audio book from several video clips

  1. First, extracting the audio (as `.m4a) from a bunch of h264 files:
for f in *.mp4; do ffmpeg -i "$f" -vn -c:a copy "$(basename "$f" .mp4).m4a"; done

the -vn output option disables video output (automatic selection or mapping of any video stream). For full manual control see the -map option.

Optional

  1. If there's an intro of, say, 40 seconds, you can skip it with the -ss parameter:
for f in *.m4a; do ffmpeg -i "$f" -ss 00:00:40  -c copy crop/"$f"; done
  1. To combine all files in one:
ffmpeg -f concat -safe 0 -i <(for f in ./*.m4a; do echo "file '$PWD/$f'"; done) -c copy output.m4a
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81
2

If the audio wrapped into the avi is not mp3-format to start with, you may need to specify -acodec mp3 as an additional parameter. Or whatever your mp3 codec is (on Linux systems its probably -acodec libmp3lame). You may also get the same effect, platform-agnostic, by instead specifying -f mp3 to "force" the format to mp3, although not all versions of ffmpeg still support that switch. Your Mileage May Vary.

Xheralt
  • 29
  • 2
2

Use -b:a instead of -ab as -ab is outdated now, also make sure your input file path is correct.

To extract audio from a video I have used below command and its working fine.

String[] complexCommand = {"-y", "-i", inputFileAbsolutePath, "-vn", "-ar", "44100", "-ac", "2", "-b:a", "256k", "-f", "mp3", outputFileAbsolutePath};

Here,

  • -y - Overwrite output files without asking.
  • -i - FFmpeg reads from an arbitrary number of input “files” specified by the -i option
  • -vn - Disable video recording
  • -ar - sets the sampling rate for audio streams if encoded
  • -ac - Set the number of audio channels.
  • -b:a - Set the audio bitrate
  • -f - format

Check out this for my complete sample FFmpeg android project on GitHub.

Raz Luvaton
  • 3,166
  • 4
  • 21
  • 36
Android Developer
  • 9,157
  • 18
  • 82
  • 139
2

To extract without conversion I use a context menu entry - as file manager custom action in Linux - to run the following (after having checked what audio type the video contains; example for video containing ogg audio):

bash -c 'ffmpeg -i "$0" -map 0:a -c:a copy "${0%%.*}".ogg' %f 

which is based on the ffmpeg command ffmpeg -i INPUT -map 0:a -c:a copy OUTPUT.

I have used -map 0:1 in that without problems, but, as said in a comment by @LordNeckbeard, "Stream 0:1 is not guaranteed to always be audio. Using -map 0:a instead of -map 0:1 will avoid ambiguity."

  • @LordNeckbeard ~ I will take that into account, thanks. –  May 19 '18 at 19:10
  • I used `mediainfo` to determine the audio track, and chose it like this: `-map 0:a:1` where a:1 is the second audio track index, thx! `-c:a copy` made it much faster! – Aquarius Power Apr 26 '19 at 00:02