public
Last active

  • Download Gist
gistfile1
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
#!/ports/bin/perl
# -*- Mode: Perl; tab-width: 4; indent-tabs-mode: nil; -*-
 
use Modern::Perl;
 
use CDDB;
use Config::Std { def_sep => '=' };
use FileHandle;
use Getopt::Std;
use Readonly;
use Term::Menu;
 
 
# TODO
# * need examples of 'Various Artists' data from Freedb to implement
# multiple artists code
# * validate option r, provide menu when multiple rips can be restarted
# * bash completion for -r
 
# use Data::Dumper;
 
$| = 1;
 
Readonly my $LEAD_OUT_TRACK => 162;
Readonly my $DETAILS_FILE => 'details.conf';
Readonly my $WORKING_DIRECTORY => $ENV{'RIP_TMP'}
// "${ENV{'HOME'}}/Music/rips";
Readonly my $EDITOR => $ENV{'RIP_EDITOR'}
// $ENV{'VISUAL'}
// $ENV{'EDITOR'}
// 'vi';
Readonly my %DEFAULT_TRACK_TAGS => (
title => '',
);
Readonly my %DEFAULT_ALBUM_TAGS => (
compilation => 'false',
album => '',
artist => '',
year => '1970',
disk => '1/1',
);
 
 
my %option;
getopts( 'dr:t', \%option );
 
# clean up afterwards
if ( defined $option{'d'} && defined $option{'r'} ) {
system "/bin/rm -rf ${WORKING_DIRECTORY}/${option{'r'}}";
exit;
}
 
if ( !defined $option{'r'} ) {
# if we aren't resuming an existing rip, first fetch the CD details
# from freedb and write them to a file
my $cddb_details = get_disk_cddb_details();
$option{'r'} = $cddb_details->{'discid'};
say '-> ' . $option{'r'};
# create the working directory, save the details to a file and allow
# the user to edit the details
mkdir "${WORKING_DIRECTORY}/${option{'r'}}"
or die "mkdir: $!";
chdir "${WORKING_DIRECTORY}/${option{'r'}}";
write_details_to_config( $cddb_details );
open_conversion_directory();
system "${EDITOR} ${DETAILS_FILE}";
# after ripping the tracks eject the CD, as we are done with it
rip_tracks_from_cd( $cddb_details );
system "drutil eject";
# confirm before continuing to convert the files
print "
Before continuing, ensure you have:
 
* made any required modifications to the source audio
(splitting tracks, etc.)
* entered the correct titles, artists, years, etc. into the
description file
* scanned the cover image and saved it as 'cover.jpg'
 
Pressing [^C] now will interrupt this process. You can always return
to the job later with the command 'rip -r ${option{'r'}}'.
";
my $continue = <>;
}
else {
chdir "${WORKING_DIRECTORY}/${option{'r'}}"
or die "chdir: $!";
}
 
read_config $DETAILS_FILE, my %config;
 
if ( !defined $option{'t'} ) {
convert_tracks_to_aac( \%config );
relocate_wav_files( \%config );
}
 
tag_converted_tracks( \%config );
 
print "
Done. You can delete the directory once the rip has been confirmed with
the command: 'rip -dr ${option{'r'}}'.
";
exit;
 
 
 
sub get_disk_cddb_details {
my $cddbp = CDDB->new()
or die $!;
 
# get the disk information
my $disk;
my @disk_toc = calculate_disk_toc();
my @disk_info = $cddbp->calculate_id( @disk_toc );
my @disks = $cddbp->get_discs(
$disk_info['0'],
$disk_info['3'],
$disk_info['4']
);
# choose from multiple matches:
if ( 0 < $#disks ) {
my $count = 0;
my %menu;
foreach my $option ( @disks ) {
my $id = $option->['1'];
my $title = $option->['2'];
my $menu = chr( ord('a') + $count );
$menu{ $count } = [ $title, $menu ];
$count++;
}
my $options = Term::Menu->new(
beforetext => 'Multiple matches - choose from the following:',
aftertext => '',
tries => 0,
);
my $answer = $options->menu( %menu );
 
$disk = $disks[ $answer ];
}
# single or zero matches:
else {
$disk = pop @disks;
}
my $result = $cddbp->get_disc_details( $disk->['0'], $disk->['1'] );
# if no data was retrieved, create a dummy entry
if ( !defined $result ) {
$result = {
discid => $disk_info['0'],
dtitle => 'Unknown Artist / Unknown Album'
};
foreach my $track ( @{ $disk_info['1'] } ) {
push @{ $result->{'ttitles'} }, "Track $track";
}
}
 
return $result;
}
sub write_details_to_config {
my $details = shift;
my %config;
my( $artist, $title ) = extract_info_from_title( $details->{'dtitle'} );
foreach my $tag ( keys %DEFAULT_ALBUM_TAGS ) {
$config{''}{ $tag } = $DEFAULT_ALBUM_TAGS{ $tag };
}
$config{''}{'artist'} = $artist;
$config{''}{'album'} = $title;
$config{''}{'year'} = $details->{'dyear'}
if defined $details->{'dyear'};
$config{''}{'genre'} = $details->{'genre'}
if defined $details->{'genre'};
my $various_artists = is_various_artists( $details );
if ( $various_artists ) {
$config{''}{'artist'} = "Various Artists";
$config{''}{'original_artist'} = $artist;
}
my $current_track = 1;
foreach my $track ( @{ $details->{'ttitles'} } ) {
my $track_number = sprintf '%02d', $current_track;
foreach my $tag ( keys %DEFAULT_TRACK_TAGS ) {
$config{ $track_number }{ $tag } = $DEFAULT_TRACK_TAGS{ $tag };
}
if ( $various_artists ) {
my ( $artist, $title ) = get_artist_and_title( $track );
$config{ $track_number }{'artist'} = $artist;
$config{ $track_number }{'title'} = $title;
}
else {
$config{ $track_number }{'title'} = $track;
}
$current_track++;
}
write_config %config, $DETAILS_FILE;
# massage the default output of Config::Std a little
{
local $/;
my $handle = FileHandle->new( $DETAILS_FILE, 'r' );
my $contents = <$handle>;
undef $handle;
# remove extraneous blank lines
$contents =~ s{\n\n}{\n}gs;
# blank lines before tracks
$contents =~ s{\n\[}{\n\n[}gs;
# add a comment to the first track
my $comment = "# tracks can also over-ride options, such as:\n"
. "# artist = <track artist>\n"
. "# genre = <track genre>\n"
. "# year = <track year>\n";
$contents =~ s{^(.*?\n)\[}{${1}${comment}\[}s;
$handle = FileHandle->new( $DETAILS_FILE, 'w' );
print {$handle} $contents;
}
}
sub extract_info_from_title {
my $text = shift;
my $artist_title_split_regexp = qr{
^
\s*
(.*) # artist
[ ] [/] [ ]
(.*) # album title
\s*
$
}x;
if ( $text =~ $artist_title_split_regexp ) {
return $1, $2;
}
return $text, '';
}
sub is_various_artists {
my $details = shift;
# check each track
foreach my $track ( @{ $details->{'ttitles'} } ) {
my ( $artist, $track ) = get_artist_and_title( $track );
if ( !defined $artist ) {
return 0;
}
}
return 1;
}
sub get_artist_and_title {
my $track = shift;
my $split_by_slash = qr{
^
(?<title> .* )
\s+
/
\s+
(?<artist> .* )
$
}x;
if ( $track =~ $split_by_slash ) {
return( $+{'artist'}, $+{'title'} );
}
return( undef, $track );
}
 
 
sub rip_tracks_from_cd {
my $details = shift;
my $number_of_tracks = $#{ $details->{'ttitles'} } + 1;
print "\nRIPPING:\n";
foreach my $track ( 1 .. $number_of_tracks ) {
print "$track. " . $details->{'ttitles'}->[$track-1] . "\n";
system "cdparanoia -qP ${track} ${track}.wav";
}
}
sub convert_tracks_to_aac {
my $config = shift;
my $tracks = count_tracks( $config );
print "CONVERTING: ";
# convert them to AAC
foreach my $track ( 1 .. $tracks ) {
# faac is giving weird echoes?
# system "faac -wQ --mpeg-vers 4 -q 500 ${track}.wav";
system "afconvert -f m4af -d aac -s 3 -u vbrq 127 ${track}.wav";
print ".";
}
print "\n";
}
sub tag_converted_tracks {
my $details = shift;
print "TAGGING: ";
foreach my $track ( 1 .. count_tracks( $details ) ) {
# remove artwork, otherwise it is not properly replaced
my $command_line = '--artwork REMOVE_ALL';
system "atomic-parsley ${track}.m4a ${command_line} >/dev/null";
# apply new metadata
$command_line = get_atomic_parsley_options( $details, $track );
system "atomic-parsley ${track}.m4a ${command_line} >/dev/null";
print ".";
}
print "\n";
}
sub open_conversion_directory {
system "open -a Finder .";
}
sub relocate_wav_files {
my $details = shift;
print "RELOCATING WAVs: ";
mkdir 'wavs';
my $number_of_tracks = ( scalar keys %{ $details } )- 1;
foreach my $track ( 1 .. $number_of_tracks ) {
rename "${track}.wav", "wavs/${track}.wav"
or die "Cannot rename: $!";
print ".";
}
print "\n";
}
 
sub get_atomic_parsley_options {
my $details = shift;
my $track = shift;
my $track_number = sprintf '%02d', $track;
my %options;
my $tracks = count_tracks( $details );
my $command_line = "--overWrite --tracknum ${track}/${tracks} ";
# album-wide options
foreach my $option ( keys %{ $details->{''} } ) {
if ( 'artist' eq $option ) {
$options{'albumArtist'} = $details->{''}{ $option };
$options{'artist'} = $details->{''}{ $option };
}
else {
$options{ $option } = $details->{''}{ $option };
}
}
# over-ride with per-track options
foreach my $option ( keys %{ $details->{ $track_number } } ) {
$options{ $option } = $details->{ $track_number }{ $option };
}
# add options to command line string
foreach my $option ( keys %options ) {
$command_line .= qq{--$option "${options{ $option }}" };
}
# add artwork if it exists
if ( -f "cover.jpg" ) {
$command_line .= "--artwork cover.jpg";
}
return $command_line;
}
 
sub calculate_disk_toc {
my @toc;
my $lead_out;
my $current_track = 1;
my $handle = FileHandle->new( "cdparanoia --query 2>&1 |" )
or die "$!";
while ( my $line = <$handle> ) {
my $current_track_regexp = qr{
^
track_num [ ] = [ ] ${current_track} [ ]
start [ ] sector [ ] \d+ [ ] msf: [ ]
(\d+),
(\d+),
(\d+)
}x;
my $lead_out_track_regexp = qr{
^
track_num [ ] = [ ] ${LEAD_OUT_TRACK} [ ]
start [ ] sector [ ] \d+ [ ] msf: [ ]
(\d+),
(\d+),
(\d+)
}x;
if ( $line =~ $current_track_regexp ) {
push @toc, "$current_track $1 $2 $3";
$current_track++;
}
elsif ( $line =~ $lead_out_track_regexp ) {
$lead_out = "999 $1 $2 $3";
}
}
push @toc, $lead_out;
return @toc;
}
sub count_tracks {
my $config = shift;
# count the tracks based upon config entries only
my $tracks = 0;
foreach my $key ( keys %{ $config } ) {
next if '' eq $key;
$tracks++;
}
return $tracks;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.