I have about 70 mp4 files, and I want to extract the audio files directly without transcoding further. All the mp4 files have aac audio files.
I understand I can use the following command to extract the audio file from one of the mp4 files:
avconv -i "INPUT FILE" -map 0:1 -c:a copy "OUTPUT FILE"
How do I extract all the audio files from all the 70 mp4 files, so I end up with 70 aac files?
You can use a simple for loop:
or on one line:
What is does is run
avconv
once for every file named like*.mp4
where the filename is stored in the${i}
variable.${i%.mp4}
means${i}
(ie. the filename) with.mp4
stripped off from the end.Xiao's answer is generally the most useful if you have all the files in one directory; but if there are MP4 files scattered in different directories, you can use this
find
command to convert them all.This uses a slightly different form of bash string substitution at the end: "${0/%mp4/m4a}" tells bash to replace mp4 with m4a, but only if the mp4 is at the end of the string.
To avoid multiple files being processed at the same time (which might use too many resources), use
parallel
. By default, it only does one job at a time. If you want to use multiple CPU cores in parallel, tryparallel -j 4 ...
for 4 jobs in parallel. Also check outman parallel
.This line will extract
aac
audio files from selected videos and put them intom4a
containers.When working straight in the CLI, substitute
%F
with the list of your input files. E.g.*.mp4
.Inspired by this answer, by a previous answer to this question, and by evilsoup's comment to the latter (saying: 'raw aac files can't contain metadata; if you want to keep metadata from the original files then use m4a (which is just another name for mp4, but is fairly widely recognised by audio players) instead of aac as a file extension').
I use such commands in Thunar custom actions,
limiting the application to videos that contain
aac
audio.To have a terminal window open during processing, which closes at the end, the command can be changed like:
I extended @Xiao-Long Chen's answer, turning it into a script that skips files when the output file is newer: