]> diplodocus.org Git - flac-archive/blob - tags.pl
c65a2763a3470485f5a04288a94d550dd782d901
[flac-archive] / tags.pl
1 package epg::flac::archive::tags;
2
3 =head1 NAME tags.pl
4
5 subroutines for tag processing
6
7 =head1 DESCRIPTION
8
9 TODO
10
11 =cut
12
13 # POSIX.1-2017, Base Definitions, 3.282 Portable Filename Character Set says
14 # [A-Za-z0-9._-]
15 # https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_282
16
17 # Flac tags (set with `flac -T fieldname=...`) are Vorbis comments.
18 # https://www.xiph.org/vorbis/doc/v-comment.html#fieldnames field names we care about:
19 # - TITLE - Track/Work name
20 # - VERSION - may be used to differentiate multiple versions of the same track title in a single collection. (e.g. remix info)
21 # - ALBUM - The collection name to which this track belongs
22 # - TRACKNUMBER - The track number of this piece
23 # - ARTIST - The artist generally considered responsible for the track
24 # - DATE - Date the track was recorded (XXX I use US release date)
25 # https://age.hobba.nl/audio/mirroredpages/ogg-tagging.html (supposedly mirrored from http://reactor-core.org/ogg-tagging.html )
26 # specifies more:
27 # - DISCNUMBER - if part of a multi-disc album, put the disc number here
28 # - VERSION - e.g. "live", "radio edit"
29 # - PARTNUMBER - part number if a work is divided across tracks
30 # - PART - part name e.g. "Oh sole mio"
31 # https://picard-docs.musicbrainz.org/en/appendices/tag_mapping.html
32 # - ALBUMARTIST - maps to ID3v2 TPE2
33
34 # Input is a directory containing:
35 # - trackNN.cdda.wav - WAV format files ripped from CD-DA audio tracks where NN is track number 01 - 99
36 # - tags - described below
37 # - cover.front.jpeg - optional JPEG format file containing the album front cover
38
39 # The tags file is composed of two blocks:
40 # 1. album tags
41 # 2. track tags
42 # Album tags may be:
43 # - ALBUM
44 # - ARTIST
45 # - DATE
46 # - DISCNUMBER (optional)
47
48 # Track tags may be any of the rest of the tags listed above, suffixed with
49 # [N] where N is the track number. ARTIST, and only ARTIST, may also appear
50 # in the track tags. In this case, it overrides the album artist. In order
51 # to add artists, the album artist must be listed again. For example, Reba
52 # McEntire's "The Heart Won't Lie" on the album "It's Your Call" features
53 # Vince Gill, and is specified as:
54 # ARTIST=Reba McEntire
55 # ALBUM=It's Your Call
56 # TITLE[5]=The Heart Won't Lie
57 # ARTIST[5]=Reba McEntire
58 # ARTIST[5]=Vince Gill
59
60 use v5.12;
61 use warnings;
62
63 use Exporter 'import';
64
65 our @EXPORT_OK = qw[
66 read_tags
67 read_tags_metaflac
68 disc_tags
69 track_tags
70 track_tags_from_disc
71
72 mangle_for_file_name
73 quote
74 two_digits
75 ];
76
77 sub two_digits {
78 my $n = shift;
79 my $s = sprintf('%02d', $n);
80 if (length($s) != 2) {
81 die("$n won't fit into two digits")
82 }
83 $s
84 }
85
86 sub read_tags {
87 my $fh = shift;
88 my %album;
89 my @tracks;
90 my $tracknum = 0;
91 my $name;
92 while (<$fh>) {
93 chomp;
94 if (!s/^([^=[\]]+)(\[(\d+)])?=//) {
95 if (defined($name) && $name eq 'DESCRIPTION') {
96 # Qobuz offers FLAC files like this.
97 warn("looks like DESCRIPTION had multiple lines; discarding and returning early: $_");
98 last
99 }
100 die("no field name in: $_");
101 }
102 $name = $1;
103 # TODO validate $name
104 # TODO album tags should be illegal after track tags
105 if (defined($3)) {
106 if ($3 == $tracknum + 2) {
107 $tracknum++;
108 } elsif ($3 != $tracknum + 1) {
109 $tracknum++; # increment from 0 to 1 for error message
110 die("illegal track number jump from $tracknum to $3")
111 }
112 push(@{$tracks[$tracknum]->{$name}}, $_);
113 } else {
114 push(@{$album{$name}}, $_);
115 }
116 }
117
118 \%album, \@tracks
119 }
120
121 sub read_tags_metaflac {
122 my $fn = shift;
123 open(my $fh, '-|', 'metaflac', '--no-utf8-convert', '--export-tags-to=-', $fn) || die("metaflac: $!");
124 my @result = read_tags($fh);
125 if (!close($fh)) {
126 if ($! == 0) {
127 die("metaflac exited $?")
128 }
129 die("close(metaflac): $!")
130 }
131 @result
132 }
133
134 sub one_or_undef {
135 my $name = shift;
136 my $tags = shift;
137 my $tag = $tags->{$name};
138 if (defined($tag) && @$tag > 0) {
139 return $tag->[0]
140 }
141 undef
142 }
143
144 sub one {
145 my $tag = one_or_undef(@_);
146 if (!defined($tag)) {
147 my $name = shift;
148 die("exactly one $name tag required")
149 }
150 $tag
151 }
152
153 =over
154
155 =item B<disc_tags>
156
157 Interpret the tags hash for a disc, returning:
158
159 =over
160
161 =item *
162 Artist tag (exactly one required in the hash).
163
164 =item *
165 Album tag (exactly one required).
166
167 =item *
168 Date tag (zero or one required).
169
170 =item *
171 Disc number (zero or one required) as a reference to an array of size 0 or 1.
172
173 =back
174 =cut
175 sub disc_tags {
176 my %tags = @_;
177
178 if (!defined($tags{ALBUM}) || @{$tags{ALBUM}} != 1) {
179 die('exactly one ALBUM tag required')
180 }
181 my $album_tag = $tags{ALBUM}->[0];
182
183 # TODO ALBUMARTIST
184 if (!defined($tags{ARTIST}) || @{$tags{ARTIST}} != 1) {
185 die('exactly one ARTIST tag required')
186 }
187 my $artist = $tags{ARTIST}->[0];
188
189 my $date;
190 if (defined($tags{DATE})) {
191 if (@{$tags{DATE}} != 1) {
192 die('one or zero DATE tags required')
193 }
194 $date = $tags{DATE}->[0];
195 }
196
197 my @discnumber = ();
198 if (defined($tags{DISCNUMBER})) {
199 if (@{$tags{DISCNUMBER}} != 1) {
200 die('one or zero DISCNUMBER tags required')
201 }
202 my ($discnum) = @{$tags{DISCNUMBER}};
203 @discnumber = ($discnum);
204 # But check it against DISCTOTAL if present.
205 my $total = $tags{DISCTOTAL};
206 if (defined($total) && @$total > 0) {
207 my ($total) = @$total;
208 if ($total < $discnum) {
209 die("DISCNUMBER=$discnum > DISCTOTAL=$total")
210 }
211 if ($total == 1) {
212 # Qobuz offers FLAC files like this.
213 @discnumber = ();
214 }
215 }
216 }
217
218 $artist, $album_tag, $date, \@discnumber
219 }
220
221 # TODO dedup with flac2mp3
222 sub track_tags {
223 my $tags = shift;
224 # TODO probably ought to return hash too. then track_tags would augment
225 my ($artist, $album, $date, $discnumber) = disc_tags(%$tags);
226
227 # TODO resurrect whole-disc FLAC?
228 # Stupid hack: only a single-track file should have the
229 # TRACKNUMBER tag, so use it if set for the first pass through
230 # the loop. At the end of the loop, we'll set $track for the
231 # next run, so this continues to work for multi-track files.
232 # if track == None:
233 # track = 1
234 # else:
235 # track = int(track)
236
237 my $tracknumber = epg::flac::archive::tags::one(TRACKNUMBER => $tags);
238 my $title = epg::flac::archive::tags::one(TITLE => $tags);
239 # TODO restore PARTNUMBER and VERSION next time i need them
240
241 (
242 artist => $artist,
243 album => $album,
244 date => $date,
245 discnumber => $discnumber,
246 tracknumber => $tracknumber,
247 title => $title,
248 )
249 }
250
251 # TODO confusing relationship with track_tags which is wrong
252 sub track_tags_from_disc {
253 my ($album, $tracks, $tracknumber) = @_;
254 my $trackcount = @$tracks;
255 if ($trackcount < $tracknumber) {
256 die("requested track $tracknumber out of $trackcount tracks");
257 }
258 # Listing the track tags second makes them override the album tags.
259 %$album, %{$tracks->[$tracknumber - 1]}
260 }
261
262 sub mangle_for_file_name {
263 my $fn = shift;
264 $fn =~ s/[!,.?]//g; # discard these punctuation marks
265 $fn =~ s/\s+/_/g; # replace all whitespace with _
266 $fn =~ s/[^A-Za-z0-9_]/-/g; # everything else with -
267 $fn =~ s/_[_-]/_/g; # collapse repeated _ or - to _
268 $fn =~ s/-[_-]/_/g; # more
269 $fn =~ s/^[_-]+//; # discard any _ or - at beginning
270 $fn =~ s/[_-]+$//; # at end too
271 $fn
272 }
273
274 sub quote {
275 my $s = shift;
276 $s =~ s/'/'\\''/g;
277 "'$s'"
278 }
279
280 =back
281
282 =cut
283
284 1;