]> diplodocus.org Git - flac-archive/blob - fa-encode
tidy and test epg::flac::archive::tags::disc_tags
[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 package epg::flac::archive::encode;
19
20 use v5.12;
21 use warnings;
22
23 use FindBin;
24
25 require "$FindBin::Bin/tags.p";
26 epg::flac::archive::tags->import(
27 qw[
28 read_tags
29 mangle_for_file_name
30 quote
31 two_digits
32 ]);
33
34 sub main {
35 my $input_directory = shift;
36 my $fn = "$input_directory/tags";
37 open(my $fh, '<', $fn) || die("open($fn): $!");
38 my ($album, $tracks) = read_tags($fh);
39 if (!defined($album->{ALBUM}) || scalar(@{$album->{ALBUM}}) != 1) {
40 die('exactly one ALBUM tag required')
41 }
42 my $album_tag = $album->{ALBUM}->[0];
43 # TODO -d support
44 if (!defined($album->{ARTIST}) || scalar(@{$album->{ARTIST}}) != 1) {
45 die('exactly one ARTIST tag required')
46 }
47 my $artist = $album->{ARTIST}->[0];
48
49 my $date;
50 if (defined($album->{DATE})) {
51 if (scalar(@{$album->{DATE}}) != 1) {
52 die('one or zero DATE tags required')
53 }
54 $date = $album->{DATE}->[0];
55 }
56 my @discnumber = ();
57 if (defined($album->{DISCNUMBER})) {
58 if (scalar(@{$album->{DISCNUMBER}}) != 1) {
59 die('one or zero DISCNUMBER tags required')
60 }
61 @discnumber = ($album->{DISCNUMBER}->[0]);
62 }
63
64 my $dir = join('/', '..', mangle_for_file_name($artist), mangle_for_file_name($album_tag));
65
66 say('set -ex');
67 say('mkdir -p ', quote($dir));
68 my $tracknum = 0;
69 for my $track (@$tracks) {
70 $tracknum++;
71 if (!defined($track->{TITLE}) || scalar(@{$track->{TITLE}}) < 1) {
72 die("at least one TITLE required for track $tracknum")
73 }
74 $track->{ALBUM} = $album->{ALBUM};
75 $track->{TRACKNUMBER} = [$tracknum];
76 if (!defined($track->{ARTIST}) && defined($album->{ARTIST})) {
77 $track->{ARTIST} = $album->{ARTIST};
78 }
79 if (defined($date)) {
80 $track->{DATE} = [$date];
81 }
82 if (@discnumber) {
83 $track->{DISCNUMBER} = \@discnumber;
84 }
85 my $title = join(' ', @{$track->{TITLE}});
86 my $tracknum_s = two_digits($tracknum);
87 my $fn = join('/',
88 $dir,
89 join('_',
90 (map { two_digits($_) } @discnumber),
91 $tracknum_s,
92 mangle_for_file_name($title) . '.flac',
93 ),
94 );
95 -e $fn && die("cowardly refusing to clobber $fn");
96 my @pictures = ('--picture', quote('3|image/jpeg|||cover.front.jpeg')); # TODO optional
97 say(join(' ',
98 'flac -o',
99 quote($fn),
100 '--delete-input-file',
101 '-V',
102 '--no-padding',
103 '--best',
104 @pictures,
105 (map {
106 my $name = $_;
107 map {
108 ('-T', quote($name . '=' . $_))
109 } @{$track->{$_}}
110 } sort(keys(%$track))),
111 "track$tracknum_s.cdda.wav"
112 ));
113 }
114 say('rm tags cover.front.jpeg');
115
116 return 0;
117 }
118
119 if (!caller) {
120 exit(main(@ARGV))
121 }
122
123 1;