How to Trim MP3 file selectively from start time to end time. For example if I have 4 minutes mp3 file and I need to trim the sound of the file only between the second 140 to the second 170. If pure Perl module or a module that installs on Windows.
There are two modules on CPAN. MP3::Splitter
and MP3::Cut::Gapless
, both of which contain C-code and currently do not compile under Windows. Not totally the fault of Windows mind you, MP3::Splitter
doesn't seem to work anywhere with recent Perls. It fails because of a broken dependency (MPEG::Audio::Frame
), I'm looking into that, maybe I can find a fix.
In the meantime, if you use Windows 10, you can give the Linux Subsystem for Windows (WSL) a try and run your script from there.
use MP3::Cut::Gapless;
my $cut = MP3::Cut::Gapless->new(
file => 'input.mp3',
start_ms => 140000,
end_ms => 170000,
);
open my $out, '>', 'output.mp3' or die "Can't write MP3";
while ( $cut->read( my $buf, 4096 ) ) {
syswrite $out, $buf;
}
close $out;
Or you can install a lightweight tool named ffmpeg and shell out to that
`ffmpeg -ss 00:02:20 -t 00:00:30 -i input.mp3 output.mp3`
Ok, here is an update.
I looked into the problem with MPEG::Audio::Frame
and it's just the tests that are failing¹!
That means, you can force install MPEG::Audio::Frame
first, then do a regular install of MP3::Splitter
. Then you can run this script
use MP3::Splitter;
mp3split('yourfile.mp3', {verbose => 1}, [140, 30]);
You will then find your new file "01_yourfile.mp3" in the same directory as the input file. That's a clunky interface but at least it works.
¹I contacted the author. The problem is that the tests read their data from DATA, but the author did not use binmode
. So under Windows that DATA gets read as text and shit goes awry.
Final update
I managed to get maintainership of MPEG::Audio::Frame
and fixed the bug. The new version 0.10 is now on CPAN. Hence, with this fix, MP3::Splitter
will install fine everywhere.