So you have a bunch of FLAC files for whatever reason, but if you’re like me, it’s because you are an audiophile and prefer a lossless codec for files you play on your high-end stereo equipment. However, FLAC files are huge, which can make them impractical for small personal music players with limited storage. This is especially true if you’re just going to plug the player into a cheap-ass stereo (like the one in my base model Toyota RAV4, for example.) That’s a bit like hunting squirrels with hand-grenades: overkill. Your best bet is to convert your FLAC files to something smaller and more manageable (and sounds better than MP3!): OGG/Vorbis.
Let’s get started. We’ll assume your FLAC files are tagged with the artist name, song title, etc. (Maybe you previously ripped them with Rubyripper?) The little script below will do the following:
- Find all the FLAC files in the current directory
- Extract relevant id3 tag data (ignoring Comments, if there are any)
- Convert 01. Some Song.flac to 01. Some Song.ogg at 320kbps and apply the id3 tag data from the FLAC file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
for a in *.flac do OUTF=`echo "$a" | sed s/\.flac$/.ogg/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` oggenc -q9 --discard-comments -a "$ARTIST" -t "$TITLE" -l "$ALBUM" -G "GENRE" -N "$TRACKNUMBER" -d "$DATE" -o "$OUTF" "$a" done |
Save this as a file such as flac2ogg and give it chmod 755. Put it in a directory that contains FLAC files you want to convert and execute it. This results in the following (example) output:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
$ ./flac2ogg Opening with flac module: FLAC file reader Encoding "01. Take Back the City.flac" to "01. Take Back the City.ogg" at quality 9.00 [ 99.8%] [ 0m00s remaining] \ Done encoding file "01. Take Back the City.ogg" File length: 4m 42.0s Elapsed time: 0m 09.6s Rate: 29.5266 Average bitrate: 338.0 kb/s Opening with flac module: FLAC file reader Encoding "02. Shut Your Eyes.flac" to "02. Shut Your Eyes.ogg" at quality 9.00 [ 99.7%] [ 0m00s remaining] / Done encoding file "02. Shut Your Eyes.ogg" File length: 3m 17.0s Elapsed time: 0m 06.8s Rate: 29.2893 Average bitrate: 321.5 kb/s ... |
And the resulting properties page for the new OGG files look like this:

If you want to run the script from any directory on your machine without the need to copy it every time, just put it somewhere on your path. For example, on my Arch Linux box:
|
1 |
# cp flac2ogg /usr/local/bin |
Of course, there are other ways to get this on your $PATH; read your distribution’s help for specifics.
Another thing to note is that the script sets the quality to -q9, which yields 320kbps. If this is still overkill for your needs, try -q6; that will give you 192kbps. This yields a smaller file size and most folks can’t hear the difference in the quality.
A slight variation
With just a minor tweak or two, the script can also recursively scan for FLAC files starting with the current directory, and then put the resulting OGG files in a tidy directory structure:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#!/bin/bash # Recursively find FLAC files starting in current directory find -name *.flac | while read FLACFILE do # Grab the id3 tags from the FLAC file ARTIST=`metaflac "$FLACFILE" --show-tag=ARTIST | sed s/.*=//g` TITLE=`metaflac "$FLACFILE" --show-tag=TITLE | sed s/.*=//g` ALBUM=`metaflac "$FLACFILE" --show-tag=ALBUM | sed s/.*=//g` GENRE=`metaflac "$FLACFILE" --show-tag=GENRE | sed s/.*=//g` TRACKNUMBER=`metaflac "$FLACFILE" --show-tag=TRACKNUMBER | sed s/.*=//g` DATE=`metaflac "$FLACFILE" --show-tag=DATE | sed s/.*=//g` # A little shell globbing to get the filename # (everything after the rightmost "/") FILENAME=`echo ${FLACFILE##*/}` # Build the output path and rename the file OGGFILE=`echo "/home/$USER/Music/Ogg/$ARTIST/$ALBUM/$FILENAME" | sed s/\.flac$/.ogg/g` # Convert to OGG at 320kbps (use -q6 for 192kbps) oggenc -q9 --discard-comments -a "$ARTIST" -t "$TITLE" -l "$ALBUM" -G "GENRE" -N "$TRACKNUMBER" -d "$DATE" -o "$OGGFILE" "$FLACFILE" done |
This will spit out the OGG files to a structure like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/home/ricky/Music/Ogg ├── Artist One │ ├── Album One │ │ ├── 01 - Some Track.ogg │ │ ├── 02 - Some Other Track.ogg │ │ └── Etc. │ ├── Album Two │ │ ├── 01 - Some Track.ogg │ │ ├── 02 - Some Other Track.ogg │ │ └── Etc. │ └── Album Three │ ├── 01 - Some Track.ogg │ ├── 02 - Some Other Track.ogg │ └── Etc. └── Artist Two └── Album One ├── 01 - Some Track.ogg ├── 02 - Some Other Track.ogg └── Etc. |
If you don’t care about free file formats
Well, you should. But maybe you’re a subservient little lamb and you paid The New Evil Empire for an iDevice to play your music. If that’s the case, you probably want MP3 files. Ba-a-a-a, little lamb. Ba-a-a-a.
Use the lame encoder in place of oggenc. Modify the script to do something like this:
|
1 |
flac -c -d "$a" | lame --noreplaygain -V0 --add-id3v2 --pad-id3v2 --ignore-tag-errors --tt "$TITLE" --tn "${TRACKNUMBER:-0}" --ta "$ARTIST" --tl "$ALBUM" --ty "$DATE" --tg "${GENRE:-12}" - "$OUTF" |
Then, read up on why you should consider moving away from MP3: http://www.fsf.org/campaigns/playogg/en/.
Cheers!
