Automator Workflow – Convert FLAC to MP3

automatorencodingfile conversionmp3terminal

I would like to have an Automator Workflow that converts FLAC to Mp3. Right now I am using this shell script which works fine and preserves ID3 Tags:

#!/bin/bash

for f in "$@"; do
    [[ "$f" != *.flac ]] && continue
    album="$(metaflac --show-tag=album "$f" | sed 's/[^=]*=//')"
    artist="$(metaflac --show-tag=artist "$f" | sed 's/[^=]*=//')"
    date="$(metaflac --show-tag=date "$f" | sed 's/[^=]*=//')"
    title="$(metaflac --show-tag=title "$f" | sed 's/[^=]*=//')"
    year="$(metaflac --show-tag=date "$f" | sed 's/[^=]*=//')"
    genre="$(metaflac --show-tag=genre "$f" | sed 's/[^=]*=//')"
    tracknumber="$(metaflac --show-tag=tracknumber "$f" | sed 's/[^=]*=//')"

    flac --decode --stdout "$f" | lame --preset extreme --add-id3v2 --tt "$title" --ta "$artist" --tl "$album" --ty "$year" --tn "$tracknumber" --tg "$genre" - "${f%.flac}.mp3"
done

Unfortunately, if I put this into Automator's "Run Shell Script" it won't work.
First it complained about unknown command for metaflac so I exported my PATH in the script. If I start the workflow now it runs for a second and is done instantly without converting the files. It is set to expect Files and Folders in Finder.

I also found this answer Converting WAV to MP3 using LAME and Automator but this also does not work. I suspect find is not aware of spaces in the path. This also does not preserve ID3 Tags.
Could anyone help me out with this? I am not very experienced with Automator at all. I don't want to use iTunes or other third party Apps. I just need a service which could be used with files or folders.

Best Answer

Found this. Edited it to my needs (no iTunes import, don't delete the files). If I select files it works but not for folders. Pretty nice. A solution where I can select files or folders is still prefered.