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?
Best Answer
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:
To convert many files:
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):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 theffprobe
command to see which codec you have, this may provide some information:A possible way to automatically parse the audio codec and name the audio file accordingly would be:
Notes: the output files are created in sub-directory
output
it creates in the beginning (if necessary). For other codecs than mp3 and vorbis it converts audio to ogg. Ubuntu 14.04 does not have ffmpeg in standard repositories, but you could add ppa:mc3man/trusty-media repository and install ffmpeg package to get the needed software. See here for details.