A motion-jpeg stream with Ruby and Sinatra

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

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

## ... 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 -= ['.', '..']

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

3 thoughts on “A motion-jpeg stream with Ruby and Sinatra”

  1. Hello,

    I am very interested in your script, but how integrate it into an rails application to streaming a remote ip cam with open-uri and read it in browser ?


  2. Hello,

    You can integrate it in a website by simple using a normal tag. This way:

    img src=’http://your.streaming.server/your/motion/stream’

  3. Hi , I want to make a restreaming an ip camera. In each iteration I order get to image . You seem to ruby the most appropriate solution? or is there a more efficient technology ?

Leave a Reply

Your email address will not be published.