[Gllug] Command line tool to find video dimensions?

Nix nix at esperi.org.uk
Thu May 31 21:13:40 UTC 2007


On 31 May 2007, salsaman at xs4all.nl spake thusly:
> In perl:
>
> #!/usr/bin/perl
>
> $file=$ARGV[0];
>
> # $file might be a stream, so we need to cache a bit
> system("mplayer -identify -vo null -ao null -frames 0 -cache 32
> \"$file\">tmpfile 2>/dev/null </dev/null");
>
> $hsize=`grep ID_VIDEO_WIDTH tmpfile 2>/dev/null`;
> $hsize=(split("=",(split("\n",$hsize))[0]))[1];
> chomp($hsize);
>
> $vsize=`grep ID_VIDEO_HEIGHT tmpfile 2>/dev/null`;
> $vsize=(split("=",(split("\n",$vsize))[0]))[1];
> chomp($vsize);
>
> unlink "tmpfile";
>
> print "Video size is $hsize x $vsize\n";

*icccck*

Perl does support pipes you know, and grep, and stuff, natively; no need
for temporary files, let alone ones with insecure names possibly
conflicting with existing files in the directory. Here's a three-minute
hack (a proper implementation would use Pod::Usage at least and have
proper docs; IPC::Run might be nice to use here as well):

#!/usr/bin/perl -w

use strict;

die "Syntax: vidgeom filename\n" if $#ARGV != 0;

die "$ARGV[0] not found" unless -f $ARGV[0];

open MPLAYER, 'mplayer -identify -vo null -ao null -frames 0 -cache 32 ' .
    $ARGV[0] . '|' or die "Can't run mplayer: $!\n";

my @output = <MPLAYER>;

close MPLAYER;

my $width_line = (grep /^ID_VIDEO_WIDTH/, at output)[0];
my $height_line = (grep /^ID_VIDEO_HEIGHT/, at output)[0];

print $width_line;
print $height_line;

chomp $width_line;
chomp $height_line;

die "Can't determine height and width\n"
    if !defined ($width_line) or !defined ($height_line);

print 'Video size is ' . (split /=/, $width_line)[1] .
    'x' . (split /=/, $height_line)[1] . "\n";


-- 
`On a scale of one to ten of usefulness, BBC BASIC was several points ahead
 of the competition, scoring a relatively respectable zero.' --- Peter Corlett
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list