Converting Audio Files with GStreamer
I've written before about ripping audio CD's from the command line (part 1, part 2, part 3, part 4). Certainly, if you're just ripping a new CD, then just convert the audio to your chosen format at that time. But maybe you've been archiving your music in FLAC format or you purchased some high bit-rate MP3's for download and now you want them in a different format or bit-rate for your music player.
GStreamer is an open source multimedia framework that's used by many GNOME applications and a few KDE apps as well. It's really meant to be used by programmers to create rich applications by accessing GStreamer's API. Thankfully there's also a command line interface available for us non-programmers. It's really meant for only testing, but that's OK. If it works, it works.
Now you may be asking yourself why you'd want to use GStreamer to convert audio when there are so many other choices available. Well, one really cool thing about GStreamer is that the metadata is preserved through the conversion process. That means you won't have to go through and re-tag your files after converting.
GStreamer works with a system of plugins to handle different media formats. If you have trouble with a given media format, it's probably because you don't have the proper plugins installed. In Ubuntu, the plugins are in packages with names like: gstreamer0.10-plugins-good, gstreamer0.10-plugins-bad, gstreamer0.10-plugins-ugly, etc. I'm assuming you already have GStreamer installed on your system as well as any plugins you will need. To get a list of plugins installed on your system, use:
You can use GStreamer to decode an audio file and send it to your sound server. You can do this with the gst-launch command along with the playbin plugin. The playbin plugin will auto-detect which plugins need to be used to convert the audio and send it to your sound server. You use it like this:
That's the simplest form for playing audio. Now GStreamer strings together calls to different plugins in what it refers to as a pipeline. This is very similar to typical command line pipelines, but it uses the exclamation mark (!) as the pipeline symbol instead of the typical Linux pipe symbol (|). This is how you would play a FLAC file with GStreamer. This assumes you are already in the directory containing your file.
The above line locates your FLAC file, sends it to the FLAC decoder, converts it to raw audio, and sends the raw audio to the Pulse Audio Server.
OK, so now we finally get to the audio conversion. You've already seen how the GStreamer pipelines go together, so now we'll use one to convert from one format to another. So, let's say that you archived your music in the FLAC format and now you want to convert it to Ogg Vorbis to put on your portable media player.
So you can see that this command took your FLAC file and decoded it, converted it to raw, encoded it to Vorbis, and placed it in an Ogg container, and finally wrote it to file.ogg.
If you want to control the quality level of the Ogg file, you can specify it like this.
The above command will convert your file to an Ogg Vorbis quality level of 5. While Ogg quality levels normally range from -1 to 10 including decimal values, the vorbisenc plugin specifies the quality values ranging from -0.1 to 1. It's the same thing, just called out differently.
So there you have it. You can learn more about GStreamer by looking at the man pages for gst-launch and gst-inspect. There's also a nice FAQ on using GStreamer. You can also learn more about the various GStreamer plugins.
See my next post if you're interesting in converting FLAC to MP3.