]> diplodocus.org Git - flac-archive/blob - tags.pl
25c7d3afb9fe74a772bd72ee28100ec1c5dc2e82
[flac-archive] / tags.pl
1 package epg::flac::archive::tags;
2
3 # POSIX.1-2017, Base Definitions, 3.282 Portable Filename Character Set says
4 # [A-Za-z0-9._-]
5 # https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_282
6
7 # Flac tags (set with `flac -T fieldname=...`) are Vorbis comments.
8 # https://www.xiph.org/vorbis/doc/v-comment.html#fieldnames field names we care about:
9 # - TITLE - Track/Work name
10 # - VERSION - may be used to differentiate multiple versions of the same track title in a single collection. (e.g. remix info)
11 # - ALBUM - The collection name to which this track belongs
12 # - TRACKNUMBER - The track number of this piece
13 # - ARTIST - The artist generally considered responsible for the track
14 # - DATE - Date the track was recorded (XXX I use US release date)
15 # https://age.hobba.nl/audio/mirroredpages/ogg-tagging.html (supposedly mirrored from http://reactor-core.org/ogg-tagging.html )
16 # specifies more:
17 # - DISCNUMBER - if part of a multi-disc album, put the disc number here
18 # - VERSION - e.g. "live", "radio edit"
19 # - PARTNUMBER - part number if a work is divided across tracks
20 # - PART - part name e.g. "Oh sole mio"
21 # https://picard-docs.musicbrainz.org/en/appendices/tag_mapping.html
22 # - ALBUMARTIST - maps to ID3v2 TPE2
23
24 # Input is a directory containing:
25 # - trackNN.cdda.wav - WAV format files ripped from CD-DA audio tracks where NN is track number 01 - 99
26 # - tags - described below
27 # - cover.front.jpeg - optional JPEG format file containing the album front cover
28
29 # The tags file is composed of two blocks:
30 # 1. album tags
31 # 2. track tags
32 # Album tags may be:
33 # - ALBUM
34 # - ARTIST
35 # - DATE
36 # - DISCNUMBER (optional)
37
38 # Track tags may be any of the rest of the tags listed above, suffixed with
39 # [N] where N is the track number. ARTIST, and only ARTIST, may also appear
40 # in the track tags. In this case, it overrides the album artist. In order
41 # to add artists, the album artist must be listed again. For example, Reba
42 # McEntire's "The Heart Won't Lie" on the album "It's Your Call" features
43 # Vince Gill, and is specified as:
44 # ARTIST=Reba McEntire
45 # ALBUM=It's Your Call
46 # TITLE[5]=The Heart Won't Lie
47 # ARTIST[5]=Reba McEntire
48 # ARTIST[5]=Vince Gill
49
50 use v5.12;
51 use warnings;
52
53 use Exporter 'import';
54
55 our @EXPORT_OK = qw[
56 disc_tags
57 read_tags
58 mangle_for_file_name
59 quote
60 ];
61
62 sub read_tags {
63 my $fh = shift;
64 my %album;
65 my @tracks;
66 my $tracknum = 0;
67 while (<$fh>) {
68 chomp;
69 s/^([^=[\]]+)(\[(\d+)])?=// || die("no field name in $_");
70 my $name = $1;
71 # TODO validate $name
72 # TODO album tags should be illegal after track tags
73 if (defined($3)) {
74 if ($3 == $tracknum + 2) {
75 $tracknum++;
76 } elsif ($3 != $tracknum + 1) {
77 $tracknum++; # increment from 0 to 1 for error message
78 die("illegal track number jump from $tracknum to $3")
79 }
80 push(@{$tracks[$tracknum]->{$name}}, $_);
81 } else {
82 push(@{$album{$name}}, $_);
83 }
84 }
85
86 \%album, \@tracks
87 }
88
89 sub one_or_undef {
90 my $name = shift;
91 my $tags = shift;
92 my $tag = $tags->{$name};
93 if (defined($tag) && @$tag > 0) {
94 return $tag->[0]
95 }
96 undef
97 }
98
99 sub one {
100 my $tag = one_or_undef(@_);
101 if (!defined($tag)) {
102 my $name = shift;
103 die("exactly one $name tag required")
104 }
105 $tag
106 }
107
108 sub disc_tags {
109 my $album = shift;
110
111 if (!defined($album->{ALBUM}) || @{$album->{ALBUM}} != 1) {
112 die('exactly one ALBUM tag required')
113 }
114 my $album_tag = $album->{ALBUM}->[0];
115
116 # TODO ALBUMARTIST
117 if (!defined($album->{ARTIST}) || @{$album->{ARTIST}} != 1) {
118 die('exactly one ARTIST tag required')
119 }
120 my $artist = $album->{ARTIST}->[0];
121
122 my $date;
123 if (defined($album->{DATE})) {
124 if (@{$album->{DATE}} != 1) {
125 die('one or zero DATE tags required')
126 }
127 $date = $album->{DATE}->[0];
128 }
129
130 my @discnumber = ();
131 if (defined($album->{DISCNUMBER})) {
132 if (@{$album->{DISCNUMBER}} != 1) {
133 die('one or zero DISCNUMBER tags required')
134 }
135 @discnumber = ($album->{DISCNUMBER}->[0]);
136 }
137
138 $album_tag, $artist, $date, \@discnumber
139 }
140
141 sub mangle_for_file_name {
142 my $fn = shift;
143 $fn =~ s/[!,.?]//g; # discard these punctuation marks
144 $fn =~ s/\s+/_/g; # replace all whitespace with _
145 $fn =~ s/[^A-Za-z0-9_]/-/g; # everything else with -
146 $fn =~ s/_[_-]/_/g; # collapse repeated _ or - to _
147 $fn =~ s/-[_-]/_/g; # more
148 $fn =~ s/^[_-]+//; # discard any _ or - at beginning
149 $fn =~ s/[_-]+$//; # at end too
150 $fn
151 }
152
153 sub quote {
154 my $s = shift;
155 $s =~ s/'/'\\''/g;
156 "'$s'"
157 }
158
159 1;