find "[MP3]"* -name "*.flac" -exec bash -c 'ffmpeg -i "${0}" -ab 320k -c:v copy "${0/.flac/.mp3}" && rm "${0}"' {} \;
I prefer to duplicate my .flac
files before ffmpeg
-ing them all, hence the "[MP3]"*
prefix on
the find
and the rm
at the end. Sometimes, this may be unnecessary, since I might put the MP3
files in their own subdirectory, at which point find .
will work fine. In the case this example is
pulled from, I had two [MP3] folders and two [FLAC] folders in the same directory.
find
"[MP3]"*
-name "*.flac" # Files ending with .flac extension
-exec
bash -c # run a subshell to allow for command expansion. -c = use a command_string
ffmpeg
-i "${0}" # the filename passed from find down to the bash instance
-ab 320k # MP3 output audio quality
-c:v copy # copy the "video" stream (the album art) instead of transcoding it
"${0/.flac/.mp3}" # replace .flac with .mp3 in output filename
&& rm "${0}" # delete the .flac when you're done
\;
Note that this explanation is missing the ''
around the -exec
string. I wanted GitHub Gist to
highlight my comments properly. 😋
Where I ended up finding the final answers to my find -exec
and Bash expansion questions: