Shell – Mass .flac –> .mp3 transcoding: How to write a shell script that preserves ID3 tag information

flacid3mp3shell-scripttagging

In recent weeks I've gone from a fairly 'hands-on' approach to .flac –> .mp3 transcoding, to one that's far more 'set & forget'.

The first step was to stop using a GUI front end (Audacity with a LAME plug-in) and instead use the method I outlined here.

The second step was to find a bash shell script that would tell that command loop to work recursively, allowing directories with many subdirectories containing .flac files to be transcoded in one simple step. That answer was provided by a user at askubuntu.com.

Now I wish to learn how to further refine things so that ID3 tag information is preserved. The methods linked to above strip ID3 tag data, leaving the bare minimum (i.e. only the title field remains).

Can anyone teach me how to write such a shell script?


The shell script has been updated thus:

#!/bin/bash
file="$1"
flac -cd "$file" | lame --preset fast extreme - "${file%.flac}.mp3"
id3cp "$file" "${file%.flac}.mp3"

Doing find . -name '*.flac' -exec ~/bin/flac2mp3 '{}' \; in ~/Desktop/stack gives the following output:

01 - Amon Tobin - Chomp Samba.flac: done         
LAME 3.98.4 64bits (http://www.mp3dev.org/)
Using polyphase lowpass filter, transition band: 19383 Hz - 19916 Hz
Encoding <stdin> to ./01 - Amon Tobin - Chomp Samba.mp3
Encoding as 44.1 kHz j-stereo MPEG-1 Layer III VBR(q=0)
Parsing ./01 - Amon Tobin - Chomp Samba.flac: done.  Copying to ./01 - Amon Tobin - Chomp Samba.mp3: done

id3info for the original .flac and resultant .mp3 gives, respectively:

*** Tag information for 01 - Amon Tobin - Chomp Samba.flac

(i.e. nothing);

*** Tag information for 01 - Amon Tobin - Chomp Samba.mp3
*** mp3 info
MPEG1/layer III
Bitrate: 128KBps
Frequency: 44KHz

The .flac definitely has tag information. I can verify this by opening up EasyTAG. EasyTAG refers to this as 'FLAC Vorbis Tag' but 'ID3 Tag' for the .mp3. Is this the problem?

Best Answer

#!/bin/sh

file="$1"
outfile=${file%.flac}.mp3

eval $(metaflac --export-tags-to - "$file" | sed "s/=\(.*\)/='\1'/")

flac -cd "$file" | lame --preset fast extreme \
        --add-id3v2 --tt "$TITLE" --ta "$ARTIST" --tl "$ALBUM" \
        --ty "$DATE" --tn "$TRACKNUMBER" --tg "$GENRE" \
        - "$outfile"
Related Question