Skip to content

Instantly share code, notes, and snippets.

@TJC
Created August 25, 2014 02:02
Show Gist options
  • Save TJC/e041cd0a22d82e2bfe60 to your computer and use it in GitHub Desktop.
Save TJC/e041cd0a22d82e2bfe60 to your computer and use it in GitHub Desktop.
Bulk conversion script from FLAC albums to MP3 with ID3 tags added
#!/usr/bin/env perl
use 5.12.0;
use warnings;
use IPC::Run qw(run);
use autodie;
=head1 NAME
convert_flac_to_mp3.pl
=head1 SYNOPSIS
Bulk conversion of albums from FLAC to MP3, with ID3v2 tags added.
In this script, album directories are in format "$year - $albumTitle" and tracks are
in the format of either:
* "$trackNumber - $title"
* "$trackNumber $title"
=head1 DEPENDENCIES
apt-get install flac lame libipc-run-perl
=cut
our $LAME_PRESET = "extreme";
opendir(my $dir, ".");
my @root_dirs = grep { -d $_ } grep { /^[\w\d]/ } readdir($dir);
closedir $dir;
process_dir($_) for @root_dirs;
sub process_dir {
my $dir = shift;
say "Working on $dir";
my ($year, $album) = $dir =~ /^(\d\d\d\d)\s\-\s(.+)/;
chdir $dir;
opendir(my $filedir, ".");
my @files = grep { /\.flac$/i } readdir($filedir);
closedir $filedir;
process_file($_, $year, $album) for @files;
chdir "..";
}
sub process_file {
my ($file, $year, $album) = @_;
$file =~ /^ (?'track'\d+) (\s\-\s|\s) (?'title'.+) \.flac$/x;
my $outfile = "$+{track} - $+{title}.mp3";
say "encoding $file to $outfile";
unlink "/tmp/temp.wav" if (-e "/tmp/temp.wav");
run('flac', '-d', '-o', "/tmp/temp.wav", $file);
die "Failed to encode $file" unless (-s "/tmp/temp.wav");
run('lame', '--preset', $LAME_PRESET,
"--id3v2-only",
"--tt", $+{title},
"--tn", $+{track},
"--ta", "Sounds from the Ground",
"--ty", $year,
"--tl", $album,
"--tc", "enc with lame preset extreme",
"/tmp/temp.wav", $outfile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment