Skip to content

Instantly share code, notes, and snippets.

@fracai
Created July 6, 2011 19:28
Show Gist options
  • Save fracai/1068119 to your computer and use it in GitHub Desktop.
Save fracai/1068119 to your computer and use it in GitHub Desktop.
Parse the length of media files using mplayer
#!/usr/bin/perl
use strict;
use warnings;
use Cwd;
use Getopt::Long;
my ($human, $help, $sum);
my $result = GetOptions(
"human" => \$human,
"sum" => \$sum,
"total" => \$sum,
);
my ($command, @result, $file);
my ($hur, $min, $sec);
$command = "mplayer -idx -identify -frames 0 -vc null -vo null -ao null ";
for my $file (@ARGV) {
$command = $command."\"$file\" ";
}
$command = $command."2>/dev/null";
@result = `$command`;
for my $line (@result) {
$line =~ s/[\r\n]+$//;
if ($line =~ s/^ID_FILENAME=//) {
$file = $line;
}
if ($line =~ s/^ID_LENGTH=//) {
if ($human) {
$sec = $line;
$hur = int( $sec / (60*60) );
$min = int( ($sec - $hur*60*60) / (60) );
$sec = int( $sec - ($hur*60*60) - ($min*60) );
printf ("%02d:%02d:%06.3f",$hur, $min, $sec);
} else {
printf ("%10.3f",$line);
}
if ($sum and $#ARGV > 0) {
$sum += $line;
}
if ($#ARGV > 0) {
print "\t$file\n";
} else {
print "\n";
}
}
}
if ($sum and $#ARGV > 0) {
if ($human) {
$sum -= 1;
$hur = int( $sum / (60*60) );
$min = int( ($sum - $hur*60*60) / (60) );
$sec = int( $sum - ($hur*60*60) - ($min*60) );
print "------------\n";
printf ("%02d:%02d:%06.3f\n",$hur, $min, $sec);
} else {
print "----------\n";
printf ("%10.3f\n",$sum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment