ffmpeg to extract audio from video

ffmpeg to extract audio from video



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


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



but i get output as


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?




9 Answers
9



To extract the audio stream without re-encoding:


ffmpeg -i input-video.avi -vn -acodec copy output-audio.aac


-vn


-acodec copy



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





and instead of running ffmpeg a first time just to check which audio stream it is, use ffprobe input-video.avi
– FlorianB
Dec 2 '16 at 6:45





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





This works for me.
– Sam
Oct 20 '17 at 1:40





It would be great to expand this answer with a command that also encapsulates the raw AAC into a M4A container.
– Gras Double
Jun 5 at 10:47





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 5 at 11:03



To encode a high quality MP3 from an AVI best using -q:a for variable bit rate.


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.



Another popular request is extracting images from videos, here is the command for frames per second to images:


ffmpeg.exe -i simpsonsRockBottom.mp4 -r 1/1 $filename%03d.jpg -ss 00:00:52 -t 00:01:52





Here's a guide for the -q:a values: 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 8:32


-q:a



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.



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.


ffmpeg -i sample.avi





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






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 lame.sourceforge.net
– smp
Mar 29 '12 at 21:00




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.


input.wav


2



Just a guess:


-ab 192



should be


-ab 192k



Seems like that could be a problem, but maybe ffmpeg is smart enough to correct it.





Valid comment, and I edited the OP's question to change it, but this does not change anything (at most, this produces a warning).
– Jean-Philippe Pellet
Jun 22 '14 at 10:19



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.


-acodec mp3


-acodec libmp3lame


-f mp3



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


-b:a


-ab


-ab



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.



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):


ogg


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.


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."


-map 0:1


0:1


-map 0:a


-map 0:1





Stream 0:1 is not guaranteed to always be audio. Using -map 0:a instead of -map 0:1 will avoid ambiguity.
– LordNeckbeard
May 17 at 18:23


0:1


-map 0:a


-map 0:1





@LordNeckbeard ~ I will take that into account, thanks.
– cipricus
May 19 at 19:10




Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



Would you like to answer one of these unanswered questions instead?

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

Henj