If you want to also loop in subfolders, you can use the globstar
shell optional behavior, see the Pattern Matching section of the reference manual and the Shopt Builtin section of the reference manual as so:
shopt -s globstar
for f in **/*.m4a; do ffmpeg -i "$f" -acodec libmp3lame -ab 320 "${f%.m4a}.mp3"; done
Using find
it's a bit trickier since you're using a Shell Parameter Expansion. Here's a possibility that will be 100% safe regarding files with spaces or other funny symbols in their name:
find . -name '*.m4a' -type f -exec bash -c 'ffmpeg -i "$0" -acodec libmp3lame -ab 320 "${0%.m4a}.mp3"' {} \;
This second possibility might be faster if you have a huge number of files, since bash globbing is known to be quite slow for huge number of files.
In the -exec
statement of find
, I'm using bash -c '...'
. In this case, every parameter given after the string to be executed will be set as the positional parameters, indexed from 0
, hence the $0
that appears in the code
ffmpeg -i "$0" -acodec libmp3lame -ab 320 "${0%.m4a}.mp3"
Hope this helps!