Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have 228 mp4 files (2.6GB) and would like to extract audio from them (mp3 or ogg). I want to batch extract them - preferably with bash. I'm not sure if all files use the same audio codec as they were recorded in different years, ranging from 2006-2012.

I want to loop through all of them, pick the file name, detect audio codec and use ffmpeg to extract the audio.

Is it possible?

share|improve this question
    
This is a scripting question and as such has nothing to do with Ubuntu. It's not off topic here, but SO or unix.SE might be a better place to ask. –  Mahesh Nov 24 '12 at 7:33
    
Thanks. Next time I'll try to ask such a question in more appropriate place. –  rrh Nov 25 '12 at 10:06
    
There are many tools are available that offers batch conversion but it is necessary to focus on the output quality because if output is not good then there are no meaning of batch conversion. Some tools are able to retain the quality of batched converted files, so download efficient audio extractor and enjoy your good quality audio. –  jack Mar 6 at 6:02

1 Answer 1

up vote 13 down vote accepted

You say you want to "extract audio from them (mp3 or ogg)". But what if the audio in the mp4 file is not one of those? you'd have to transcode anyway. So why not leave the audio format detection up to ffmpeg?

To convert one file:

ffmpeg -i videofile.mp4 -vn -acodec libvorbis audiofile.ogg

To convert many files:

for vid in *.mp4; do ffmpeg -i "$vid" -vn -acodec libvorbis "${vid%.mp4}.ogg"; done

You can of course select any ffmpeg parameters for audio encoding that you like, to set things like bitrate and so on.

Use -acodec libmp3lame and change the extension from .ogg to .mp3 for mp3 encoding.

If what you want is to really extract the audio, you can simply "copy" the audio track to a file using -acodec copy. Of course, the main difference is that transcoding is slow and cpu-intensive, while copying is really quick as you're just moving bytes from one file to another. Here's how to copy just the audio track (assuming it's in mp3 format):

ffmpeg -i videofile.mp4 -vn -acodec copy audiofile.mp3

Note that in this case, the audiofile format has to be consistent with what the container has (i.e. if the audio is AAC format, you have to say audiofile.aac). You can use the ffprobe command to see which formats you have, this may provide some information:

for file in *; do ffprobe $file 2>&1 |grep Audio; done

A possible way to automatically parse the audio codec and name the audio file accordingly would be:

for file in *mp4 *avi; do ffmpeg -i "$file" -vn -acodec copy "$file".`ffprobe "$file" 2>&1 |sed -rn 's/.*Audio: (...), .*/\1/p'`; done

Note that this command uses sed to parse output from ffprobe for each file, it assumes a 3-letter audio codec name (e.g. mp3, ogg, aac) and will break with anything different.

share|improve this answer
    
I put it wrong. I wanted to have mp3 or ogg but as you noticed the audio codec was different - aac in this example. So I have to transcode them anyway. Thank you very much for your help! –  rrh Nov 25 '12 at 10:04
    
Your automagic example breaks with filenames that have spaces in them because you didn't quote $file in the ffprobe sub-statement. Changing it to: for file in *mp4 *avi; do ffmpeg -i "$file" -vn -acodec copy "$file".ffprobe "$file" 2>&1 |sed -rn 's/.*Audio: (...), .*/\1/p'; done Fixes that. –  Andrew Beals Apr 24 '13 at 8:56
    
+1 but looks like this answer could use an update: This program is only provided for compatibility and will be removed in a future release. Please use avconv instead. –  Nicolas Raoul Apr 4 at 1:10
    
thanks, very helpful –  Max L. Apr 9 at 0:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.