Where developers come to connect, share, build and be inspired.

8

Nginx + secure pseudo-streaming flv files

2213 views

You can use Nginx to serve media files by pseudo-streaming without any specific Media Server software. The good option is to use signature's based URLs with TTL. All that functionality described on the gist and stackoverflow. In result you will use a link:

  http://your_server.com:8080/video/HLz1px_YzSNcbcaskzA6nQ/1326559618/video1.flv

Notes: - easy way to generate URLs on Ruby server-side with secret pass_phrase (shared with nginx only, not for clients) - nginx uses internal private locations without direct access from the outside - no options to scan all files' names - it's short lived URL (1 hour) - signature hard depends on file name and can't be used to access other files

require 'rubygems'
require 'activesupport'
require 'digest/md5'

class Signature
  PASSWORD    = 'YOUR_SECRET_PASSWORD_HERE'
  DIR         = 'flv'
  SERVER_NAME = 'http://your_server.com:8080'
  attr_reader :expiration_time, :file_name, :md5

  def initialize(file_name)
    @expiration_time = (Time.now + 2.hours).to_i
    @file_name       = file_name
    md5_calculate
  end

  def generate
    "#{SERVER_NAME}/video/#{<a href="/md5">@md5</a>}/#{expiration_time}/#{file_name}"
  end


  private

  def md5_calculate
    s    = "#{PASSWORD}#{@expiration_time}/#{DIR}/#{@file_name}"
    a    = Base64.encode64(Digest::MD5.digest(s))
    <a href="/md5">@md5</a> = a.tr("+/", "-_").sub('==', '').chomp
  end
end


url = Signature.new('video1.flv')
puts url.expiration_time #=> 1326559618
puts url.md5             #=> HLz1px_YzSNcbcaskzA6nQ
puts url.generate        #=> http://your_server.com:8080/video/HLz1px_YzSNcbcaskzA6nQ/1326559618/video1.flv

Comments

Add a comment