flac2mp3: batch flac to mp3 converter
Here is a quick script I whipped together to convert flac files to mp3. Its nothing fancy but it does the job. Doesnt require anything but flac and lame.
I was going to include a bunch of fancy id3 tag stuff but to be honest most of the id3 perl libraries out there arent that great to use. I may do something like that in the future though. Since I use quod libet which has a fantastic tagger built in, it isnt really an issue for me.
February 13th, 2008 at 10:51 pm
For windows users, I’ve created a vbscript that allows for reflacing files using the latest version of the encoder, Decode to WAV, encode from WAV, or re-encode as MP3, copying the tags from the FLAC to MP3. You can also use the script to synchronize your FLAC library to either WAV or MP3.
The code is available on Google Code under the Reflacer project.
May 9th, 2008 at 6:18 pm
#
# added some code to get id3 info in your mp3s
# PROGRAMME
$flac=flac;
$metaflac=metaflac;
$lame=lame;
# OPTIONEN
$lame_opt="–preset extreme –add-id3v2 –tc \"Waldmeisters Musiktruhe\" ";
$flacinfo="flacinfo.txt";
print "*** flac2mp3 – Converts .flac to .mp3\n";
foreach (@ARGV) {
print "\n\n*** Now processing ", $_, "\n";
if (!($_ =~ /\.flac$/)) {
print "Skipping $_\n";
next;
}
# getting the meta data (title, interpret, etc.)
system("$metaflac –export-tags-to=$flacinfo $_");
open ( IN, "$flacinfo" ) or die "No such file: $flacinfo\n";
$i = 0;
while ( $line = ) {
chomp $line;
($metainfo[$i],$metainfo[$i 1]) = split (m/=/, $line);
$i =2;
}
for( $i = 0; $i < @metainfo; $i ) {
if( $metainfo[$i] =~ /ARTIST/ ) { $ARTIST = $metainfo[$i 1]; }
elsif( $metainfo[$i] =~ /ALBUM/ ) { $ALBUM= $metainfo[$i 1]; }
elsif( $metainfo[$i] =~ /YEAR/ ) { $YEAR= $metainfo[$i 1]; }
elsif( $metainfo[$i] =~ /TITLE/ ) { $TITLE= $metainfo[$i 1]; }
elsif( $metainfo[$i] =~ /TRACKNo/ ) { $TRACKNo= $metainfo[$i 1]; }
}
# Metainfo for lame
$LAMEmeta="–tt \"$TITLE\" –ta \"$ARTIST\" –tl \"$ALBUM\" –tn $TRACKNo –ty $YEAR ";
# now converting follows
`$flac -d "$_"`;
$_ =~ s/\.flac$/.wav/;
$target = $_;
$target =~ s/\.wav$/.mp3/;
`$lame $lame_opt $LAMEmeta "$_" "$target"`;
`rm "$_"`;
}
`rm $flacinfo`;
print "\n";
May 9th, 2008 at 6:22 pm
Some funny things happen while using Provoxy:
open ( IN, “$flacinfo” ) or die “No such file: $flacinfo\n”;
It should read _o p e n_, not PrivoxyWindowOpen.
July 27th, 2008 at 5:52 pm
Using Waldmeister’s Perl script as a starting point, I’ve streamlined it, made it more robust, made some corrections for Ubuntu, and added some features I needed. I hope others will find it useful.
#
# added some code to get id3 info in your mp3s
use Getopt::Std;
my $opt_string = ‘ho:p:’;
getopts( "$opt_string", \%opt ) or usage();
usage() if $opt{h};
$opt{o} = "." if ( $opt{o} eq "" );
sub usage() {
print STDERR << "EOF";
usage: $0 [-h] [-o directory] [-p prefix] file.flac [file.flac …]
-h help (this message)
-o directory optional destination for MP3 files
-p prefix optional text to prepend to each MP3 file
EOF
exit;
}
if ( ! -d $opt{o} ) {
print "’$opt{o}’ is not a valid directory for output, using current directory…\n";
$opt{o} = ".";
}
# PROGRAMME
$flac=flac;
$metaflac=metaflac;
$lame=lame;
# OPTIONEN
$lame_opt="–preset extreme -V2 –add-id3v2 –tc \"Curtis Schroeder\" ";
$flacinfo="flacinfo.txt";
print "*** flac2mp3 — Converts .flac to .mp3\n";
foreach (@ARGV) {
print "\n\n*** Now processing ", $_, "\n";
if (!($_ =~ /\.flac$/)) {
print "Skipping $_\n";
next;
}
# Convert spaces in filename to underscores as flac can’t handle spaces
if ( $_ =~ /\ /) {
$file = $_;
$file =~ s/ /_/g;
system("mv ‘$_’ ‘$file’");
}
else { $file = $_ }
# getting the meta data (title, album, etc.)
system("$metaflac –export-tags-to=$flacinfo ‘$file’");
open ( $info, "$flacinfo" ) or die "No such file: $flacinfo\n";
my @metainfo;
while ( ! eof $info ) {
$line = ;
chomp $line;
@metainfo = split /=/, $line;
if ( $metainfo[0] =~ /ARTIST/i ) { $ARTIST = $metainfo[1]; }
elsif( $metainfo[0] =~ /ALBUM/i ) { $ALBUM = $metainfo[1]; }
elsif( $metainfo[0] =~ /DATE/i ) { $YEAR = $metainfo[1]; }
elsif( $metainfo[0] =~ /TITLE/i ) { $TITLE = $metainfo[1]; }
elsif( $metainfo[0] =~ /TRACKN/i ) { $TRACKNo = $metainfo[1]; }
}
# Remove leading zero from track number
$TRACKNo =~ s/0(\d)/$1/;
# Metainfo for lame
$LAMEmeta="–tt \"$TITLE\" –ta \"$ARTIST\" –tl \"$ALBUM\" –tn $TRACKNo –ty $YEAR ";
# Now converting follows
system("$flac -d ‘$file’");
$file =~ s/\.flac$/.wav/;
$target = $file;
$target =~ s/\.wav$/.mp3/;
system("$lame $lame_opt $LAMEmeta ‘$file’ ‘$opt{o}/$opt{p}$target’");
unlink $file;
print "\n";
}
unlink $flacinfo;