]> diplodocus.org Git - flac-archive/blob - fa-encode
c18b1bc18a5e9a5e0df39e327403eb13ef3dd89e
[flac-archive] / fa-encode
1 #!/usr/local/bin/perl
2
3 =head1 NAME
4
5 B<fa-encode> - encode WAV files to FLAC
6
7 =head1 SYNOPSIS
8
9 B<fa-encode> [B<-d> I<artist-directory>] B<input-directory>
10
11 I<artist-directory> defaults to the album B<ARTIST> in the tags file; one of
12 these is required.
13
14 TODO: implement B<-d>
15
16 =cut
17
18 # POSIX.1-2017, Base Definitions, 3.282 Portable Filename Character Set says
19 # [A-Za-z0-9._-]
20 # https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_282
21
22 # Flac tags (set with `flac -T fieldname=...`) are Vorbis comments.
23 # https://www.xiph.org/vorbis/doc/v-comment.html#fieldnames field names we care about:
24 # - TITLE - Track/Work name
25 # - VERSION - may be used to differentiate multiple versions of the same track title in a single collection. (e.g. remix info)
26 # - ALBUM - The collection name to which this track belongs
27 # - TRACKNUMBER - The track number of this piece
28 # - ARTIST - The artist generally considered responsible for the track
29 # - DATE - Date the track was recorded (XXX I use US release date)
30 # I use one more, though I'm unsure where I got it. Did I make it up?
31 # - DISCNUMBER - number in multi-disc collection
32
33 # Input is a directory containing:
34 # - trackNN.cdda.wav - WAV format files ripped from CD-DA audio tracks where NN is track number 01 - 99
35 # - tags - described below
36 # - cover.front.jpeg - optional JPEG format file containing the album front cover
37
38 # The tags file is composed of two blocks:
39 # 1. album tags
40 # 2. track tags
41 # Album tags may be:
42 # - ALBUM
43 # - ARTIST
44 # - DATE
45 # - DISCNUMBER (optional)
46
47 # Track tags may be any of the rest of the tags listed above, suffixed with
48 # [N] where N is the track number. ARTIST, and only ARTIST, may also appear
49 # in the track tags. In this case, it overrides the album artist. In order
50 # to add artists, the album artist must be listed again. For example, Reba
51 # McEntire's "The Heart Won't Lie" on the album "It's Your Call" features
52 # Vince Gill, and is specified as:
53 # ARTIST=Reba McEntire
54 # ALBUM=It's Your Call
55 # TITLE[5]=The Heart Won't Lie
56 # ARTIST[5]=Reba McEntire
57 # ARTIST[5]=Vince Gill
58
59 package epg::flac::archive::encode;
60
61 use v5.12;
62 use warnings;
63
64 use FindBin;
65
66 require "$FindBin::Bin/tags.p";
67 epg::flac::archive::tags->import(
68 qw[
69 read_tags
70 mangle_for_file_name
71 quote
72 ]);
73
74 sub main {
75 my $input_directory = shift;
76 my $fn = "$input_directory/tags";
77 open(my $fh, '<', $fn) || die("open($fn): $!");
78 my ($album, $tracks) = read_tags($fh);
79 if (!defined($album->{ALBUM}) || scalar(@{$album->{ALBUM}}) != 1) {
80 die('exactly one ALBUM tag required')
81 }
82 my $album_tag = $album->{ALBUM}->[0];
83 # TODO -d support
84 if (!defined($album->{ARTIST}) || scalar(@{$album->{ARTIST}}) != 1) {
85 die('exactly one ARTIST tag required')
86 }
87 my $artist = $album->{ARTIST}->[0];
88
89 my $date;
90 if (defined($album->{DATE})) {
91 if (scalar(@{$album->{DATE}}) != 1) {
92 die('one or zero DATE tags required')
93 }
94 $date = $album->{DATE}->[0];
95 }
96 my @discnumber = ();
97 if (defined($album->{DISCNUMBER})) {
98 if (scalar(@{$album->{DISCNUMBER}}) != 1) {
99 die('one or zero DISCNUMBER tags required')
100 }
101 @discnumber = ($album->{DISCNUMBER}->[0]);
102 }
103
104 my $dir = join('/', '..', mangle_for_file_name($artist), mangle_for_file_name($album_tag));
105
106 say('set -ex');
107 say('mkdir -p ', quote($dir));
108 my $tracknum = 0;
109 for my $track (@$tracks) {
110 $tracknum++;
111 if (!defined($track->{TITLE}) || scalar(@{$track->{TITLE}}) < 1) {
112 die("at least one TITLE required for track $tracknum")
113 }
114 $track->{ALBUM} = $album->{ALBUM};
115 $track->{TRACKNUMBER} = [$tracknum];
116 if (!defined($track->{ARTIST}) && defined($album->{ARTIST})) {
117 $track->{ARTIST} = $album->{ARTIST};
118 }
119 if (defined($date)) {
120 $track->{DATE} = [$date];
121 }
122 if (@discnumber) {
123 $track->{DISCNUMBER} = \@discnumber;
124 }
125 my $title = join(' ', @{$track->{TITLE}});
126 my $tracknum_s = sprintf('%02d', $tracknum);
127 my $fn = join('/',
128 $dir,
129 join('_',
130 (map { sprintf('%02d', $_) } @discnumber),
131 $tracknum_s,
132 mangle_for_file_name($title) . '.flac',
133 ),
134 );
135 -e $fn && die("cowardly refusing to clobber $fn");
136 my @pictures = ('--picture', quote('3|image/jpeg|||cover.front')); # TODO optional
137 say(join(' ',
138 'flac -o',
139 quote($fn),
140 '--delete-input-file',
141 '-V',
142 '--no-padding',
143 '--best',
144 @pictures,
145 (map {
146 my $name = $_;
147 map {
148 ('-T', quote($name . '=' . $_))
149 } @{$track->{$_}}
150 } sort(keys(%$track))),
151 "track$tracknum_s.cdda.wav"
152 ));
153 }
154 say('rm tags cover.front');
155
156 return 0;
157 }
158
159 if (!caller) {
160 exit(main(@ARGV))
161 }
162
163 1;
164