Run command only on certain files in subfolders, depending on other files

Posted on

Problem :

I’ve got a folder called Music, that has subfolders of different artist, containing a whole lot of .flac files.
I’m trying to convert all of them into the .wav file format and then convert them to Apple alac, because the direct conversion is not working correctly, but the catch is that I have converted some of them before, but not all of them.

My folder looks basically like this:






Basically I want a command that lets me convert only the .flac files that haven’t got a corresponding .m4a file next to them.
In the past I simply converted all of them again by running this crude for loop:

for f in ./**/*.flac; do ffmpeg -i "$f" "${f%.*}.wav"; done && for f in ./**/*.wav; do ffmpeg -n -i "$f" -acodec alac "${f%.*}.m4a"; done && find . -type f -name '*.wav' -delete

But this is very inefficient for my current setup.

If it is at all possible I would prefer to use ffmpeg.

Solution :

Why not just searching for flac files, check for existing m4a files, and convert if no m4a file exists? Just a little example:

for i in $(find $PWD -name "*.flac")
    # check if file was already converted and therefore a m4a file exists
    if [ ! -e $(dirname ${i})/$(basename ${i} .flac).m4a ]
       # convert your file... File including path is in ${i}
       echo "Need to convert input file ${i} now..."
       WAV=$(dirname ${i})/$(basename ${i} .flac).wav
       M4A=$(dirname ${i})/$(basename ${i} .flac).m4a
       ffmpeg -i ${i} ${WAV}
       ffmpeg -n -i ${WAV} -acodec alac ${M4A}
       rm ${WAV}

Leave a Reply

Your email address will not be published. Required fields are marked *