Created
November 19, 2011 01:07
-
-
Save dtolj/1378252 to your computer and use it in GitHub Desktop.
vlc playlist daemon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'yaml' | |
require 'socket' | |
list_file = "~/.music" | |
vlc_socket = "/tmp/vlc.sock" | |
vlc_path = "/usr/bin/vlc" | |
list_file = File.expand_path(list_file) | |
unless File.exist?(vlc_path) | |
puts "Expected to find VLC program at #{vlc_path}, it wasn't there. Please install VLC from: http://www.videolan.org/vlc/download-macosx.html" | |
exit | |
end | |
if File.exist?(list_file) | |
list = YAML.load_file(list_file).inject({}) do |options, (key, value)| | |
options[key.to_s] = value | |
options | |
end | |
else | |
puts "Please create the YAML format file: #{list_file} so we have some stations to use" | |
puts "Example content:" | |
puts "fav: http://myfavstream.com/stream.pls" | |
exit | |
end | |
class VLC | |
class << self | |
attr_accessor :socket_path | |
attr_accessor :program_path | |
end | |
def self.play(url) | |
self.command('clear') | |
self.command("add #{url}") | |
end | |
def self.stop | |
self.command('stop') | |
end | |
def self.resume | |
self.command('play') | |
end | |
def self.quit | |
self.command('quit') | |
end | |
def self.running? | |
File.exist?(socket_path) | |
end | |
def self.start | |
system("#{program_path} --daemon -I oldrc --rc-unix=#{socket_path} --rc-fake-tty") | |
sleep 1 | |
end | |
def self.command(cmd) | |
socket.puts(cmd) | |
end | |
def self.socket | |
@socket ||= UNIXSocket.new(socket_path) | |
end | |
end | |
VLC.socket_path = vlc_socket | |
VLC.program_path = vlc_path | |
if ARGV[0] == 'start' | |
VLC.start unless VLC.running? | |
exit | |
end | |
if ARGV[0] == 'quit' | |
VLC.quit if VLC.running? | |
exit | |
end | |
if ARGV[0] == 'stop' || ARGV[0] == 'pause' | |
VLC.stop if VLC.running? | |
exit | |
end | |
if ARGV[0] == 'resume' | |
VLC.resume if VLC.running? | |
exit | |
end | |
if ARGV[0] == 'add' | |
name = ARGV[1] | |
url = ARGV[2] | |
if name.nil? || url.nil? | |
puts "Need to pass a name and a url" | |
exit | |
end | |
list[name] = url | |
File.open(list_file, 'w') do |f| | |
f.write list.to_yaml | |
end | |
exit | |
end | |
if ARGV[0] == 'rm' | |
name = ARGV[1] | |
if name.nil? | |
puts "Need to pass a name" | |
exit | |
end | |
list.delete(name) | |
File.open(list_file, 'w') do |f| | |
f.write list.to_yaml | |
end | |
exit | |
end | |
if ARGV[0].nil? | |
puts "Available streams:" | |
list.each do |k,v| | |
puts "#{k} (#{v})" | |
end | |
puts "" | |
puts "- To play the first one: music #{list.keys.first}" | |
puts "- To stop: music stop" | |
puts "- To resume: music resume" | |
puts "- To quit: music quit" | |
puts "" | |
exit | |
end | |
if list[ARGV[0]] | |
VLC.start unless VLC.running? | |
VLC.play(list[ARGV[0]]) | |
exit | |
else | |
puts "Unknown command or stream '#{ARGV[0]}'" | |
exit | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment