Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl Audio file duration

I am writing an audio playlist generator. However, I want it to output not only name and numeration in a strict row but also duration of an audio file. Is it any way I can do so without such hacks like:

print "#: $number\tDuration: ";
system("mp3info -p \"%m:%s\" \"$track\"");
print "\tNAME: $name";

If it is any important, my perl version is v5.16.3.

like image 702
theoden8 Avatar asked Jan 31 '26 09:01

theoden8


1 Answers

Ideally you want to determine the file type based on the file content instead of the extension, File::Type comes in handy for this. MP3::Info and Audio::Flac::Header are well written perl modules.

use File::Type;
use MP3::Info;
use Audio::FLAC::Header;

my $duration = -1;
my $ft = File::Type->new()->checktype_filename($track);

$duration = Audio::FLAC::Header
              ->new($track)
              ->tags()
              ->{trackTotalLengthSeconds} 
              if($ft eq 'audio/flac');

$duration = MP3::Info
              ->get_mp3info($track)
              ->{SECS} 
              if($ft eq 'audio/mpeg3');
like image 176
harvey Avatar answered Feb 02 '26 02:02

harvey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!