Here's the script :

  1. #!/usr/bin/perl
  2. use strict;
  3. use JSON;
  4.  
  5. my $files = "[\n";
  6.  
  7. while(){
  8. 	chomp;$_ =~ s/([\ \(\)\&'])/\$1/g;
  9. 	my ( $md5 ) = split( " ", `md5sum $_` );
  10. 	my $ffmpegInfo = `ffmpeg -i $_ 2>/dev/stdout`;
  11. 	my $file = {fileName => $_,md5 => $md5,bruteInfo => `file $_`};
  12. 	for( split( "\n", $ffmpegInfo ) ){
  13. 		if( /bitrate\:\ ([\d]*?)\ kb\/s/ ){ $file->{ bitrate } = $1; }
  14. 		if( /Duration\:\ (.*?)\,\ start/ ){ $file->{ duration } = $1; }
  15. 		if( /Video\:\ (.*?)\,\ (.*?)\,\ (.*?)\,\ (.*?)\ fps/ ){ $file->{ codec } = $1;$file->{ video } = $2;$file->{ resolution } = $3;$file->{ fps } = $4; }
  16. 		if( /Audio\:\ (.*?)\,\ ([\d]*?)\ Hz\,.*?\,(.*?)\ kb\/s/ ){ $file->{ audioCodec } = $1;$file->{ hertz } = $2;$file->{ audioBitRate } = $3; }
  17. 		if( $file->{ duration } =~ /(\d\d)\:(\d\d)\:(\d\d)\.(\d)/ ){ $file->{ durationSeconds } = ( $1 * 3600 ) + ( $2 * 60 ) + $3 ; }
  18. 	}
  19. 	my $command = "ffmpeg -i $_ ";
  20. 	if( $file->{ bitrate } > 1000 ){ $command .= " -b 1000 " }else{ $command .= " -b " . $file->{ bitrate } . " " }
  21. 	$command .=  " $md5.flv ";
  22. 	system( $command );
  23. 	$files .= objToJson( $file ) . ",\n";
  24. }
  25.  
  26. $files .= "]";
  27.  
  28. print $files;

How to use it :

  1. find . -name '*YAPC*.avi'  |perl encode.pl > info.js

What it does ?

* Searches for all my YAPC videos.

Then for each file :
* The script gets various information about the file ( video/audio info, length, MD5sum - which will be used for file naming )
* It encodes the movies to Flash Video format.
* And finally saves all Info about the files in a nice JSON file.

This of course is not perfect ( you'll probably often get bugs due to bad Audio Rate for example, but it's easy to fix )

Requirements :

* You need to have ffmpeg compiled with the --enable-mp3lame option ( see : http://ubuntuguide.org/wiki/Ubuntu:Feisty#How_to_enable_lame_for_FFMPEG_.28needed_to_encode_FLV_with_sound.29 )
* You need the JSON Perl module ( see : http://search.cpan.org/~makamaka/JSON-1.13/lib/JSON.pm )

Next time we'll see how to reuse the files we've created, client side, in a very funky way with very little of javascript code :)
And you'll get everything you need to get any video you want ready for streaming in just a bunch of minutes.

http://dev.af83.com/trackback/3
Vincent Caron on Wed, 05/16/2007 - 10:14

If I manage to uploade a file named YAPC-screwed.avi;rm%20-rf%20/, which is not a problem as for a default LAMP setup, your account is screwed.

Use the system(@list) paradigm when possible, the Perl taint mode and sanitize your input (the Perl CGI mantra, written back in '96 for those young PHP hackers...). Eg:

  1. #/usr/bin/perl -wT
  2. use strict;
  3. ...
  4. s/'/'''/g; # basic single-quote shell escaping
  5. my $ffmpegInfo = `ffmpeg -i '$_';
  6. ...
shekar on Wed, 03/12/2008 - 15:02

hi can anybody tell me how to find whether a video file is streaming or not using perl? i.e i just wanted to know its streaming or not....