Note that this will be applied recursively in the given directory. I.e. if you run this from your Music folder, it will convert all flacs from subfolders and produce a .mp3 next to it. You may also do it without ffmpeg by directly using flac and lame (i.e. read w/ flac, pipe to lame, output to a file .mp3), as shown in the link.

You can use -maxdepth 1 I think like in X Tian's answer to limit the recursiveness.
– user44370Oct 24 '14 at 20:00

2

Yes you can. You could also use ls piped to xargs or a for loop. I'm just making the (possibly wrong) assumption that a recursive search is more along what the OP needed (i.e. change all the .flac from my music library).
– Lewis DiamondOct 24 '14 at 20:48

12

This command is great - however; You end up with files named *.flac.mp3. Using your command, I came up with this... find -name "*.flac" -exec bash -c 'ffmpeg -i "{}" -y -acodec libmp3lame -ab 128k "${0/.flac}.mp3"' {} \;
– ShaneOct 27 '15 at 2:54

2

Yes, as I explained in the blog, I like this aspect of adding the .mp3 because it tells me this files comes from a lossless source, which should be found somewhere on my hard drive.
– Lewis DiamondOct 28 '15 at 16:23

I took everything I found here (and maybe on some other sites) and created a small tool to not only create mp3s of flacs recursively, but also preserve relative paths to create them elsewhere with multithread support.

oh, and yes, I see, I didn't use ffmpeg in that case, because my OSMC didn't provide packages for ffmpeg, only avconv, but since you're already here, I guess you know, it's "basically" the same - at least for the most important part. Just replace the command "avconv" with "ffmpeg". My first runs were with the ffmpeg bin and the exact same options.

I am by no means a bash hacker, but I managed it, as my first bashscript with the given demands, and maybe someone will benefit.
I am open for any suggestions from your side, but so far it works for me.

my script to spin up the 4 instances, one for each core, is like this:

#!/bin/bash
# this should be quite self-explanatory
for i in {1..4}
do
echo "started instance no: $i"
/home/osmc/transform.sh . &
# sleeping time can be shorter, this is just so, that
# not all 4 processes will want to start with the same
# file, during runtime collisions should not become an issue
sleep 5
done
echo "all instances started"

And the worker script like this:

#!/bin/bash
# take care of spaces
IFS=$'\n'
# my music folders, remote is the source, local the target dir
remote=/mnt/music/FLAC
local=/mnt/1tb/mp3
# for all flac files start loop
for i in $(find $remote -type f -iname '*.flac' );
do
## SET VARIABLES for PATHS and FILENAMES
## this might be able to be super short with sed and complex one-liner,
## but I s*ck at regex
fullfile=$i
# strip extension
filename="${i##*/}"
# add new extension
filename="${filename%.*}.mp3"
# get full dirname from inputfile
fulldir=$(dirname "${i}")
# strip leading dirs from full input dir
# count the dirs, add two, then you're good.
reldir="$(echo $fulldir | cut -d'/' -f5-)"
# some subdirs in my collection even have a flac subdir, you might
# ignore this, it strips only if it exists
reldir=${reldir//flac}
# combine target dir and relative dir
outdir="$local/$reldir"
# generate the full output filename for conversion
outfile="$outdir/$filename"
# create whole target directory - yes, I need it only once, but hey,
# it works, didn't want to start a if not exist statement... should I?
mkdir -p "$outdir"
# run conversion - finally... you may want or need to replace
# "avconv" with "ffmpeg"
avconv -n -nostats -loglevel info -i "$fullfile" -codec:a libmp3lame -qscale:a 0 "$outfile"
done

It will create a folder named "mp3" inside the one with flac or wav files and, inside the mp3 folder, it will save relative mp3 files with a bitrate of 320kbps, without keeping the old file extension in the name.

I realize this comes quite late, but for memory, see my script "batchaudiocvt" on sourceforge. It is a (quite large) shell script designed for efficient mass conversion of audio files, between many formats. In particular, it makes its best to convert the usual tags.