Categoría: Ruby


A motion-jpeg stream with Ruby and Sinatra

noviembre 30th, 2011 — 12:37pm

I’ve been trying to set up a Motion-JPEG streaming with ruby, for a webcam which uploads a picture per second to a server. I want new images to be served via streaming instead of making the browser call them every second via a javascript call.

I found many different solutions, some of them including EventMachine, which now is not necessary thanks to the new Sinatra 1.3 streaming feature.
The tricky part was related to the headers, the boundary, and the need to send the content type before each image.

For the test I first created a directory and stored some pictures inside

mkdir /tmp/images
# place some jpg pictures here

And the code. My little app looks like this:

# motion_stream.rb
require 'sinatra'
set :server, :thin

get '/' do |dir|
  boundary      = 'some_shit'
  source_dir    = '/tmp/images'

  headers \
    "Cache-Control" => "no-cache, private",
    "Pragma"        => "no-cache",
    "Content-type"  => "multipart/x-mixed-replace; boundary=#{boundary}"

  stream(:keep_open) do |out|
    while true
      file        = random_file(source_dir) # see also latest_file() below
      content     = File.open("#{source_dir}/#{file}", 'rb') { |f| f.read }

      out << "Content-type: image/jpeg\n\n"
      out << content
      out << "\n\n--#{boundary}\n\n"

      sleep 1
    end
  end
end

## get a random image from a directory
##
def random_file(dir)
  files = Dir.entries(dir).collect { |file| file }
  files -= ['.', '..']
  files[rand(files.size)]
end

## ... or get the newest image
## In this case I'm not taking the latest file
## uploaded by the camera, but the previous one.
## This is to avoid grabbing a currently uploading
## file, which may be shown as corrupt or incomplete.
##
def latest_file(dir)
  files = Dir.entries(dir).collect { |file| file }.sort { |file2,file1| File.mtime(dir+file1) <=> File.mtime(dir+file2) }
  files -= ['.', '..']
  files[1]
end

Then simply create a Gemfile including Sinatra and Thin, as WebRick is not evented and does not support this kind of stream.

# Gemfile
source :rubygems

gem 'sinatra'
gem 'thin'

And that’s all. Run the app and you’re done.

ruby motion_stream.rb

Just visit http://localhost:4567/ with your browser :-)

2 comentarios » | Programación, Ruby

Copy files using ruby

abril 13th, 2011 — 6:16pm

I’m translating my usual sysadmining shellscripts into ruby for fun and practice. One thing I hate from Ruby (maybe the only or the most important one) is its documentation. It is zero browseable and friendly, and makes you waste half of your time trying to find on google which module/class does what you need.

One thing I’ve been trying to find is how to copy a file (OMG!).
Can it be so difficult?? … please, try to find it on File class reference.

You have to figure out that exists a module called FileUtils or its old bro (ruby 1.8.6) ftools. Using them you can easily copy, move, rename and make basic file management.
What I find more surprising is that the File class reference does not mention any of them (Uh!?).

Once I found it the job was easy (using ftools as I’m still on Ruby 1.8.7):

require 'ftools'
File.copy source_file, target_file

Comentar » | Ruby

Volver arriba