Batch extract audio with avconv without transcoding

Solution 1:

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.

Solution 2:

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.

find . -type f -name '*.mp4' -exec bash -c 'avconv -i "$0" -c:a copy "${0/%mp4/m4a}"' {} \;

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.