Tuesday 31 January 2012

Install latest ffmpeg in Centos

Install ffmpeg from git source on CentOS:

Dependencies:


yum install SDL-devel a52dec a52dec-devel alsa-lib-devel faac faac-devel faad2 faad2-devel
yum install freetype-devel giflib gsm gsm-devel imlib2 imlib2-devel lame lame-devel libICE-devel libSM-devel libX11-devel
yum install libXau-devel libXdmcp-devel libXext-devel libXrandr-devel libXrender-devel libXt-devel
yum install libid3tag libogg-devel libvorbis-devel mesa-libGL-devel mesa-libGLU-devel xorg-x11-proto-devel xvidcore xvidcore-devel zlib-devel
yum install amrnb-devel amrwb-devel libtheora libtheora-devel glibc gcc gcc-c++ autoconf automake libtool ncurses-devel libdc1394 libdc1394-devel yasm nasm

Installation:

cd /usr/local/src
git clone --depth 1 git://source.ffmpeg.org/ffmpeg
cd ffmpeg
./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvorbis --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab
make
make install
hash x264 ffmpeg ffplay ffprobe

Check:

ffmpeg -filters

And you are done.

Alternatives:

1). http://ffmpeg.org/trac/ffmpeg/wiki/CentosCompilationGuide
2). http://www.ffmpeginstaller.com/ - Installs through automated script.


Monday 30 January 2012

Paperclip: Unzip/decompress uploaded file

Very often(in a client-server API architecture) we might want to transfer compressed file data over HTTP, so gain speed over file uploads.

So, this is how the decompression code looks like in Rails 3: For a Video model, with 'data' as the paperclip attachment and opts[:data] being the uploaded file.



    tmp_fle = Tempfile.new(opts[:data].original_filename.to_s)
    zi = Zlib::GzipReader.new(opts[:data])
    File.open(tmp_fle, "wb+") do |ucf|
      ucf << zi.read
    end
    zi.close
    tmp_fle.binmode
    self.data = tmp_fle

Hope it works.

Thursday 5 January 2012

Rails/Mysql - Selecting Random records

The straight forward and the most reliable solution to select random record from a table would be to use rand() in Mysql or random() in Postgres.

However the glitch is with the performance of rand() when using against large tables (even as 1000 rows). With my test results of 750 rows.

Photo.order('rand()').limit(30)  - took 9ms


Photo.where(:id => Photo.order('rand()').limit(30).select(:id).collect(&:id))  - took 2.5ms

So using rand() with selecting just the id, is atleast 3 times faster.