Changeset 4 in flacrip


Ignore:
Timestamp:
07/26/12 21:33:59 (12 years ago)
Author:
peter
Message:
  • the MusicBrainz XML parsing returns the actual strings instead of XML::XPath::Literal elements
  • flactrack takes an --all parameter that transcodes all of the tracks for a particular flac file into a directory (directory name defaults to the same as the flac filename, but without the ".flac" extension)
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/MusicBrainz.pm

    r3 r4  
    4747    my $release_count = $xpath->findvalue('count(//release)'); 
    4848    my ($release) = $xpath->findnodes('//release[1]'); 
    49     $info{RELEASE_MBID} = $xpath->findvalue('@id', $release); 
    50     $info{ALBUM}        = $xpath->findvalue('title', $release); 
    51     $info{ARTIST}       = $xpath->findvalue('artist-credit/name-credit/artist/name', $release); 
     49    $info{RELEASE_MBID} = $xpath->findvalue('@id', $release)->value; 
     50    $info{ALBUM}        = $xpath->findvalue('title', $release)->value; 
     51    $info{ARTIST}       = $xpath->findvalue('artist-credit/name-credit/artist/name', $release)->value; 
    5252 
    5353    # TODO: get release date 
     
    5858        my $prefix = sprintf('TRACK%02d', $tracknum); 
    5959        #$info{"$prefix.MB_TRACKID"} = $xpath->findvalue('@id', $track_node); 
    60         my $recording_mbid = $info{"$prefix.RECORDING_MBID"} = $xpath->findvalue('recording/@id', $track_node); 
    61         $info{"$prefix.TITLE"}          = $xpath->findvalue('recording/title', $track_node); 
    62         $info{"$prefix.ARTIST"}         = $xpath->findvalue('recording/artist-credit/name-credit/artist/name', $track_node) || $info{ARTIST}; 
     60        my $recording_mbid = $info{"$prefix.RECORDING_MBID"} = $xpath->findvalue('recording/@id', $track_node)->value; 
     61        $info{"$prefix.TITLE"}          = $xpath->findvalue('recording/title', $track_node)->value; 
     62        $info{"$prefix.ARTIST"}         = $xpath->findvalue('recording/artist-credit/name-credit/artist/name', $track_node)->value || $info{ARTIST}; 
    6363        $info{TRACKS}[$tracknum]{TITLE} = $info{"$prefix.TITLE"}; 
    6464        $info{TRACKS}[$tracknum]{ARTIST} = $info{"$prefix.ARTIST"}; 
  • trunk/flactrack

    r3 r4  
    77# TODO: separate sox filter for each track! 
    88 
    9 use Getopt::Long; 
     9use Getopt::Long qw{:config no_ignore_case}; 
     10use File::Spec::Functions qw{catfile}; 
     11use File::Path; 
     12use Audio::FLAC::Header; 
    1013use MusicBrainz; 
    1114 
     
    1417    't=s' => \my $TYPE, 
    1518    'x=s' => \my $SOX_FILTER, 
     19    'all|a' => \my $ALL, 
     20    'dir|d=s' => \my $DIRECTORY, 
    1621); 
    1722 
     
    2126$TYPE ||= 'mp3'; 
    2227 
     28# default the directory to be named like the flac file 
     29($DIRECTORY ||= $FLAC_FILE) =~ s/\.flac$//; 
     30 
     31my $flac = Audio::FLAC::Header->new($FLAC_FILE) or die "Can't read FLAC header from $FLAC_FILE\n"; 
     32 
    2333# for getting track metadata from MusicBrainz 
    2434my $info; 
    25 if ($TYPE eq 'mp3') { 
    26     require Audio::FLAC::Header; 
    27     require LWP; 
    28     require XML::XPath; 
    29     require XML::XPath::XMLParser; 
    30  
    31     my $flac = Audio::FLAC::Header->new($FLAC_FILE) or warn "Can't read FLAC header from $FLAC_FILE\n"; 
     35if ($ALL || $TYPE eq 'mp3') { 
    3236    my $discid = $flac->tags('MBZ_DISCID') or warn "No MBZ_DISCID tag in $FLAC_FILE\n" if $flac; 
    3337    #TODO: calculate TOC and DISCID from cuesheet if there is no MBZ_DISCID tag present 
    3438 
     39    #TODO: also get the year from MBZ 
    3540    $info = get_musicbrainz_info($discid); 
     41} 
     42 
     43if ($ALL) { 
     44    die "Use of --all requires a --directory\n" unless $DIRECTORY; 
     45    use YAML; 
     46    my $cuesheet = $flac->cuesheet; 
     47    my $count = scalar grep { /TRACK \d\d/ } @{ $flac->cuesheet }; 
     48    print "Found $count tracks\n"; 
     49    #TODO: default to just 01, 02, etc. if there is no $info 
     50    %TRACKS = map { 
     51        $_ => catfile($DIRECTORY, sprintf('%02d.%s', $_, to_filename($info->{TRACKS}[$_]{TITLE}))) 
     52    } (1 .. $count); 
     53    print Dump(\%TRACKS); 
     54    mkpath($DIRECTORY); 
    3655} 
    3756 
     
    7998    return qq{"$string"}; 
    8099} 
     100 
     101sub to_filename { 
     102    my @strings = @_; 
     103    #TODO: deal with non-ascii characters (unidecode) e.g. ü --> ue 
     104    return map { 
     105        s/[^a-z0-9-_ ]+//gi; 
     106        s/ +/_/g; 
     107        lc; 
     108    } @strings; 
     109} 
Note: See TracChangeset for help on using the changeset viewer.