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