Skip to content

Instantly share code, notes, and snippets.

@badboy
Created March 5, 2010 16:57
Show Gist options
  • Save badboy/322914 to your computer and use it in GitHub Desktop.
Save badboy/322914 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
#
# simple youtube2mplayer script
# opens mplayer with the specified youtube video
#
# http://gist.github.com/322914
require "open-uri"
require "json"
YOUTUBE_BASE_URL = "http://www.youtube.com/get_video?video_id=%s&t=%s&fmt=%s"
player_command = ENV["player"] || "mplayer"
format = ENV["format"] || "18"
if ARGV.size < 1
$stderr.puts "usage: #{File.basename($0)} [youtube url]"
$stderr.puts "the following options can be given via shell variable:"
$stderr.puts "\tformat (defaults to 18)"
$stderr.puts "\tplayer (defaults to mplayer)"
exit 1
end
url = ARGV.shift
content = open(url).read
swf_args = content.scan(/SWF_ARGS':\s+({.+})/)
if swf_args && swf_args.first
swf_args = swf_args.first.first
end
j_swf_args = JSON.parse(swf_args)
video_id = j_swf_args["video_id"]
t = j_swf_args["t"]
exec player_command, YOUTUBE_BASE_URL % [video_id, t, format]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment