]>
diplodocus.org Git - flac-archive/blob - flacsplit
6 use Getopt
::Long
qw(:config gnu_getopt no_ignore_case);
7 use POSIX
':sys_wait_h';
14 $verbose and print(STDERR
$_) for @_;
21 $verbose and print(STDERR
"$command\n");
22 $status = system($command);
24 if (WIFEXITED
($status)) {
25 if (($status = WEXITSTATUS
($status)) != 0) {
26 die("$command exited with status $status");
28 } elsif (WIFSIGNALED
($status)) {
29 die("$command killed with signal ", WTERMSIG
($status));
30 } elsif (WIFSTOPPED
($status)) {
31 die("$command stopped with signal ", WSTOPSIG
($status));
33 die("Major horkage on system($command): \$? = $? \$! = $!");
38 return sprintf('%02d:%02d.%02d', @_);
45 open(F
, '-|', 'metaflac', '--export-cuesheet-to=-', $fn);
47 /INDEX 01 (\d\d):(\d\d):(\d\d)$/ or next;
48 push(@l, [$1, $2, $3]);
53 my $arg = ["--skip=" . tformat
(@{$l[$i]})];
56 if ($next->[2] == 0) {
57 if ($next->[1] == 0) {
58 push(@$arg, '--until=' . tformat
($next->[0] - 1, 59, 74));
60 push(@$arg, '--until=' . tformat
($next->[0], $next->[1] - 1,
64 push(@$arg, '--until=' . tformat
($next->[0], $next->[1],
78 # Return the ARTIST, ALBUM, and DISCNUMBER followed by a list of all
79 # the lines in the file FN.
94 ($tag, $value) = split(/=/, $_, 2);
98 verbose
("ARTIST $artist from $fn\n");
99 } elsif (/^ALBUM=/i) {
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");
108 return ($artist, $album, $discnum, @tags);
115 while (my ($key, $vall) = each(%$h)) {
116 for my $val (@$vall) {
117 push(@result, "$key=$val")
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) {
142 -d
$artist or mkdir($artist) or die("mkdir($artist): $!");
143 $outdir = "$artist/$album";
144 -d
"$outdir" or mkdir("$outdir") or die("mkdir($outdir): $!");
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.
152 for my $tag (@tags) {
153 if ($tag =~ /^([^[]+)\[(\d+)]=(.*)/) {
154 push(@{$tracks_to_tags{$2}->{$1}}, $3);
155 } elsif ($tag =~ /^ARTIST=/) {
156 push(@disc_artist, $tag);
165 for my $tracknum (sort(map(int, keys(%tracks_to_tags)))) {
166 my $title = join(' ', map(split, @{$tracks_to_tags{$tracknum}->{'TITLE'}}));
167 $title =~ s/'/'\\''/g;
173 ? sprintf('%02d', $discnum)
175 sprintf('%02d', $tracknum),
178 # If we have ARTIST[n] tags for this track, set @track_artist
179 # to the empty list; they will go in along with the other [n]
182 if (exists($tracks_to_tags{$tracknum}->{'ARTIST'})) {
185 @track_artist = @disc_artist;
188 my $flac_options = '';
189 my ($skip_arg, $until_arg) = @{$_[$tracknum - 1]};
193 "flac $flac_options -cd $skip_arg $until_arg '$fn'",
194 " | flac -o '$outfile.flac' -V --no-padding --best",
195 map({ s/'/'\\''/g; ('-T', "'$_'") }
198 "TRACKNUMBER=$tracknum",
199 track_tags
($tracks_to_tags{$tracknum})),
209 'quiet|q' => \
$quiet,
210 'verbose|v' => \
$verbose,
211 'help|h|?' => \
$help,
213 $help and pod2usage
(-exitstatus
=>0, -verbose
=>1);
215 @ARGV or pod2usage
();
217 my @args = get_decode_args
($fn);
218 flacsplit
($fn, @args);