Home > Operating Systems > .flac to .mp3 parallel conversion script

.flac to .mp3 parallel conversion script

I’ve been using FLAC to backup my CDs for some time, but due to the ubiquity of MP3, I frequently found myself transcoding a large number of these files for various devices.

I found a good number of scripts[1] for doing the conversion, and it seems to be  a bike shed everyone wants to chime in on.  What I found didn’t really handle modern multi-core processors, so I made some quick tweaks and thought I’d post my results.  There are two scripts:

mp3xcode:

#!/bin/bash

mkdir mp3
parallel mp3xcode_sub — *.flac

mp3xcode_sub:

#!/bin/bash

[ -r “$1″ ] || { echo can not read file \”$1\” >&1 ; exit 1 ; } ;

FLAC=$1
MP3=”mp3/${FLAC%.flac}.mp3″

eval `metaflac –export-tags-to=- “$FLAC” | sed ‘s/=\(.*\)/=”\1″/’`

flac -dc “$FLAC” | lame –replaygain-accurate -v -V 2 –tt “$TITLE” \
–tn “$TRACKNUMBER” \
–tg “$GENRE” \
–ty “$DATE” \
–ta “$ARTIST” \
–tl “$ALBUM” \
–add-id3v2 \
– “$MP3”

This script seems to be a good way to do it. I’ve only tested this on linux.

On FreeBSD, there may be some tweaks required to not depend on bash.  Also, you’ll need to install the GNU parallel program (sysutils/parallel).

  1. lydgate
    October 23rd, 2012 at 13:11 | #1

    I know this is an old post but I found this useful to save a bit of time over doing it serially. Only issue I had was that some flac tags on my music have different capitalization, so e.g. $ARTIST would come up blank because metaflac was dumping $artist. Metaflac itself isn’t case sensitive though, so I had better luck replacing that eval line with the following:

    ARTIST=`metaflac “$FLAC” –show-tag=ARTIST | sed s/.*=//g`
    TITLE=`metaflac “$FLAC” –show-tag=TITLE | sed s/.*=//g`
    ALBUM=`metaflac “$FLAC” –show-tag=ALBUM | sed s/.*=//g`
    GENRE=`metaflac “$FLAC” –show-tag=GENRE | sed s/.*=//g`
    TRACKNUMBER=`metaflac “$FLAC” –show-tag=TRACKNUMBER | sed s/.*=//g`
    DATE=`metaflac “$FLAC” –show-tag=DATE | sed s/.*=//g`

    Bit hackish but it works on all the flacs I’ve tried so far.

  2. November 15th, 2013 at 11:24 | #2

    @lydgate
    I just ran into this myself, and updated the mp3xcode_sub to:

    #!/bin/bash
    
    FLAC=$1
    MP3="mp3/${FLAC%.flac}.mp3"
    
    [ -r "$FLAC" ] || { echo can not read file \"$FLAC\" >&1 ; exit 1 ; } ;
    eval `metaflac --export-tags-to=- "$FLAC" | sed 's/^\([^=].*\)=\(.*\)/\U\1\E="\2";/'`;
    
    flac -dc "$FLAC" | lame --replaygain-accurate -v -V 2 --tt "$TITLE" \
    --tn "$TRACKNUMBER" \
    --tg "$GENRE" \
    --ty "$DATE" \
    --ta "$ARTIST" \
    --tl "$ALBUM" \
    --add-id3v2 \
    - "$MP3"
    

    The sed regex forces capitalization, with GNU sed.

  1. No trackbacks yet.