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 | if ($TRACKS) { |
---|
32 | foreach (split /,/, $TRACKS) { |
---|
33 | if (/^(\d+)$/) { |
---|
34 | push @track_numbers, $1; |
---|
35 | } elsif (/^(\d+)-(\d+)$/) { |
---|
36 | push @track_numbers, ($1 .. $2); |
---|
37 | } elsif (/^-(\d+)$/) { |
---|
38 | push @track_numbers, (1 .. $1); |
---|
39 | } elsif (/^(\d+)-$/) { |
---|
40 | push @track_numbers, ($1 .. $track_count); |
---|
41 | } |
---|
42 | } |
---|
43 | } else { |
---|
44 | @track_numbers = (1 .. $track_count); |
---|
45 | } |
---|
46 | |
---|
47 | for my $i (@track_numbers) { |
---|
48 | my $audio_file = shift or die "No more audio files to process\n"; |
---|
49 | my $mp3 = MP3::Tag->new($audio_file); |
---|
50 | |
---|
51 | # first clear the tags if asked |
---|
52 | if ($CLEAR) { |
---|
53 | warn "clearing first"; |
---|
54 | $mp3->delete_tag($_) foreach qw{ID3v1 ID3v2}; |
---|
55 | } |
---|
56 | |
---|
57 | my $track_key = sprintf "TRACK%02d", $i; |
---|
58 | my $track = $metadata->{$track_key}; |
---|
59 | #TODO: for reissues, what should the year/date be tagged as? |
---|
60 | my ($year) = ($metadata->{ORIGINALDATE} || $metadata->{DATE} || '' =~ /^(\d{4})/); |
---|
61 | $mp3->title_set($track->{TITLE}); |
---|
62 | $mp3->artist_set($track->{ARTIST} || $metadata->{ALBUMARTIST}); |
---|
63 | $mp3->album_set($track->{ALBUM} || $metadata->{ALBUM}); |
---|
64 | $mp3->year_set($year) if $year; |
---|
65 | $mp3->track_set($i); |
---|
66 | $mp3->update_tags; |
---|
67 | printf " Apply to %s\n", $audio_file; |
---|
68 | |
---|
69 | #TODO: full version of a make-string-filename-safe function |
---|
70 | my $sort_filename = lc $track->{ARTISTSORT}; |
---|
71 | for ($sort_filename) { |
---|
72 | s/&/and/g; |
---|
73 | s/\W/-/g; |
---|
74 | s/--/-/g; |
---|
75 | } |
---|
76 | print "Artist sort filename would be: " . $sort_filename . "\n"; |
---|
77 | } |
---|