Sunday, February 13, 2011

Flac to Ogg Converter (Bash Shell Script)

My recently gathered collections of anime and Japanese musics are mostly FLACs. If you haven't heard of it, it's a free and open source encoding for storing the exact data from a WAV audio file (this is the one you get directly from ripping an audio CD - CMIIW, the data dump from an audio CD) at half of its original size. This means, you can get the equal quality of an audio CD track with only half of the original space.

Of course, if you have FLAC files, it doesn't mean you could enjoy a crispy bright playback of an audio CD. There are several more factors, they are, your sound card, your amplifiers and speakers. As for me, with only on board sound card and a Sony Ericsson standard Walkman phone headset, it's meaningless to play FLAC tracks on my laptop. It just a waste of precious harddisk space. But nonetheless, I still spare them for keepsake, in case someday I could afford a better audio system.

So, the solution for a time being is to burn my FLAC collections onto DVDs, then ripping them into OGG format. This tutorial on Archlinux Wiki provides the basic script, I made some slight modification to it so that the final result would be OGG instead of MP3. This script will work on any UNIX system with bash shell on it (including Cygwin).

First, you need to provide all the dependencies. You will need these binaries (refer to your distro guide on how to install them).
flac oggenc
Then, copy the following script to your text editor, save it as anything you feel like (the convention is not to give any extension to the filename, as this is an executable , and normally executables don't have any extension). In this example, I save it as flac2ogg.
#!/bin/bash

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`

echo "$a"

flac -c -d "$a" | oggenc -q 8 -a "$ARTIST" -G "$GENRE" -t "$TITLE" -d "$DATE" -n "$TRACKNUMBER" -l "$ALBUM" - -o "$OUTF"

done
The next step, as a superuser, make it executable.
$ sudo chmod 0775 flac2ogg
Still as a superuser, copy it to the local binary directory, /usr/local/bin.
$ sudo cp flac2ogg /usr/local/bin
The final step, as a normal user, add /usr/local/bin into PATH environment variable. To make it handy, we will need to modify the .bashrc file so that/usr/local/bin is always in PATH every time you log in. Now you can call the script from anywhere.
$ export PATH=$PATH:/usr/local/bin
Add the same line above to your .bashrc.

To use it, navigate to a directory (in a terminal, of course) where you place your FLAC files you want to convert. Simply type the script name, the terminal will show the encoding progress. The final OGG files will have the name and tags from the original FLAC files.

Now you can manually remove the unused FLAC files.
rm -v *.flac
Adding this line will get your script to automatically remove the original FLAC right after it has been encoded to OGG. Put it before the very last line (before done).
...
rm -v "$a"
done

PS:

The blogger editor fails so hard, either in HTML or WYSIWYG mode. Srsly, they need to fix it up. It always complains errors when I add images, and it also messing up the 'quote' command. I wonder why they always said blogger is better than wordpress.

Edit:
PHAIL. It cannot change the font into fixed-width font (Courier). blogger.com sucks.

No comments:

Post a Comment