[Nottingham] Converting mp3 to ogg

Thomas Preece thomas at tpreece.net
Sat Aug 15 17:23:41 UTC 2009


2009/8/15 Simon Sleaford <simon.sleaford at gmail.com>:
> I'm happy enough that I'm not losing too much quality wise by converting
> from one format to another

I made that assumption myself not that long ago - unfortunately it's
not quite as straightforward as that though.

It's not just a loss of quality, but you also get some rather...
interesting artifacts when converting from one non-linear (ie, lossy)
format to another. You might not notice it for the first conversion,
but consider that Ogg Vorbis won't be the format of choice forever,
and the more times you do a conversion between formats, you'll get
some new artifacts each time and eventually it'll sound horrible.

There are several ways to solve this problem; the best is not to use a
lossy format in the first place: I store all my music as Flac files as
far as possible - they're quite a bit bigger than lossy files but disk
space is cheap compared to the possibility of having to eventually
re-buy your entire collection because it's unlistenable.

If that's not an option, you should first look to see how much of your
collection you can re-encode from a lossless source (eg, a CD); if
that's not an option then you should definitely keep a copy of the
original MP3 files, so that when Vorbis goes out of fashion you can
re-encode into the new format from your MP3s, rather than having an
extra step in the chain.

> Can anybody recommend a program that will do a folder of files or even crawl
> my music folder and convert everything while I leave it to it? Preferably
> looking for something that will leave the originals intact in case of any
> hiccups before I've checked them out.

I recently had to do a similar thing for a web app I'm working on to
manage a radio station's music (it all has to be in Vorbis for various
reasons, but a minority of our files come in as MP3s); I used the
following (PHP) code to convert an MP3 to Ogg Vorbis:

function mp3_to_ogg($infile, $outfile, $quality=4) {
	$in = escapeshellarg($infile);
	$out = escapeshellarg($outfile);
	$q = (int)($quality);
	
	passthru("lame --decode $in - | oggenc -q $q -o $out -");
}

I also made sure to move the source MP3 files into a backup directory,
and it works fine.

Obviously you won't need the PHP parts, but you can grab the shell
part fairly easily; if you do this from the shell:

$ lame --decode </path/to/input/file.mp3> - | oggenc -q 4 -o
</path.to/output/file.ogg> -

that will do the conversion for you without any problems.

You can then combine that with Alex's code to get the following:

for in in `find /path/to/input/files | grep .mp3$`; do
	out=`echo $f | sed -e 's/.mp3$/.ogg/`
	lame --decode $f - | oggenc -q 4 -o $out -
done

The only caveat with this script is that it assumes that you have no
spaces in your filenames - if you do then it will end up it trying to
process each word as a separate file, which will obviously fail. I
can't quite remember the way around this; I think when I tried it I
eventually gave up and wrote a Perl script instead!!

You can probably then use a similar bit of shell script to pick out
all of the ogg files from the tree, or just go through and grab them
by hand.

Hope this helps,
  Thomas.



More information about the Nottingham mailing list