Created
July 6, 2011 19:28
-
-
Save fracai/1068119 to your computer and use it in GitHub Desktop.
Parse the length of media files using mplayer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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