#!/usr/bin/env ruby # -*- encoding: utf-8 -*- # Copyright muflax , 2016 # License: GNU GPLv3 (or later) module MPV class Slave def initialize socket = Dir::Tmpname.make_tmpname "/tmp/mpv-slave", ".socket" @pid = Process.spawn( "mpv", "--idle", "--really-quiet", "--input-unix-socket", socket, ) puts "waiting for mpv to start up..." until File.exists?(socket) or not running? sleep 0.1 end raise "failed to start mpv" if not running? @socket = Socket.new(socket) end def running? begin Process.waitpid(@pid, Process::WNOHANG) == nil rescue false end end # generic commands def command *args response = @socket.command(*args) response end def play file, mode: "replace", wait: true file = File.expand_path file raise "no such file: #{file}" unless File.exists? file command "loadfile", file, mode wait_for "playback-restart" if wait end def add file play file, mode: "append-play", wait: false end def get prop command "get_property", prop end def set prop, value command "set_property", prop, value end def toggle prop set(prop, !get(prop)) end def wait_for event @socket.wait_for event end def wait_while prop, val=true while get(prop) == val sleep 0.01 end end def enable_event event command "enable_event", event end def disable_event event command "disable_event", event end def enable_events enable_event "all" end def disable_events disable_event "all" end def close Process.kill :SIGTERM, @pid Process.wait @pid @socket.close end end end