Ticket #20861: flac2mp3.pl

File flac2mp3.pl, 1.3 KB (added by markus.tujula@…, 15 years ago)
Line 
1#!/opt/local/bin/perl
2#
3# Converts FLAC to MP3 preserving tags
4# License: GPLv2
5# Home: http://www.GuruLabs.com/downloads.html
6#
7# Note: Only use on flac files that you trust. A
8# malicious FLAC/ID3v1 tag could hose you.
9#
10
11use Audio::FLAC::Header;
12
13foreach $file (@ARGV) {
14  if (!($file =~ /\.flac$/)) {
15    print "Skipping $file\n";
16    next;
17  }
18  undef $year; undef $artist; undef $comment; undef $album; undef $title; undef $genre; undef $tracknum;
19  if ($flac = Audio::FLAC::Header->new($file)) {
20        $tag = $flac->tags();
21    $year = $tag->{DATE};
22    $artist = $tag->{ARTIST};
23    $comment = $tag->{COMMENT};
24    $album = $tag->{ALBUM};
25    $title = $tag->{TITLE};
26    $genre = $tag->{GENRE};
27    $tracknum = $tag->{TRACKNUMBER};
28    chomp($year, $artist, $comment, $album, $title, $genre, $tracknum);
29    $tracknum = sprintf("%2.2d", $tracknum);
30  } else {
31    print "Couldn't get flac tags for $file.\n";
32  } 
33  if (($artist) && ($title) && ($tracknum)) {
34     $outfile = "$tracknum" . "_-_" . "$title.mp3";
35         `flac -c -d "$file" | lame --alt-preset standard --ty $year --ta "$artist" --tc "$comment" --tl "$album" --tt "$title" --tg "$genre" --tn $tracknum - "$outfile"`;
36  } else {
37    $outfile = $file;
38    $outfile =~ s/\.flac$/.mp3/;
39        `flac -c -d "$file" | lame --alt-preset standard - "$outfile"`;
40  }
41}