]> diplodocus.org Git - flac-archive/blob - fa-mp3cd
pod2usage on error
[flac-archive] / fa-mp3cd
1 #!/usr/local/bin/perl
2
3 =head1 NAME
4
5 B<fa-mp3cd> - Make Universal Disk Format image of MP3 files from FLAC files
6
7 =head1 SYNOPSIS
8
9 B<fa-mp3cd> [B<-d> I<work-directory>] [B<-o> I<image-file>] [B<-V> I<VOLI>] I<file> [...]
10
11 =head1 DESCRIPTION
12
13 Each I<file> argument is a single-track[1] FLAC file to be transcoded to MP3
14 and stored in the UDF image. By default, place MP3 files in directories named
15 after the album title. Change with B<-dirname> options tracks after the
16 B<-dirname> go into that dirname. Restore default with B<-diralbum>.[2]
17
18 B<fa-mp3cd> starts by validating all input files and planning the layout, then
19 looking into I<work-directory> for any files not in the plan, exiting with an
20 error if any are found. If files exist that are in the plan, assume this is a
21 rerun after crash and resume starting at the last file written. E.g. if
22 writing 100 files and crash after finishing file 98 but before starting file
23 99, go ahead and rewrite file 98.
24
25 =head2 Notes
26
27 =over
28
29 =item 1.
30 Still considering restoring whole-disc FLAC file support.
31
32 =item 2.
33 Not yet implemented.
34
35 =back
36
37 =head1 OPTIONS
38
39 =over
40
41 =item B<-d> I<work-directory>
42
43 Path of the directory to write the UDF layout into. Default is the
44 current directory.
45
46 =item B<-o> I<image-file>
47
48 Path of file to write UDF image to. No default: if not specified, B<fa-mp3cd>
49 prepares the I<work-directory> but makes no image.
50
51 =item B<-V> I<VOLI>
52
53 Passed through to B<mkisofs(1)>, which says:
54
55 =item B<--no-resume>
56
57 Assume files in I<work-directory> are up to date rather than assuming resume
58 after crash.
59
60 =item B<--print-size>
61
62 Run B<mkisofs(1)> to print out the estimated size (in kilobytes) of the image
63 as well as how much remains of the 650 MB allowed on CD-ROM.
64
65 =over
66
67 Specifies the volume ID (volume name or label) to be written into the master
68 block. There is space on the disc for 32 characters of information.
69
70 =back
71
72 Some systems may use this volume ID as the name of a mount point.
73
74 =back
75
76 =head1 AUTHORS
77
78 Written by Eric Gillespie <epg@pretzelnet.org>.
79
80 =cut
81
82 package epg::flac::archive::mp3::cd;
83
84 use v5.12;
85 use warnings;
86 use FindBin;
87
88 # TODO eval hack
89 eval {
90 require "$FindBin::Bin/flac2mp3";
91 #require "$FindBin::Bin/tags.pl";
92 };
93 epg::flac::archive::tags->import(
94 qw[
95 track_tags
96 read_tags_metaflac
97 mangle_for_file_name
98 quote
99 two_digits
100 ]);
101
102 sub plan_flac {
103 my $workdir = shift;
104 my $path = shift;
105 my $tags = shift;
106
107 my %track = track_tags($tags);
108 $track{flac} = $path;
109 $track{filename} = mangle_for_file_name(
110 join(' ',
111 (map { two_digits($_) } @{$track{discnumber}}),
112 two_digits($track{tracknumber}),
113 $track{title},
114 )) . '.mp3';
115 $track{dir} = join('/', $workdir, mangle_for_file_name($track{album}));
116 \%track
117 }
118
119 sub plan {
120 my $workdir = shift;
121 map { plan_flac($workdir, @$_) } @_
122 }
123
124 # if @layout is partially on disk, return the portion that remains to be written
125 sub check_directory {
126 my $workdir = shift;
127 my $workdir_files = shift;
128 my @layout = @_;
129
130 my @remaining;
131 my $last;
132 my %workdir_files = map { ($_, 1) } @$workdir_files;
133 for my $mp3 (@layout) {
134 my $path = join('/', $mp3->{dir}, $mp3->{filename});
135 if (exists($workdir_files{$path})) {
136 delete($workdir_files{$path});
137 $last = $mp3;
138 } else {
139 if (defined($last)) {
140 push(@remaining, $last);
141 undef($last);
142 }
143 push(@remaining, $mp3);
144 }
145 }
146 defined($last) && push(@remaining, $last);
147 if (%workdir_files > 0) {
148 die("unexpected files in $workdir: ", join(' ', sort(keys(%workdir_files))))
149 }
150
151 @remaining
152 }
153
154 sub read_args {
155 my $workdir = '.'; # TODO
156 my $imagefile; # TODO
157 # TODO other flags
158 $workdir, $imagefile, @_
159 }
160
161 sub find_files {
162 my $path = shift;
163 # If we come across stupid file names embedded newlines, check_directory will complain about them.
164 open(my $fh, '-|', 'find', $path, '!', '-type', 'd') || die("find $path ! -type d: $!");
165 my @files;
166 while (<$fh>) {
167 chomp;
168 push(@files, $_);
169 }
170 if (!close($fh)) {
171 if ($! == 0) {
172 die("find $path ! -type d exited $?")
173 }
174 die("close(find $path ! -type d): $!")
175 }
176 \@files
177 }
178
179 sub main {
180 my ($workdir, $imagefile, @flac_files) = read_args(@_);
181
182 # -no-resume would mean skipping preparing the work directory at all.
183 # So that's probably not the right name for the flag.
184 my @layout = check_directory(
185 $workdir,
186 find_files($workdir),
187 plan(
188 $workdir,
189 map { ["$_", read_tags_metaflac($_)] } @flac_files,
190 ));
191 for my $mp3 (@layout) {
192 say('mkdir -p ' . $mp3->{dir});
193 epg::flac::archive::mp3::flac2mp3(join('/', $mp3->{dir}, $mp3->{filename}), $mp3->{flac}, $mp3);
194 }
195
196 my @output;
197 if (defined($imagefile)) {
198 # Have to redirect it; won't accept -o with -reproducible-date!
199 #'-o', quote($imagefile)
200 @output = ('>', quote($imagefile));
201 } else {
202 # "Print estimated filesystem size in multiples of the sector size (2048 bytes)"
203 # Overhead seems to be under 1MB so shoot for total of 649 MB of files.
204 @output = ('-print-size');
205 }
206 # If I cared for fully reproducible image, set all file and directory timestamps:
207 # touch -d `git log -1 '--format=%cd' '--date=format:%FT%T'`
208 say(join(' ',
209 'mkisofs',
210 @output,
211 #'-reproducible-date', `date '+%Y-%m-%d %H:%M:%S %z'`,
212 #'-reproducible-date', `git log -1 '--format=%ai'`,
213 #'-reproducible-date', quote('2022-03-11 23:37:11 -0600'),
214 '-J',
215 '-full-iso9660-filenames',
216 '-r',
217 '-udf',
218 quote($workdir),
219 ));
220
221 0
222 }
223
224 if (!caller) {
225 exit(main(@ARGV))
226 }
227
228 1;