AnyEvent-MPV
view release on metacpan or search on metacpan
to "no", this time using the "cmd" method, which queues the command, but
instead of waiting for a reply, it immediately returns a condvar that
cna be used to receive results.
This should then cause mpv to start playing the video.
It then again waits two seconds and quits.
Now, just waiting two seconds is rather, eh, unuseful, so let's look at
receiving events (using a somewhat embellished example):
use AnyEvent;
use AnyEvent::MPV;
my $videofile = "xyzzy.mkv";
my $quit = AE::cv;
my $mpv = AnyEvent::MPV->new (
trace => 1,
args => ["--pause", "--idle=yes"],
);
$mpv->start;
$mpv->register_event (start_file => sub {
$mpv->cmd ("set", "pause", "no");
});
$mpv->register_event (end_file => sub {
my ($mpv, $event, $data) = @_;
print "end-file<$data->{reason}>\n";
$quit->send;
});
$mpv->cmd (loadfile => $mpv->escape_binary ($videofile));
$quit->recv;
This example uses a global condvar $quit to wait for the file to finish
playing. Also, most of the logic is now implement in event handlers.
The two events handlers we register are "start-file", which is emitted
by mpv once it has loaded a new file, and "end-file", which signals the
end of a file (underscores are internally replaced by minus signs, so
you cna speicfy event names with either).
In the "start-file" event, we again set the "pause" property to "no" so
the movie starts playing. For the "end-file" event, we tell the main
program to quit by invoking $quit.
This should conclude the basics of operation. There are a few more
examples later in the documentation.
ENCODING CONVENTIONS
As a rule of thumb, all data you pass to this module to be sent to mpv
is expected to be in unicode. To pass something that isn't, you need to
escape it using "escape_binary".
Data received from mpv, however, is *not* decoded to unicode, as data
returned by mpv is not generally encoded in unicode, and the encoding is
usually unspecified. So if you receive data and expect it to be in
unicode, you need to first decode it from UTF-8, but note that this
might fail. This is not a limitation of this module - mpv simply does
not specify nor guarantee a specific encoding, or any encoding at all,
in its protocol.
METHODS
$mpv = AnyEvent::MPV->new (key => value...)
Creates a new "mpv" object, but does not yet do anything. The
support key-value pairs are:
mpv => $path
The path to the mpv binary to use - by default, "mpv" is used
and therefore, uses your "PATH" to find it.
args => [...]
Arguments to pass to mpv. These arguments are passed after the
hardcoded arguments used by this module, but before the
arguments passed ot "start". It does not matter whether you
specify your arguments using this key, or in the "start" call,
but when you invoke mpv multiple times, typically the arguments
used for all invocations go here, while arguments used for
specific invocations (e..g filenames) are passed to "start".
trace => false|true|coderef
Enables tracing if true. In trace mode, output from mpv is
printed to standard error using a "mpv>" prefix, and commands
sent to mpv are printed with a ">mpv" prefix.
If a code reference is passed, then instead of printing to
standard errort, this coderef is invoked with a first arfgument
being either "mpv>" or ">mpv", and the second argument being a
string to display. The default implementation simply does this:
sub {
warn "$_[0] $_[1]\n";
}
on_eof => $coderef->($mpv)
on_event => $coderef->($mpv, $event, $data)
on_key => $coderef->($mpv, $string)
These are invoked by the default method implementation of the
same name - see below.
$string = $mpv->escape_binary ($string)
This module excects all command data sent to mpv to be in unicode.
Some things are not, such as filenames. To pass binary data such as
filenames through a comamnd, you need to escape it using this
method.
The simplest example is a "loadfile" command:
$mpv->cmd_recv (loadfile => $mpv->escape_binary ($path));
$started = $mpv->start (argument...)
Starts mpv, passing the given arguemnts as extra arguments to mpv.
If mpv is already running, it returns false, otherwise it returns a
true value, so you can easily start mpv on demand by calling "start"
just before using it, and if it is already running, it will not be
( run in 0.555 second using v1.01-cache-2.11-cpan-39bf76dae61 )