Saturday, September 03, 2005

Transcoding Ogg to MP3

Wrote a small perl script to convert oggs to mp3s in linux. you need vorbiscomment, lame, and oggdec:

#!/usr/bin/perl

foreach $file (@ARGV) {
if (!($file =~ /\.ogg$/)) {
print "Skipping $file\n";
next;
}
undef $year;
undef $artist;
undef $comment;
undef $album;
undef $title;
undef $genre;
undef $tracknum;

$id3args = "";

$year = `vorbiscomment -l "$file" | grep -i date`;
$year =~ s/^date=//i;
chomp($year);
$id3args = "$id3args --ty $year" if ($year);

$artist = `vorbiscomment -l "$file" | grep -i artist`;
$artist =~ s/^artist=//i;
chomp($artist);
$id3args = "$id3args --ta \"$artist\"" if ($artist);

$comment = `vorbiscomment -l "$file" | grep -i comment`;
$comment =~ s/^comment=//i;
chomp($comment);
$id3args = "$id3args --tc \"$comment\"" if ($comment);

$album = `vorbiscomment -l "$file" | grep -i album`;
$album =~ s/^album=//i;
chomp($album);
$id3args = "$id3args --tl \"$album\"" if ($album);

$title = `vorbiscomment -l "$file" | grep -i title`;
$title =~ s/^title=//i;
chomp($title);
$id3args = "$id3args --tt \"$title\"" if ($title);

$tracknum = `vorbiscomment -l "$file" | grep -i tracknumber`;
$tracknum =~ s/^tracknumber=//i;
chomp($tracknum);
$id3args = "$id3args --tn $tracknum" if ($tracknum);

chomp($year, $artist, $comment, $album, $title, $genre, $tracknum, $id3args);
$tracknum = sprintf("%2.2d", $tracknum);

if (($artist) && ($title) && ($tracknum)) {
$outfile = "$tracknum" . " - " . "$title.mp3";
`oggdec -o - "$file" | lame --add-id3v2 -q 2 -b 192 $id3args - "$outfile"`;

} else {
$outfile = $file;
$outfile =~ s/\.ogg$/.mp3/;
`oggdec -o - "$file" | lame lame --add-id3v2 -q 2 -b 192 $id3args - "$outfile"`;
}
}

0 Comments:

Post a Comment

<< Home