source: flacrip/trunk/applymeta @ 20

Last change on this file since 20 was 20, checked in by peter, 10 years ago

applymeta now takes an optional list of track numbers instead of always using all of the tracks from the properties file

  • Property svn:executable set to *
File size: 2.0 KB
Line 
1#!/usr/bin/perl -w
2use strict;
3
4use Getopt::Long;
5use Config::Properties;
6use YAML;
7use MP3::Tag;
8
9GetOptions(
10    'file|f=s'   => \my $META_FILE,
11    'clear'      => \my $CLEAR,
12    'tracks|t=s' => \my $TRACKS,
13);
14
15my $meta;
16
17if ($META_FILE eq '-') {
18    $meta = Config::Properties->new;
19    $meta->load(*STDIN);
20} else {
21    $meta = Config::Properties->new(
22        file => $META_FILE,
23    );
24}
25
26my $metadata = $meta->splitToTree;
27
28my $track_count = scalar grep { /^TRACK/ } keys %{ $metadata };
29
30my @track_numbers;
31foreach (split /,/, $TRACKS) {
32    if (/^(\d+)$/) {
33        push @track_numbers, $1;
34    } elsif (/^(\d+)-(\d+)$/) {
35        push @track_numbers, ($1 .. $2);
36    } elsif (/^-(\d+)$/) {
37        push @track_numbers, (1 .. $1);
38    } elsif (/^(\d+)-$/) {
39        push @track_numbers, ($1 .. $track_count);
40    }
41}
42
43for my $i (@track_numbers) {
44    my $audio_file = shift or die "No more audio files to process\n";
45    my $mp3 = MP3::Tag->new($audio_file);
46
47    # first clear the tags if asked
48    if ($CLEAR) {
49        warn "clearing first";
50        $mp3->delete_tag($_) foreach qw{ID3v1 ID3v2};
51    }
52
53    my $track_key = sprintf "TRACK%02d", $i;
54    my $track = $metadata->{$track_key};
55    #TODO: for reissues, what should the year/date be tagged as?
56    my ($year) = ($metadata->{ORIGINALDATE} || $metadata->{DATE} || '' =~ /^(\d{4})/);
57    $mp3->title_set($track->{TITLE});
58    $mp3->artist_set($track->{ARTIST} || $metadata->{ALBUMARTIST});
59    $mp3->album_set($track->{ALBUM}  || $metadata->{ALBUM});
60    $mp3->year_set($year) if $year;
61    $mp3->track_set($i);
62    $mp3->select_id3v2_frame_by_descr('TPE2', $metadata->{ALBUMARTIST}) if $metadata->{ALBUMARTIST};
63    $mp3->update_tags;
64    printf "    Apply to %s\n", $audio_file;
65
66    #TODO: full version of a make-string-filename-safe function
67    my $sort_filename = lc $track->{ARTISTSORT};
68    for ($sort_filename) {
69        s/&/and/g;
70        s/\W/-/g;
71        s/--/-/g;
72    }
73    print "Artist sort filename would be: " . $sort_filename . "\n";
74}
Note: See TracBrowser for help on using the repository browser.