This is a short module to grab various informations from a video (or audio) file. I did two versions : one using mpgtx (http://mpgtx.sourceforge.net/) one using ffmpeg (http://ffmpeg.sourceforge.net/).
The mpgtx version works only with MPEG files, obviously, but it reports the duration right. FFMPEG reads most formats (avi, divx, etc) BUT reports the duration very approximately.
mpgtx version
#!/usr/bin/perl -w # ###################################################################### +##### # # videoinfo.pm # # V 0.3.1 08/04/05 # returns duration in frames (25f/s default) # # V 0.3.0 08/01/05 # use mpginfo instead of ffmpeg # # V 0.2.0 du 13/12/2004 # support of ffmpeg CVS # # V 0.1.0 of 20/11/2004 # ###################################################################### +##### # # Copyright (c) Intellique 2004 # All rights reserved. # # # This program is free software; you can redistribute it and/or mod +ify # it under the terms of the GNU General Public License as published + by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. use strict; require Exporter; our @ISA=qw(Exporter); our @EXPORT=qw(videoinfo); # débug (set to true top get debugging messages) my $debug; # mpginfo command my $mpginfo='/usr/bin/mpgtx -i'; ########################################################## # videoinfo # returns media information in a hash # ########################################################## sub videoinfo { # variables my %finfo = ('duration' => "0", # duration in frames 'bitrate' => "0", # Mux bitrate video + audio 'vcodec' => "", 'vformat' => "", 'framerate' => "0.00", 'acodec' => "", 'samplerate' => "0", 'stereo' => "0", # 0 false (mono), 1 true (stereo) 'audiorate' => "0" ); # file to test my $file=shift; # escaping characters $file=~s/(\W)/\\$1/g; my @res; #scan only MPEG if ( $file =~ m/(MP.)$/i) { @res= `$mpginfo $file` or warn "can't run $mpginfo\n"; } # parse mpgtx output foreach (@res) { # bitrate total if ( m!Muxrate : (\d+\.?\d*) Mbps! ) { $finfo{'bitrate'}=$1; next; } # Duration if ( m!Duration: (.*[0-9][0-9]\.[0-9][0-9])s! ) { $finfo{'duration'}=$1; # convert to frame count my @tu=split(/:|\./, $finfo{'duration'}); # parse duration from end my ($c, $s, $m, $h)=reverse(@tu); # duration in seconds, converted to frames later no warnings qw(uninitialized); $finfo{'duration'}=($h*360000+$m*6000+$s*100+$c)/100; use warnings; next; } # vformat and framerate if ( /Size \[(\d+ x \d+)\]\s+(\d+\.\d+) fps/) { $finfo{'vformat'}=$1; $finfo{'framerate'}=$2; next; } # acodec if ( m!Audio : (.*)! ) { $finfo{'acodec'}=$1; next; } # audiorate and samplerate if ( m!\s+(\d+) kbps (\d+) Hz! ) { $finfo{'audiorate'}=$1; $finfo{'samplerate'}=$2; next; } # Stereo or mono if ( /Stereo/ ) { $finfo{'stereo'}=1; } } # compute the duration in frames if ($finfo{'framerate'}>1) { $finfo{'duration'}=sprintf( "%.0f", $finfo{'duration'}*$finfo{'fra +merate'}); } else { #if no framerate (audio) set to default 25/s $finfo{'duration'}=sprintf( "%.0f", $finfo{'duration'}*25); } return %finfo; } ########################################## # end ########################################## 1;
ffmpeg version
#!/usr/bin/perl -w # ###################################################################### +##### # # # V 0.2.0 du 13/12/2004 # support of ffmpeg CVS # # videoinfo.pm # V 0.1.0 du 20/11/2004 # ###################################################################### +##### # # Copyright (c) Intellique 2004 # All rights reserved. # # # This program is free software; you can redistribute it and/or mod +ify # it under the terms of the GNU General Public License as published + by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. use strict; require Exporter; use IPC::Open3; our @ISA=qw(Exporter); our @EXPORT=qw(videoinfo); ########################################################## # videoinfo # returns media information in a hash # ########################################################## sub videoinfo { # ffmpeg command my $ffmpeg='/usr/bin/ffmpeg'; # variables my %finfo = ('duration' => "00:00:00.0", 'bitrate' => "0", 'vcodec' => "", 'vformat' => "", 'framerate' => "0.00", 'acodec' => "", 'samplerate' => "0", 'stereo' => "0", # 0 false (mono), 1 true (stereo) 'audiorate' => "0" ); # fichier à traiter my $file=shift; # escaping characters $file=~s/(\W)/\\$1/g; open3("</dev/null",">/dev/null",\*ERPH, "$ffmpeg -i $file") or die "ca +n't run $ffmpeg\n"; my @res=<ERPH>; # parse ffmpeg output foreach (@res) { # duration and bitrate if ( m!Duration: ([0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9]), bitrate +: (\d*) kb/s! ) { $finfo{'duration'}=$1; $finfo{'bitrate'}=$2; } # vcodec, vformat and framerate if ( /Video: (\w*), (\d*x\d*), (\d*.\d*) fps/) { $finfo{'vcodec'}=$1; $finfo{'vformat'}=$2; $finfo{'framerate'}=$3; } # acodec, samplerate, stereo and audiorate if ( m!Audio: (\w*), (\d*) Hz, (mono|stereo), (\d*) kb/s!) { $finfo{'acodec'}=$1; $finfo{'samplerate'}=$2; $finfo{'stereo'}=(($3 eq 'stereo')|| 0 ); $finfo{'audiorate'}= +$4; } } return %finfo; } ########################################## # end ########################################## 1;

In reply to Video information by wazoox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.