flac2mp3 conversion bash script

Submit to reddit

I recently started buying some music in flac format. Though I’m content on storing it as flac on my NAS I don’t like filling up my phone’s memory and also I have the impression that listening to flac uses more battery on my n900.

So I devised a small script that converts all the flac’s from a folder to mp3. It has some provisions to carry the metadata from one format to the other. It’s based on the one found here, with the following modifications:

  • it encodes as 320kbps stereo (not joint stereo) There’s a whole debate on whether one can hear or not the quality improvement but it’s still less than half the original flac file so I didn’t really care on finding out if I could go lower. Changing the lame parameters is easy enough if you want different output settings
  • instead of id3 it uses id3tag as it’s more current.
  • Dependencies: metaflac, id3tag, lame (use your distro’s package manager to find out which packages provide those binaries)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
for a in *.flac

do
OUTF=`echo "$a" | sed s/.flac$/.mp3/g`

ARTIST=`metaflac "$a" --show-tag=ARTIST | sed s/.*=//g`
TITLE=`metaflac "$a" --show-tag=TITLE | sed s/.*=//g`
ALBUM=`metaflac "$a" --show-tag=ALBUM | sed s/.*=//g`
GENRE=`metaflac "$a" --show-tag=GENRE | sed s/.*=//g`
TRACKNUMBER=`metaflac "$a" --show-tag=TRACKNUMBER | sed s/.*=//g`
DATE=`metaflac "$a" --show-tag=DATE | sed s/.*=//g`

flac -c -d "$a" | lame -m s -b 320 -s 44.1 - "$OUTF"
id3tag -s"$TITLE" -t"${TRACKNUMBER:-0}" -a"$ARTIST" -A"$ALBUM" -y"$DATE" -g"${GENRE:-12}" "$OUTF"

done
  • Victor_casao

    Thanx for sharing your script. Also, I must say it´s the first time I hear about stereo vs joint stereo, thought I always use FLAC files…
    Now, as I´ve finally got a smartphone , I find ypur article is very useful ¿Could you tell us more  about tnings like stereo vs. joint stere, or pros of using mp3 instaead flac to play music with a samrtphone, and so on?

    (Sorry for my poor english)

  • Hi,

    I prefer mp3 to FLAC on my phone because it uses less cpu and so battery when playing. I don’t know about others but my n900 has a dsp core that decodes mp3 (and other stuff) and thus offloading the applications cpu. Decoding like this is much more efficient energy wise than decoding flacs on the applications cpu.

    Regarding joint vs normal there’s a whole debate and I’m not a specialist. What I know is that:
    – there are lossy and lossless ways of having joint stereo (on lower file sizes it makes sense because jointstereo uses less bits and so leaves more room for improving quality)
    – The LAME encoder is lossless AFAIK in regards to joint stereo

    Basically I didn’t want to bother researching all this and so I always encode at 320kbps normal stereo. It’s basically the maximum quality mp3 can offer. I know that you can get much better file sizes with no audible differences by using vbr, joint stereo, etc. but I have plenty of space everywhere (My phone has 32Gb, my other phone has 16Gb, my NAS is 4Tb and so on, so losing time on finding the perfect settings that spare me 50Mb / album makes no sense to me)

    I’m not “fluent” on all this so if anybody has a better ideea please share your knowledge with us.

    Here are a few links that should get you further than my limited knowledge:

    http://www.hydrogenaudio.org/forums/index.php?showtopic=30637
    http://www.hydrogenaudio.org/forums/index.php?showtopic=12004
    http://en.wikipedia.org/wiki/Joint_Stereo