Skip to content

Instantly share code, notes, and snippets.

@branch14
Created December 16, 2010 15:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save branch14/743495 to your computer and use it in GitHub Desktop.
Save branch14/743495 to your computer and use it in GitHub Desktop.
mplayer.rb
# control mplayer remotely via rack
#
# load & append files (mp3, ogg, etc) & lists (m3u)
# http://localhost:3000/mplayer/loadfile+http://www.archive.org/download/af002/03_Swiss_Jazz.mp3
# http://localhost:3000/mplayer/loadlist+http://www.archive.org/stream/af002
#
# playback
# http://localhost:3000/mplayer/pause
# http://localhost:3000/mplayer/stop
#
# reset
# http://localhost:3000/mplayer/quit
#
# volume control
# http://localhost:3000/mplayer/volume+10+1
# http://localhost:3000/mplayer/volume+60+1
#
module Rack
class Mplayer
HEADERS = { "Content-Type" => "text/plain" }
NAMESPACE = 'mplayer'
def initialize(app)
@app = app
@regexp = %r[^/#{NAMESPACE}/(.*)]
instanciate_mplayer
end
def call(env)
if md = env["PATH_INFO"].match(@regexp)
cmd = CGI::unescape(md.to_a.last)
begin
@in.puts cmd
rescue
instanciate_mplayer
@in.puts cmd
end
[ 200, HEADERS, cmd ]
else
@app.call env
end
end
private
def instanciate_mplayer
cmd = "mplayer -quiet -idle -slave"
@in, @out, @err = Open3.popen3 cmd
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment