Random Post: VLAN Bridging in Xen
RSS .92| RSS 2.0| ATOM 0.3
  • Home
  •  

    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.

    #!/usr/bin/perl

    $lame_opt = "–preset extreme";

    foreach (@ARGV) {
    if (!($_ =~ /\.flac$/)) {
    print "Skipping $_\n";
    next;
    }

    `flac -d "$_"`;
    $_ =~ s/\.flac$/.wav/;
    $target = $_;
    $target =~ s/\.wav$/.mp3/;
    `lame $lame_opt "$_" "$target"`;
    `rm "$_"`;
    }

    4 Responses to “flac2mp3: batch flac to mp3 converter”

    1. Kerry Says:

      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.

    2. Waldmeister Says:
      #!/usr/bin/perl
      #
      # 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";

    3. Waldmeister Says:

      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.

    4. curts Says:

      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.

      #!/usr/bin/perl
      #
      # 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;

    Leave a Reply