Archive for May, 2007

Ruby on Rails is a great framework, but would you believe it .. it has no built-in support for localized error messages for model level validation.

The problem is that you can internationalize the error messages but not the field name. Which makes for some very weird behavior.

Here is a bit of code to solve the problem. You have to install localization_generator before.

How to have clean internationalized validation error messages in Ruby on Rails?

in the file app/contollers/application.rb

RUBY:
  1. require 'localization'
  2.  
  3. class ActiveRecord::Errors
  4.   include Localization
  5.   def add_on_blank(attributes, msg = @@default_error_messages[:blank])
  6.     for attr in [attributes].flatten
  7.       value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
  8.       if value.blank?
  9.         add(l(attr), msg)
  10.       end
  11.     end
  12.   end
  13.  
  14.   def add(attribute, msg = @@default_error_messages[:invalid])
  15.     @errors[l(attribute).to_s] = [] if @errors[attribute.to_s].nil?
  16.     @errors[l(attribute).to_s] <<msg
  17.   end
  18.  
  19. end

And that's it! the magic is done with the l() function from localization_generator

Click to continue reading

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/([\ \(\)\&amp;'])/\$1/g;
  9.     my ( $md5 ) = split( " ", `md5sum $_` );
  10.     my $ffmpegInfo = `ffmpeg -i $_ 2&gt;/dev/stdout`;
  11.     my $file = {fileName =&gt; $_,md5 =&gt; $md5,bruteInfo =&gt; `file $_`};
  12.     for( split( "\n", $ffmpegInfo ) ){
  13.         if( /bitrate\:\ ([\d]*?)\ kb\/s/ ){ $file-&gt;{ bitrate } = $1; }
  14.         if( /Duration\:\ (.*?)\,\ start/ ){ $file-&gt;{ duration } = $1; }
  15.         if( /Video\:\ (.*?)\,\ (.*?)\,\ (.*?)\,\ (.*?)\ fps/ ){ $file-&gt;{ codec } = $1;$file-&gt;{ video } = $2;$file-&gt;{ resolution } = $3;$file-&gt;{ fps } = $4; }
  16.         if( /Audio\:\ (.*?)\,\ ([\d]*?)\ Hz\,.*?\,(.*?)\ kb\/s/ ){ $file-&gt;{ audioCodec } = $1;$file-&gt;{ hertz } = $2;$file-&gt;{ audioBitRate } = $3; }
  17.         if( $file-&gt;{ duration } =~ /(\d\d)\:(\d\d)\:(\d\d)\.(\d)/ ){ $file-&gt;{ durationSeconds } = ( $1 * 3600 ) + ( $2 * 60 ) + $3 ; }
  18.     }
  19.     my $command = "ffmpeg -i $_ ";
  20.     if( $file-&gt;{ bitrate }&gt; 1000 ){ $command .= " -b 1000 " }else{ $command .= " -b " . $file-&gt;{ 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

This is the developers blog @ AF83 here you can learn about our discoveries and frustrations our questions and our solutions..

AF83 is start-up company specializing in web and mobile solutions for communities.

you will find a lot of different bits of code on this site, unless otherwise mentioned you can consider all code published here is covered by a creative commons license. We try to make it as usable as possible and as close as possible to copy and paste usage. But it is not always tested, sometimes not tested at all. So needless to say: test, test and test again. If you ever find an error, please drop us a line and we will correct it.

Click to continue reading

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