Archive for the perl Category

Here's the script :

PERL:
  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 :


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.

Click to continue reading

Creative Commons License
This work is licensed under a Creative Commons Attribution 2.0 License.