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