]> diplodocus.org Git - flac-archive/blob - flacsplit
Bah, go ahead and require flac 1.1.3 .
[flac-archive] / flacsplit
1 #! /usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use Getopt::Long qw(:config gnu_getopt no_ignore_case);
7 use POSIX ':sys_wait_h';
8 use Pod::Usage;
9
10 my $quiet;
11 my $verbose;
12
13 sub verbose {
14 $verbose and print(STDERR $_) for @_;
15 }
16
17 sub run_or_die {
18 my $command = shift;
19 my $status;
20
21 $verbose and print(STDERR "$command\n");
22 $status = system($command);
23
24 if (WIFEXITED($status)) {
25 if (($status = WEXITSTATUS($status)) != 0) {
26 die("$command exited with status $status");
27 }
28 } elsif (WIFSIGNALED($status)) {
29 die("$command killed with signal ", WTERMSIG($status));
30 } elsif (WIFSTOPPED($status)) {
31 die("$command stopped with signal ", WSTOPSIG($status));
32 } else {
33 die("Major horkage on system($command): \$? = $? \$! = $!");
34 }
35 }
36
37 sub tformat {
38 return sprintf('%02d:%02d.%02d', @_);
39 }
40
41 sub get_decode_args {
42 my $fn = shift;
43 my @l;
44
45 open(F, '-|', 'metaflac', '--export-cuesheet-to=-', $fn);
46 while (<F>) {
47 /INDEX 01 (\d\d):(\d\d):(\d\d)$/ or next;
48 push(@l, [$1, $2, $3]);
49 }
50
51 my @args;
52 for my $i (0..$#l) {
53 my $arg = ["--skip=" . tformat(@{$l[$i]})];
54 my $next = $l[$i+1];
55 if (defined($next)) {
56 if ($next->[2] == 0) {
57 if ($next->[1] == 0) {
58 push(@$arg, '--until=' . tformat($next->[0] - 1, 59, 74));
59 } else {
60 push(@$arg, '--until=' . tformat($next->[0], $next->[1] - 1,
61 74));
62 }
63 } else {
64 push(@$arg, '--until=' . tformat($next->[0], $next->[1],
65 $next->[2] - 1));
66 }
67 }
68 push(@args, $arg);
69 }
70
71 if (@args == 0) {
72 die('no cue sheet');
73 }
74
75 return @args;
76 }
77
78 # Return the ARTIST, ALBUM, and DISCNUMBER followed by a list of all
79 # the lines in the file FN.
80 sub get_tags {
81 my $fp = shift;
82 my $fn = shift;
83 my $tag;
84 my $value;
85 my $artist;
86 my $album;
87 my $discnum;
88 my @tags;
89
90 while (<$fp>) {
91 chomp;
92 push(@tags, $_);
93
94 ($tag, $value) = split(/=/, $_, 2);
95
96 if (/^ARTIST=/i) {
97 $artist = $value;
98 verbose("ARTIST $artist from $fn\n");
99 } elsif (/^ALBUM=/i) {
100 $album = $value;
101 verbose("ALBUM $album from $fn\n"); # cperl-mode sucks "
102 } elsif (/^DISCNUMBER=/i) {
103 $discnum = int($value);
104 verbose("DISCNUMBER $discnum from $fn\n");
105 }
106 }
107
108 return ($artist, $album, $discnum, @tags);
109 }
110
111 sub track_tags {
112 my $h = shift;
113 my @result;
114
115 while (my ($key, $vall) = each(%$h)) {
116 for my $val (@$vall) {
117 push(@result, "$key=$val")
118 }
119 }
120
121 return @result;
122 }
123
124 sub flacsplit {
125 my $fn = shift;
126 my $artist;
127 my $album;
128 my $discnum;
129 my @tags;
130 my $outdir;
131 my $outfile;
132
133 open(my $fp, '-|', 'metaflac', '--export-tags-to=-', $fn)
134 or die("open(metaflac --export-tags-to=- $fn): $!");
135 ($artist, $album, $discnum, @tags) = get_tags($fp, $fn);
136 close($fp) or die("close(metaflac --export-tags-to=- $fn): $?");
137 for ($artist, $album) {
138 s/'/'\\''/g;
139 s|/|_|g;
140 }
141
142 -d $artist or mkdir($artist) or die("mkdir($artist): $!");
143 $outdir = "$artist/$album";
144 -d "$outdir" or mkdir("$outdir") or die("mkdir($outdir): $!");
145
146 # Go over @tags, store all [n] tags in a list keyed by n in
147 # %tracks_to_tags, store all ARTIST (not ARTIST[n]) tags in
148 # @disc_artist, and leave the rest in @tags.
149 my %tracks_to_tags;
150 my @disc_artist;
151 my @tmp;
152 my $hack = 1;
153 for my $tag (@tags) {
154 if ($tag =~ /^([^[]+)\[(\d+)]=(.*)/) {
155 push(@{$tracks_to_tags{$2}->{$1}}, $3);
156 } elsif ($tag =~ /^TITLE=(.*)/) {
157 push(@{$tracks_to_tags{$hack++}->{'TITLE'}}, $1);
158 } elsif ($tag =~ /^ARTIST=/) {
159 push(@disc_artist, $tag);
160 } else {
161 push(@tmp, $tag);
162 }
163 }
164 @tags = @tmp;
165
166 $fn =~ s/'/'\\''/g;
167
168 for my $tracknum (sort(map(int, keys(%tracks_to_tags)))) {
169 my $title = join(' ', map(split, @{$tracks_to_tags{$tracknum}->{'TITLE'}}));
170 $title =~ s/'/'\\''/g;
171 $title =~ s|/|_|g;
172 $outfile = join('/',
173 $outdir,
174 join(' ',
175 (defined($discnum)
176 ? sprintf('%02d', $discnum)
177 : ()),
178 sprintf('%02d', $tracknum),
179 $title));
180
181 # If we have ARTIST[n] tags for this track, set @track_artist
182 # to the empty list; they will go in along with the other [n]
183 # tags.
184 my @track_artist;
185 if (exists($tracks_to_tags{$tracknum}->{'ARTIST'})) {
186 @track_artist = ();
187 } else {
188 @track_artist = @disc_artist;
189 }
190
191 my $flac_options = '';
192 my ($skip_arg, $until_arg) = @{$_[$tracknum - 1]};
193 $skip_arg ||= '';
194 $until_arg ||= '';
195 run_or_die(join(' ',
196 "flac $flac_options -cd $skip_arg $until_arg '$fn'",
197 " | flac -o '$outfile.flac' -V --no-padding --best",
198 map({ s/'/'\\''/g; ('-T', "'$_'") }
199 @track_artist,
200 @tags,
201 "TRACKNUMBER=$tracknum",
202 track_tags($tracks_to_tags{$tracknum})),
203 '-'));
204 }
205
206 return 0;
207 }
208
209 MAIN: {
210 my $help;
211 GetOptions(
212 'quiet|q' => \$quiet,
213 'verbose|v' => \$verbose,
214 'help|h|?' => \$help,
215 ) or pod2usage();
216 $help and pod2usage(-exitstatus=>0, -verbose=>1);
217
218 @ARGV or pod2usage();
219 for my $fn (@ARGV) {
220 my @args = get_decode_args($fn);
221 flacsplit($fn, @args);
222 }
223 }