AnyEvent-MPV
view release on metacpan or search on metacpan
to "protecting" paths in perls C<open>).
Since commands send I<to> F<mpv> are send in UTF-8, we need to escape the
filename (which might be in any encoding) using the C<esscape_binary>
method - this is not needed if your filenames are just ascii, or magically
get interpreted correctly, but if you accept arbitrary filenamews (e.g.
from the user), you need to do this.
The C<cmd_recv> method then queues the command, waits for a reply and
returns the reply data (or croaks on error). F<mpv> would, at this point,
load the file and, if everything was successful, show the first frame and
pause. Note that, since F<mpv> is implement rather synchronously itself,
do not expect commands to fail in many circumstances - for example, fit
he file does not exit, you will likely get an event, but the C<loadfile>
command itself will run successfully.
To unpause, we send another command, C<set>, to set the C<pause> property
to C<no>, this time using the C<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.
$mpv->cmd ("set_property", "deinterlace", "yes")
if $initial_deinterlace;
There is a lot going on here. First it seeks to the actual playback
position, if it is not at the start of the file (it would probaby be more
efficient to set the starting position before loading the file, though,
but this is good enough).
Then it plays with the display fps, to set it to something harmonious
w.r.t. the video framerate.
If the file does not have a video part, it assumes it is an audio file and
sets a visualizer.
Also, a number of properties are not global, but per-file. At the moment,
this is C<audio-delay>, and the current audio/subtitle track, which it
sets, and also creates an observer. Again, this doesn'T use the observe
functionality of this module, but handles it itself, assigning obsevrer
ids 100+ to temporary/per-file observers.
$mpv->cmd ("osd-msg-bar", "seek", -5, "relative+exact");
} elsif ($INPUT eq "up") {
$mpv->cmd ("osd-msg-bar", "seek", +600, "relative+exact");
} elsif ($INPUT eq "down") {
$mpv->cmd ("osd-msg-bar", "seek", -600, "relative+exact");
} elsif ($INPUT eq "select") {
$mpv->cmd ("osd-msg-bar", "add", "audio-delay", "-0.100");
} elsif ($INPUT eq "start") {
$mpv->cmd ("osd-msg-bar", "add", "audio-delay", "0.100");
} elsif ($INPUT eq "intfwd") {
$mpv->cmd ("no-osd", "frame-step");
} elsif ($INPUT eq "audio") {
$mpv->cmd ("osd-auto", "cycle", "audio");
} elsif ($INPUT eq "subtitle") {
$mpv->cmd ("osd-auto", "cycle", "sub");
} elsif ($INPUT eq "triangle") {
$mpv->cmd ("osd-auto", "cycle", "deinterlace");
Once a file has finished playing (or the user strops playback), it pauses,
unobserves the per-file observers, and saves the current position for to
be able to resume:
to "protecting" paths in perls "open").
Since commands send *to* mpv are send in UTF-8, we need to escape the
filename (which might be in any encoding) using the "esscape_binary"
method - this is not needed if your filenames are just ascii, or
magically get interpreted correctly, but if you accept arbitrary
filenamews (e.g. from the user), you need to do this.
The "cmd_recv" method then queues the command, waits for a reply and
returns the reply data (or croaks on error). mpv would, at this point,
load the file and, if everything was successful, show the first frame
and pause. Note that, since mpv is implement rather synchronously
itself, do not expect commands to fail in many circumstances - for
example, fit he file does not exit, you will likely get an event, but
the "loadfile" command itself will run successfully.
To unpause, we send another command, "set", to set the "pause" property
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.
$mpv->cmd ("set_property", "deinterlace", "yes")
if $initial_deinterlace;
There is a lot going on here. First it seeks to the actual playback
position, if it is not at the start of the file (it would probaby be
more efficient to set the starting position before loading the file,
though, but this is good enough).
Then it plays with the display fps, to set it to something harmonious
w.r.t. the video framerate.
If the file does not have a video part, it assumes it is an audio file
and sets a visualizer.
Also, a number of properties are not global, but per-file. At the
moment, this is "audio-delay", and the current audio/subtitle track,
which it sets, and also creates an observer. Again, this doesn'T use the
observe functionality of this module, but handles it itself, assigning
obsevrer ids 100+ to temporary/per-file observers.
$mpv->cmd ("osd-msg-bar", "seek", -5, "relative+exact");
} elsif ($INPUT eq "up") {
$mpv->cmd ("osd-msg-bar", "seek", +600, "relative+exact");
} elsif ($INPUT eq "down") {
$mpv->cmd ("osd-msg-bar", "seek", -600, "relative+exact");
} elsif ($INPUT eq "select") {
$mpv->cmd ("osd-msg-bar", "add", "audio-delay", "-0.100");
} elsif ($INPUT eq "start") {
$mpv->cmd ("osd-msg-bar", "add", "audio-delay", "0.100");
} elsif ($INPUT eq "intfwd") {
$mpv->cmd ("no-osd", "frame-step");
} elsif ($INPUT eq "audio") {
$mpv->cmd ("osd-auto", "cycle", "audio");
} elsif ($INPUT eq "subtitle") {
$mpv->cmd ("osd-auto", "cycle", "sub");
} elsif ($INPUT eq "triangle") {
$mpv->cmd ("osd-auto", "cycle", "deinterlace");
Once a file has finished playing (or the user strops playback), it
pauses, unobserves the per-file observers, and saves the current
position for to be able to resume:
( run in 0.566 second using v1.01-cache-2.11-cpan-df04353d9ac )