]>
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', '--no-utf8-convert', '--export-cuesheet-to=-',
48 /INDEX 01 (\d\d):(\d\d):(\d\d)$/ or next;
49 push(@l, [$1, $2, $3]);
54 my $arg = ["--skip=" . tformat
(@{$l[$i]})];
57 if ($next->[2] == 0) {
58 if ($next->[1] == 0) {
59 push(@$arg, '--until=' . tformat
($next->[0] - 1, 59, 74));
61 push(@$arg, '--until=' . tformat
($next->[0], $next->[1] - 1,
65 push(@$arg, '--until=' . tformat
($next->[0], $next->[1],
79 # Return the ARTIST, ALBUM, and DISCNUMBER followed by a list of all
80 # the lines in the file FN.
95 ($tag, $value) = split(/=/, $_, 2);
99 verbose
("ARTIST $artist from $fn\n");
100 } elsif (/^ALBUM=/i) {
102 verbose
("ALBUM $album from $fn\n"); # cperl-mode sucks "
103 } elsif (/^DISCNUMBER=/i) {
104 $discnum = int($value);
105 verbose
("DISCNUMBER $discnum from $fn\n");
109 return ($artist, $album, $discnum, @tags);
116 while (my ($key, $vall) = each(%$h)) {
117 for my $val (@$vall) {
118 push(@result, "$key=$val")
134 open(my $fp, '-|', 'metaflac', '--no-utf8-convert', '--export-tags-to=-',
136 or die("open(metaflac --no-utf8-convert --export-tags-to=- $fn): $!");
137 ($artist, $album, $discnum, @tags) = get_tags
($fp, $fn);
139 or die("close(metaflac --no-utf8-convert --export-tags-to=- $fn): $?");
140 for ($artist, $album) {
145 -d
$artist or mkdir($artist) or die("mkdir($artist): $!");
146 $outdir = "$artist/$album";
147 -d
"$outdir" or mkdir("$outdir") or die("mkdir($outdir): $!");
149 # Go over @tags, store all [n] tags in a list keyed by n in
150 # %tracks_to_tags, store all ARTIST (not ARTIST[n]) tags in
151 # @disc_artist, and leave the rest in @tags.
156 for my $tag (@tags) {
157 if ($tag =~ /^([^[]+)\[(\d+)]=(.*)/) {
158 push(@{$tracks_to_tags{$2}->{$1}}, $3);
159 } elsif ($tag =~ /^TITLE=(.*)/) {
160 push(@{$tracks_to_tags{$hack++}->{'TITLE'}}, $1);
161 } elsif ($tag =~ /^ARTIST=/) {
162 push(@disc_artist, $tag);
171 for my $tracknum (sort(map(int, keys(%tracks_to_tags)))) {
172 my $title = join(' ', map(split, @{$tracks_to_tags{$tracknum}->{'TITLE'}}));
173 $title =~ s/'/'\\''/g;
179 ? sprintf('%02d', $discnum)
181 sprintf('%02d', $tracknum),
184 # If we have ARTIST[n] tags for this track, set @track_artist
185 # to the empty list; they will go in along with the other [n]
188 if (exists($tracks_to_tags{$tracknum}->{'ARTIST'})) {
191 @track_artist = @disc_artist;
194 my $flac_options = '';
195 my ($skip_arg, $until_arg) = @{$_[$tracknum - 1]};
199 "flac $flac_options -cd $skip_arg $until_arg '$fn'",
200 " | flac -o '$outfile.flac' -V --no-padding --best",
201 map({ s/'/'\\''/g; ('-T', "'$_'") }
204 "TRACKNUMBER=$tracknum",
205 track_tags
($tracks_to_tags{$tracknum})),
215 'quiet|q' => \
$quiet,
216 'verbose|v' => \
$verbose,
217 'help|h|?' => \
$help,
219 $help and pod2usage
(-exitstatus
=>0, -verbose
=>1);
221 @ARGV or pod2usage
();
223 my @args = get_decode_args
($fn);
224 flacsplit
($fn, @args);