Print file information using ffmpeg in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print file information using ffmpeg in perl
# 1  
Old 04-15-2011
Print file information using ffmpeg in perl

I am trying to print file information using ffmpeg tool in perl

Here is my code
Code:
use strict;
use warnings;

use IPC::Open3;

# example
my $filename  = $ARGV[0];
my %videoInfo = videoInfo($filename);
print "duration: " . $videoInfo{'duration'} . "\n";
print "durationsecs: " . $videoInfo{'durationsecs'} . "\n";
print "bitrate: " . $videoInfo{'bitrate'} . "\n";
print "vcodec: " . $videoInfo{'vcodec'} . "\n";
print "vformat: " . $videoInfo{'vformat'} . "\n";
print "acodec: " . $videoInfo{'acodec'} . "\n";
print "asamplerate: " . $videoInfo{'asamplerate'} . "\n";
print "achannels: " . $videoInfo{'achannels'} . "\n";

#
# returns media information in a hash
sub videoInfo {
    # ffmpeg command
    my $ffmpeg = 'C:\ffmpeg.exe';

    my %finfo = (
                  'duration'     => "00:00:00.00",
                  'durationsecs' => "0",
                  'bitrate'      => "0",
                 'vcodec'       => "",
                 'vformat'      => "",
                 'acodec'       => "",
                 'asamplerate'   => "0",
                  'achannels'       => "0",
    );

    my $file = shift;

    # escaping characters
    $file =~ s/(\W)/\\$1/g;

    open3( "<C:\top.txt", ">C:\top.txt", \*ERPH,"$ffmpeg -i $file" ) or die "can't run $ffmpeg\n";
   my @res = <ERPH>;

    # parse ffmpeg output
    foreach (@res) {
        print;

        # duration
        if (m!Duration: ([0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9])!) {
            $finfo{'duration'} = $1;
        }      # bitrate
        if (m!bitrate: (\d*) kb/s!) {
           $finfo{'bitrate'} = $1;
        }

        # vcodec and vformat
       if (/Video: (\w*), (\w*),/) {
           $finfo{'vcodec'}  = $1;
           $finfo{'vformat'} = $2;
        }

        # Stream #0.1(und): Audio: aac, 48000 Hz, 1 channels, s16, 64 kb/s

        # acodec, samplerate, stereo and audiorate
       if (m!Audio: (\w*), (\d*) Hz, (\d*)!) {
            $finfo{'acodec'}     = $1;
          $finfo{'asamplerate'} = $2;
            $finfo{'achannels'}     = $3;
        }
    }

    my $tenths  = substr( $finfo{'duration'}, 9, 2 );
    my $seconds = substr( $finfo{'duration'}, 6, 2 );
    my $minutes = substr( $finfo{'duration'}, 3, 2 );
  my $hours   = substr( $finfo{'duration'}, 0, 2 );
  $finfo{'durationsecs'} = ( $tenths * .01 ) + $seconds + ( $minutes * 60 ) + ( $hours * 360 );

    return %finfo;
}

I am getting the following output
Code:
C:\Perl>perl listfiles.pl titanic.mov
FFmpeg version Sherpya-r10707, Copyright (c) 2000-2007 Fabrice Bellard, et al.
  libavutil version: 49.5.0
  libavcodec version: 51.45.0
  libavformat version: 51.14.0
  built on Oct 11 2007 06:25:25, gcc: 4.2.1 [Sherpya]
titanic\.mov: no such file or directory
duration: 00:00:00.00
durationsecs: 0
bitrate: 0
vcodec:
vformat:
acodec:
asamplerate: 0
achannels: 0

If you see this , you can read "titanic\.mov: no such file or directory"

From where is it getting "\" ?
Anyone know how to remove it and make it work ?
Why is it showing 00:00:00:00 ?

please me help me guys..

Thanks

Last edited by jim mcnamara; 04-15-2011 at 11:18 AM.. Reason: duplicate thread
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Adding information to c file using perl

Hi, I have c file which contains more number of tests. two or more tests in one c file. I have the XMl data regarding tests. i need to add this xml data to c file at before the test. I know which file having which test and i created hash table for that. so the problem is i have to add information... (13 Replies)
Discussion started by: veerubiji
13 Replies

2. Shell Programming and Scripting

Some Help to print out information

Need abit of help to print out information. I have something like this Logitech:My store:34.3:23 Altec Lansing:Partner:45.9:20 with : as delimiter and i need an output arranged like this Logitech My Store 34.3 23 Altec Lansing Partner 45.9 20 tried... (5 Replies)
Discussion started by: ezinele
5 Replies

3. Shell Programming and Scripting

perl script to print file information - newbie

Hi I have a perl script that prints all the video and audio file information(playing duration). It works fine in one of my friends linux laptop. But it doesn't work in my both windows and linux. My friend told me I have to do install some module ( ppm instal ...... ) but I have no... (1 Reply)
Discussion started by: srijith
1 Replies

4. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

5. Shell Programming and Scripting

using perl for matching one file with another file and print into new line

One file is fileA 0.0246*0.0068*0.0013*0.0023*0.0182*0.0028*0.0019*0.4750*0.0028*0.0812*0.0123*0.0018*0.0039*0.0020*0.0028*0.0047*0.0139*0.3330*0.0017*0.0072*0.4789... (4 Replies)
Discussion started by: cdfd123
4 Replies

6. Shell Programming and Scripting

Perl: adding columns in CSV file with information in each

Hi Wise UNIX Crew, I want to add 3 different columns to the file in which: 1. The first new column pulls in today's date and time 2. Second column one has a '0' 3. Third column has the word 'ANY' going down the column If my file content is as follows: "7","a","abc",123"... (1 Reply)
Discussion started by: dolo21taf
1 Replies

7. Shell Programming and Scripting

perl - print to a log file and screen

as the title suggests, i need to print a user message to a log file and the screen using perl. in unix i set up a function using 'tee' like so function Display_Message { echo "$*" | tee -ai $LOGFILE } the following command then obviously displays to the screen and prints to a log... (6 Replies)
Discussion started by: mjays
6 Replies

8. OS X (Apple)

Get print job information

Hi all, I am on a mac and I am trying to get more information about print jobs i have. "lpq" only displays the document printing, size, and job ID. I need the status (if its on hold or not). Anyway to do that in unix/cups? (5 Replies)
Discussion started by: CBarraford
5 Replies

9. Shell Programming and Scripting

PERL:print 32 variables into a text file

okay, down below is the script. i have 32 words put into 32 variables and i want perl to print all those 32 variables into one text document, each word under another in the text file. the text files called times.txt Sorry about the length of the script print " VToshiba ... (2 Replies)
Discussion started by: perleo
2 Replies
Login or Register to Ask a Question