class MPV::Server
Represents an active mpv process.
Attributes
@return [Array<String>] the command-line arguments used when spawning mpv
@return [Fixnum] the process id of the mpv process
@return [String] the path to the socket used by this mpv process
Public Class Methods
@return [Boolean] whether `mpv` is executable within the system path
# File lib/mpv/server.rb, line 18 def self.available? Utils.which?("mpv") end
Ensures that a binary named `mpv` can be executed. @raise [MPVNotAvailableError] if no `mpv` executable in the system path
# File lib/mpv/server.rb, line 39 def self.ensure_available! raise MPVNotAvailableError unless available? end
Ensures that that the `mpv` being executed supports the given flag. @raise [MPVNotAvailableError] if no `mpv` executable in the system path @raise [MPVUnsupportedFlagError] if `mpv` does not support the given flag
# File lib/mpv/server.rb, line 46 def self.ensure_flag!(flag) ensure_available! raise MPVUnsupportedFlagError, flag unless flag?(flag) end
@return [Boolean] whether `mpv` supports the given flag @note returns false if `mpv` is not available
# File lib/mpv/server.rb, line 24 def self.flag?(flag) return false unless available? # MPV allows flags to be suffixed with =yes or =no, but doesn't # include these variations in their list. They also allow a --no- # prefix that isn't included in the list, so we normalize these out. # Additionally, we need to remove trailing arguments. normalized_flag = flag.sub(/^--no-/, "--").sub(/=\S*/, "") flags = `mpv --list-options`.split.select { |s| s.start_with?("--") } flags.include?(normalized_flag) end
@param path [String] the path of the socket to be created
(defaults to a tmpname in `/tmp`)
@param user_args [Array<String>] additional arguments to use when
spawning mpv
# File lib/mpv/server.rb, line 55 def initialize(path: File.join('/tmp', Utils.tmpsock), user_args: []) @socket_path = path @args = [ "--idle", "--terminal=no", "--input-ipc-server=%<path>s" % { path: @socket_path }, ].concat(user_args).uniq @args.each { |arg| self.class.ensure_flag! arg } @pid = Process.spawn("mpv", *@args) end
Public Instance Methods
@return [Boolean] whether or not the mpv process is running
# File lib/mpv/server.rb, line 69 def running? !!@pid && Process.waitpid(@pid, Process::WNOHANG).nil? rescue Errno::ECHILD false end