AnyEvent-MPV

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

            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
        started again.

        The arguments passwd to mpv are a set of hardcoded built-in
        arguments, followed by the arguments specified in the constructor,
        followed by the arguments passwd to this method. The built-in
        arguments currently are --no-input-terminal, --really-quiet (or
        --quiet in "trace" mode), and "--input-ipc-client" (or equivalent).

        Some commonly used and/or even useful arguments you might want to
        pass are:

        --idle=yes or --idle=once to keep mpv from quitting when you don't
        specify a file to play.
        --pause, to keep mpv from instantly starting to play a file, in case
        you want to inspect/change properties first.
        --force-window=no (or similar), to keep mpv from instantly opening a
        window, or to force it to do so.
        --audio-client-name=yourappname, to make sure audio streams are
        associated witht eh right program.
        --wid=id, to embed mpv into another application.
        --no-terminal, --no-input-default-bindings, --no-input-cursor,
        --input-conf=/dev/null, --input-vo-keyboard=no - to ensure only you
        control input.

        The return value can be used to decide whether mpv needs
        initializing:

           if ($mpv->start) {
              $mpv->bind_key (...);
              $mpv->cmd (set => property => value);
              ...
           }

        You can immediately starting sending commands when this method
        returns, even if mpv has not yet started.

    $mpv->stop
        Ensures that mpv is being stopped, by killing mpv with a "TERM"
        signal if needed. After this, you can "->start" a new instance
        again.

    $mpv->on_eof
        This method is called when mpv quits - usually unexpectedly. The
        default implementation will call the "on_eof" code reference
        specified in the constructor, or do nothing if none was given.

        For subclassing, see *SUBCLASSING*, below.

    $mpv->on_event ($event, $data)
        This method is called when mpv sends an asynchronous event. The
        default implementation will call the "on_event" code reference
        specified in the constructor, or do nothing if none was given.

        The first/implicit argument is the $mpv object, the second is the
        event name (same as "$data->{event}", purely for convenience), and
        the third argument is the event object as sent by mpv (sans "event"
        key). See List of events
        <https://mpv.io/manual/stable/#list-of-events> in its documentation.

        For subclassing, see *SUBCLASSING*, below.

    $mpv->on_key ($string)
        Invoked when a key declared by "->bind_key" is pressed. The default
        invokes the "on_key" code reference specified in the constructor
        with the $mpv object and the key name as arguments, or do nothing if
        none was given.

        For more details and examples, see the "bind_key" method.

        For subclassing, see *SUBCLASSING*, below.

    $mpv->cmd ($command => $arg, $arg...)
        Queues a command to be sent to mpv, using the given arguments, and
        immediately return a condvar.

        See the mpv documentation
        <https://mpv.io/manual/stable/#list-of-input-commands> for details
        on individual commands.

        The condvar can be ignored:

README  view on Meta::CPAN

       $mpv->cmd ("set", "pause", "yes");

       while ($oid > 100) {
          $mpv->cmd ("unobserve_property", $oid--);
       }

       $PLAYING_STATE->{curpos} = $mpv->cmd_recv ("get_property", "playback-time");

    And thats most of the mpv-related code.

  Gtk2::CV
    Gtk2::CV is low-feature image viewer that I use many times daily because
    it can handle directories with millions of files without falling over.
    It also had the ability to play videos for ages, but it used an older,
    crappier protocol to talk to mpv and used ffprobe before playing each
    file instead of letting mpv handle format/size detection.

    After writing this module, I decided to upgprade Gtk2::CV by making use
    of it, with the goal of getting rid of ffprobe and being ablew to reuse
    mpv processes, which would have a multitude of speed benefits (for
    example, fork+exec of mpv caused the kernel to close all file
    descriptors, which could take minutes if a large file was being copied
    via NFS, as the kernel waited for thr buffers to be flushed on close -
    not having to start mpv gets rid of this issue).

    Setting up is only complicated by the fact that mpv needs to be embedded
    into an existing window. To keep control of all inputs, Gtk2::CV puts an
    eventbox in front of mpv, so mpv receives no input events:

       $self->{mpv} = AnyEvent::MPV->new (
          trace => $ENV{CV_MPV_TRACE},
       );

       # create an eventbox, so we receive all input events
       my $box = $self->{mpv_eventbox} = new Gtk2::EventBox;
       $box->set_above_child (1);
       $box->set_visible_window (0);
       $box->set_events ([]);
       $box->can_focus (0);

       # create a drawingarea that mpv can display into
       my $window = $self->{mpv_window} = new Gtk2::DrawingArea;
       $box->add ($window);

       # put the drawingarea intot he eventbox, and the eventbox into our display window
       $self->add ($box);

       # we need to pass the window id to F<mpv>, which means we need to realise
       # the drawingarea, so an X window is allocated for it.
       $self->show_all;
       $window->realize;
       my $xid = $window->window->get_xid;

    Then it starts mpv using this setup:

       local $ENV{LC_ALL} = "POSIX";
       $self->{mpv}->start (
          "--no-terminal",
          "--no-input-terminal",
          "--no-input-default-bindings",
          "--no-input-cursor",
          "--input-conf=/dev/null",
          "--input-vo-keyboard=no",

          "--loop-file=inf",
          "--force-window=yes",
          "--idle=yes",

          "--audio-client-name=CV",

          "--osc=yes", # --osc=no displays fading play/pause buttons instead

          "--wid=$xid",
       );

       $self->{mpv}->cmd ("script-message" => "osc-visibility" => "never", "dummy");
       $self->{mpv}->cmd ("osc-idlescreen" => "no");

    It also prepares a hack to force a ConfigureNotify event on every vidoe
    reconfig:

       # force a configurenotify on every video-reconfig
       $self->{mpv_reconfig} = $self->{mpv}->register_event (video_reconfig => sub {
          my ($mpv, $event, $data) = @_;

          $self->mpv_window_update;
       });

    The way this is done is by doing a "dummy" resize to 1x1 and back:

       $self->{mpv_window}->window->resize (1, 1),
       $self->{mpv_window}->window->resize ($self->{w}, $self->{h});

    Without this, mpv often doesn't "get" the correct window size. Doing it
    this way is not nice, but I didn't fine a nicer way to do it.

    When no file is being played, mpv is hidden and prepared:

       $self->{mpv_eventbox}->hide;

       $self->{mpv}->cmd (set_property => "pause" => "yes");
       $self->{mpv}->cmd ("playlist_remove", "current");
       $self->{mpv}->cmd (set_property => "video-rotate" => 0);
       $self->{mpv}->cmd (set_property => "lavfi-complex" => "");

    Loading a file is a bit more complicated, as bluray and DVD rips are
    supported:

       if ($moviedir) {
          if ($moviedir eq "br") {
             $mpv->cmd (set => "bluray-device" => $path);
             $mpv->cmd (loadfile => "bd://");
          } elsif ($moviedir eq "dvd") {
             $mpv->cmd (set => "dvd-device" => $path);
             $mpv->cmd (loadfile => "dvd://");
          }
       } elsif ($type eq "video/iso-bluray") {
          $mpv->cmd (set => "bluray-device" => $path);
          $mpv->cmd (loadfile => "bd://");
       } else {
          $mpv->cmd (loadfile => $mpv->escape_binary ($path));



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