A Few Thoughts on Encoding

The following article is a brief summary of different approaches of music file encoding I’ve used and a few thoughts about how to put them to use. Various encoding utilities such as lame, oggenc and flac are used throughout this article.

Turning a FLAC collection into MP3 with ease

When looping over hundreds or thousands of files find is an excellent utility to assure the desired procedure is being done to each file. Manually constructions using for i in $(find… tend to break because of whitespaces and other annoying things. Therefore the easiest and most elegant thing to do is using either xargs or the -exec option to find to call a script to do the encoding job.

An example of such a script:

#!/bin/bash
flac -cd "${1}" | lame -b "192" - "$(basename "${1}" .flac).mp3"

Typical usage would be:

find . -name "*.flac" -exec sh flac2mp3.sh {} ;

That would encode all FLAC files in the current directory and its subdirectories to MP3 (note that the usage of basename will cause the encoded MP3 to be created in the current working directory).

Download flac2mp3.sh here

Same thing but with OGG

If you would like to encode into OGG instead of MP3 then you could use the following script:

#!/bin/bash
flac -cd "${1}" | oggenc -b "192" -o "$(basename "${1}" .flac).ogg" -

Typical usage would be:

find . -name "*.flac" -exec sh flac2ogg.sh {} ;

Download flac2ogg.sh here

Re-encoding MP3 into OGG

If you already have a MP3 collection and for some reason want to re-encode it into OGG then the following method would be suitable. The approach uses the same procedure as we’ve used before. A script for doing the actual encoding would look something like this:

#!/bin/bash
lame --decode "${1}" - | oggenc -b "192" -o "$(basename "${1}" .mp3).ogg" -

Example usage in this case:

find . -name "*.mp3" -exec sh mp32ogg.sh {} ;

Download mp32ogg.sh here

Super efficient FLAC to MP3 encoding using mp3fs

While the traditional piping method is efficient enough in most cases there might be times when you need something more efficient. One way to do very efficient and elegant FLAC to MP3 encoding is a FUSE filesystem called mp3fs. Basically this allows you to “mount” a directory full of FLAC files and access the music through the mounted directory. When the file is read from the mp3fs mounted directory the file will be re-encoded into MP3 during the read making it an excellent tool for re-encoding larger FLAC libraries.

Note that all meta data will be preserved throughout the conversion process.

Getting mp3fs up and running on Ubuntu

Since mp3fs isn’t in the main repositories for now we have to build it ourselves. We start off by installing the dependencies and utilities required for building it:

sudo apt-get install build-essential libid3tag0-dev libfuse-dev libflac-dev liblame-dev libogg-dev fuse-utils

The latest version of mp3fs is available from their Sourceforge project page. Download it and continue the installation by unpacking it and running the appropriate build commands (change the version number to match the one you’re actually downloading, of course):

tar xvzf mp3fs-0.06.tar.gz
cd mp3fs-0.06
./configure --prefix=/usr/local/
make
sudo make install

Using the FUSE filesystem for encoding

Now let’s use mp3fs to convert our FLAC library to MP3. If the FLAC library is located in /storage/music/flac then we could create our directory to use for mp3fs at /storage/music/convert. We want our MP3 files to end up in /storage/music/mp3. To sum up:

  • /storage/music/flac - Location of our FLAC library
  • /storage/music/convert - Temporary directory to use as a mountpoint for mp3fs
  • /storage/music/mp3 - Our to-be-created MP3 library

The following command would mount the FUSE filesystem for us:

sudo mkdir /storage/music/convert
sudo mp3fs /storage/music/flac/ 192 /storage/music/convert/

Change the bitrate to something else if you like. Now it’s time to go through with the actual re-encoding:

sudo mkdir /storage/music/mp3
sudo rsync -av /storage/music/convert/. /storage/music/mp3/.

The reason to use rsync for the file transfer is because that allows us to easily resume the encoding process later on (you might want to encode a few files then turn of your computer and continue the next day, for example)

2 Responses to “A Few Thoughts on Encoding”

  1. Johan Hoeke Says:

    Thanks for the re-encode stuff. Just what i was looking for. Just getting started with the FUSE stuff as well. Interesting stuff.

    cheers,
    Johan

  2. ben Says:

    Dear sir,(hope you will understand my poor english:})

    i have been looking everyhwere on the net and cannot find a single person able to help me,even the freelancers do not bid on my problem!!!.
    i have given my ecommerce for an upgrade to a web designer group.the problem is what i am asking isn’t their domaine at all they are really lost on the music encoding process(i am lost too because i dont do any programming).
    hope you can help,if you can’t then please forget my message.

    here is the problem:

    the webdesign company asked me if i could help them finding the right script for my project because they cannot find it themself.

    my website will be an unsigned artist mp3 download site.
    the artist will create an account and then upload his tracks onto a page of my site.the problem is the programmers cannot find a script(the web is constructed in PHP)that could encode the uploaded music into a “safe” format.what i mean by safe is if someone clicks on the “listen “button he will get this encoded version ,then,if he likes the music and would like to purchase it then he will press “buy”and get the proper mp3 version.
    now the questions are these ones:
    on an mp3 site,would the site toake the uploaded mp3 and then encode a copy of it into another file or will a stream player encode the music while it is getting played?ther programmers just need a script that can be attached to the musics and will encode them or something else that can do the same job

    please help,i am lost ,noone seems to be able to help me,i am on my own……

    thank you very much

    ben

Leave a Reply