# # NAME # mp3split - split an MP3 file into multiple pieces # # SYNOPSIS # mp3split [-l mm:ss[/mm:ss/mm:ss/...]] file.mp3 # # AUTHOR # Tony Hansen, tony@att.com use strict vars; use MP3::Splitter; use MP3::Info; use Getopt::Std; my %opts; getopts('Pl:L:sv', \%opts) or exit 1; while ($opts{P}) { print "Enter mm:ss or mm:ss/mm:ss/... to establish split points for the files: "; flush; my $args = ; chomp($args); if ($args =~ /^\d/) { $opts{L} = $args; print "split at '$args'\n" if $opts{v}; $opts{P} = undef; } } print "#ARGV=$#ARGV\n" if $opts{v}; # for (my $i = 0; $i <= $#ARGV; $i++) { print "ARGV[$i]=$ARGV[$i]\n"; } # die "Usage: $0 [-l length] mp3-file" if $#ARGV != 0; while ($#ARGV == -1) { my @fns = <*.mp3>; if ($opts{s}) { @fns = <*_*.mp3>; } for (my $i = 0; $i <= $#fns; $i++) { my $x = sprintf("%d) %s%s", ($i + 1), $fns[$i], " "x80); print substr($x, 0, 39); # my $j = $i + 1; # print "$j) $fns[$i]\t"; print "\n" if ($i % 2 == 1); } # print join(' ', @fns); print "\n"; print "Enter -lmm:ss/mm:ss/... or -Lmm:ss/mm:ss/...\n"; print "(-l will repeat last mm:ss until end, -L will not)\n"; print "or filename or filename ##? "; flush; my $args = ; chomp($args); if ($args =~ /^\d+$/) { $args = $fns[$args - 1]; } @ARGV = split(/\s+/, $args); print "split => #ARGV=$#ARGV\n" if $opts{v}; print "opts{l}='" . $opts{l} . "'\n" if $opts{v}; print "opts{L}='" . $opts{L} . "'\n" if $opts{v}; getopts('l:L:v', \%opts); print "getopts => #ARGV=$#ARGV\n" if $opts{v}; print "opts{l}='" . $opts{l} . "'\n" if $opts{v}; print "opts{L}='" . $opts{L} . "'\n" if $opts{v}; } my $file = $ARGV[0]; my $info = get_mp3info($file); printf "$file length is %d:%02d\n", $info->{MM}, $info->{SS}; my $TMP = "tmpfile.tmp"; open TMP, ">$TMP" or die "Cannot write $TMP: $!\n"; my $i; my $quantum = defined($opts{l}) ? $opts{l} : defined($opts{L}) ? $opts{L} : 3; print "quantum='$quantum'\n" if $opts{v}; my @quantum = split(/\//, $quantum); print "#quantum=$#quantum\n" if $opts{v}; for ($i = 0; $i <= $#quantum; $i++) { if ($quantum[$i] =~ /^(\d*):(\d+)$/) { $quantum[$i] = ($1 * 1) + ($2 / 60); print "quantum[$i]=$quantum[$i]\n" if $opts{v}; } elsif ($quantum[$i] =~ /^(\d*)$/) { $quantum[$i] = ($1 * 1); print "quantum[$i]=$quantum[$i]\n" if $opts{v}; } } my $max = $info->{MM} + ($info->{SS} / 60); my $minutes; print "max=$max\n" if $opts{v}; for ($i = $minutes = 0; $minutes + $quantum[$i] < $max; $minutes += $quantum) { my $mm = $quantum[$i] . "m"; # . ($minutes + $quantum) . "m"; print TMP ">0 $mm\n"; print "$i: >0 $mm\n" if $opts{v}; print "#quantum=$#quantum\n" if $opts{v}; if ($i < $#quantum) { $i++; } else { last if $opts{L}; } } printf TMP ">0 INF\n"; close TMP; mp3split_read($file, $TMP, { verbose => 1, lax => 5, name_callback => sub { my ($track, $fname) = @_; print "track='$track', fname='$fname'\n" if $opts{v}; if ($fname =~ /^(.*2\d\d\d-\d\d-\d\d.*)\.mp3$/i) { return sprintf "%s_%02d.mp3", $1, $track; } else { return sprintf "%02d_%s", $track, $fname; } }, overwrite => true}); unlink($TMP) unless $opts{v};