Ubuntu – Batch extract audio with avconv without transcoding

.mp412.04aacavconv

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?

Best Answer

You can use a simple for loop:

for i in *.mp4; do
    avconv -i "${i}" -map 0:1 -c:a copy "${i%.mp4}.aac"
done

or on one line:

for i in *.mp4; do avconv -i "${i}" -map 0:1 -c:a copy "${i%.mp4}.aac"; done

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.