1 | #!/usr/bin/perl -w |
---|
2 | use strict; |
---|
3 | |
---|
4 | use Getopt::Long; |
---|
5 | use Config::Properties; |
---|
6 | use YAML; |
---|
7 | use MP3::Tag; |
---|
8 | |
---|
9 | GetOptions( |
---|
10 | 'file|f=s' => \my $META_FILE, |
---|
11 | 'clear' => \my $CLEAR, |
---|
12 | 'tracks|t=s' => \my $TRACKS, |
---|
13 | ); |
---|
14 | |
---|
15 | my $meta; |
---|
16 | |
---|
17 | if ($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 | |
---|
26 | my $metadata = $meta->splitToTree; |
---|
27 | |
---|
28 | my $track_count = scalar grep { /^TRACK/ } keys %{ $metadata }; |
---|
29 | |
---|
30 | my @track_numbers; |
---|
31 | foreach (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 | |
---|
43 | for 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 | } |
---|