AnyEvent-MPV

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

    To load a file, we then send it a "loadfile" command, which accepts, as
    first argument, the URL or path to a video file. To make sure mpv does
    not misinterpret the path as a URL, it was prefixed with ./ (similarly
    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.

    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)

README  view on Meta::CPAN

    various ways of ending playback:

          if ($INPUT eq "mpv/quit") { # should not happen, but allows user to kill etc. without consequence
             $status = 1;
             mpv_init; # try reinit
             last;

          } elsif ($INPUT eq "mpv/idle") { # normal end-of-file
             last;

          } elsif ($INPUT eq "return") {
             $status = 1;
             last;

    Or the code that actually starts playback, once the file is loaded:

       our %SAVE_PROPERTY = (aid => 1, sid => 1, "audio-delay" => 1);
   
       ...

       my $oid = 100;

          } elsif ($INPUT eq "mpv/file-loaded") { # start playing, configure video
             $mpv->cmd ("seek", $playback_start, "absolute+exact") if $playback_start > 0;

             my $target_fps = eval { $mpv->cmd_recv ("get_property", "container-fps") } || 60;
             $target_fps *= play_video_speed_mult;
             set_fps $target_fps;

             unless (eval { $mpv->cmd_recv ("get_property", "video-format") }) {
                $mpv->cmd ("set", "file-local-options/lavfi-complex", "[aid1] asplit [ao], showcqt=..., format=yuv420p [vo]");
             };

             for my $prop (keys %SAVE_PROPERTY) {
                if (exists $PLAYING_STATE->{"mpv_$prop"}) {
                   $mpv->cmd ("set", "$prop", $PLAYING_STATE->{"mpv_$prop"} . "");
                }

                $mpv->cmd ("observe_property", ++$oid, $prop);
             }

             play_video_set_speed;
             $mpv->cmd ("set", "osd-level", "$OSD_LEVEL");
             $mpv->cmd ("observe_property", ++$oid, "osd-level");
             $mpv->cmd ("set", "pause", "no");

             $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.

    Lastly, it sets some global (or per-youtube-uploader) parameters, such
    as speed, and unpauses. Property changes are handled like other input
    events:

          } elsif ($INPUT eq "mpv/property-change") {
             my $prop = $INPUT_DATA->{name};

             if ($prop eq "chapter-metadata") {
                if ($INPUT_DATA->{data}{TITLE} =~ /^\[SponsorBlock\]: (.*)/) {
                   my $section = $1;
                   my $skip;

                   $skip ||= $SPONSOR_SKIP{$_}
                      for split /\s*,\s*/, $section;

                   if (defined $skip) {
                      if ($skip) {
                         # delay a bit, in case we get two metadata changes in quick succession, e.g.
                         # because we have a skip at file load time.
                         $skip_delay = AE::timer 2/50, 0, sub {
                            $mpv->cmd ("no-osd", "add", "chapter", 1);
                            $mpv->cmd ("show-text", "skipped sponsorblock section \"$section\"", 3000);
                         };
                      } else {
                         undef $skip_delay;
                         $mpv->cmd ("show-text", "NOT skipping sponsorblock section \"$section\"", 3000);
                      }
                   } else {
                      $mpv->cmd ("show-text", "UNRECOGNIZED sponsorblock section \"$section\"", 60000);
                   }
                } else {
                   # cancel a queued skip
                   undef $skip_delay;
                }

             } elsif (exists $SAVE_PROPERTY{$prop}) {
                $PLAYING_STATE->{"mpv_$prop"} = $INPUT_DATA->{data};
                ::state_save;
             }

    This saves back the per-file properties, and also handles chapter
    changes in a hacky way.

    Most of the handlers are very simple, though. For example:

          } elsif ($INPUT eq "pause") {
             $mpv->cmd ("cycle", "pause");
             $PLAYING_STATE->{curpos} = $mpv->cmd_recv ("get_property", "playback-time");
          } elsif ($INPUT eq "right") {
             $mpv->cmd ("osd-msg-bar", "seek",  30, "relative+exact");
          } elsif ($INPUT eq "left") {
             $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") {



( run in 1.061 second using v1.01-cache-2.11-cpan-39bf76dae61 )