Skip to content

Instantly share code, notes, and snippets.

@nddrylliog
Last active December 11, 2015 06:49
Show Gist options
  • Save nddrylliog/4562341 to your computer and use it in GitHub Desktop.
Save nddrylliog/4562341 to your computer and use it in GitHub Desktop.
A ruby command-line justin.tv/twitch.tv player. Requires rtmpdump 2.4, uses mplayer by default.
#!/usr/bin/env ruby
require 'rubygems'
require 'json'
require 'faraday'
# Script options
channel=ARGV[0]
stream=ENV['STREAM'] || "live"
player=ENV['PLAYER'] || "mplayer -quiet"
# Get an API request, parse as JSON, return an object
def get(url)
response = Faraday.get url
object = JSON.parse(response.body)
end
# Follow HTTP 302 once, give out proper location
def locate(url)
response = Faraday.head url
response.headers['location'] || url
end
# Usher gives us most rtmpdump parameters we need
response = get "http://usher.justin.tv/find/#{channel}.json?type=#{stream}"
info = response[0] # always return a list, only need the first element
# Justin generates a unique swf address with a built-in token
swf = locate 'http://www.justin.tv/widgets/live_site_player.swf'
# rtmp address components
connect = info['connect']
play = info['play']
rtmp = "#{connect}/#{play}"
# token has embedded json, escape quotes
token = info['token'].gsub('"', '\\"')
cmdline = "rtmpdump -r\"#{rtmp}\" -j\"#{token}\" -W\"#{swf}\" -m5 -o - | #{player} -"
puts cmdline
system(cmdline)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment