MPV::Simple::JSON
use strict; use warnings; use utf8; use MPV::Simple::JSON; use Tcl::Tk; # IMPORTANT: Because the mpv process is in MPV::Simple::JSON multithreaded # you MUST create the MPV::Simple::Object before creating the TCL interpreter # and before doing any GUI work!!! my $mpv = MPV::Simple::JSON->new(event_handling => 1); my $int = Tcl::Tk->new(); my $mw = $int->mainwindow(); $mw->title("MPV::Simple example"); # Create the video frame my $f = $mw->Frame(-width => 640, -height => 480)->pack(-expand =>1,-fill => "both"); # With the MPV property "wid" you can embed MPV in a foreign window $mpv->set_property_string("wid",$f->id()); # The video shall start paused here $mpv->set_property_string('pause','yes'); # Load a video file $mpv->command("loadfile", "./einladung2.mp4"); # For handling events you must repeatly call a event handler. # I think, it is enough to call the event handler every 500/1000ms $int->call('after',1000,\&handle_events); my $b1 = $mw->Button( -text => "Play", -command => sub {$mpv->set_property_string('pause','no')} )->pack(-side => 'left'); my $b2 = $mw->Button( -text => "Pause", -command => sub {$mpv->set_property_string('pause','yes')} )->pack(-side => 'left'); my $b3 = $mw->Button( -text => "Backward", -command => sub {$mpv->command('seek',-5)} )->pack(-side => 'left'); my $b4 = $mw->Button( -text => "Forward", -command => sub {$mpv->command('seek',5)} )->pack(-side => 'left'); my $b5 = $mw->Button( -text => "Close", -command => sub {$mpv->terminate_destroy();$mw->destroy();} )->pack(-side => 'left'); $int->MainLoop; # Event handler # If you set $opt{event_handling} to a true value in the constructor # the events are sent through a non-blocking pipe ($mpv->{evreader}) you can access # by the method $mpv->get_events(); which returns a hashref of the event # The event_ids can be translated to the event names with the global array # $MPV::Simple::event_names[$id] sub handle_events { while ( my $event = $mpv->get_events() ) { if ($event->{event} eq "property-change") { print "prop ".$event->{name}." changed to ".$event->{data}." %\n"; } else { print $event->{event}."\n"; } } # Don't forget to call the event handler repeatly $int->call('after',1000,\&handle_events); }
With this pure perl module you can use the mpv media player through the JSON IPC interface (see https://mpv.io/manual/stable/#json-ipc). This is useful to integrate mpv in a foreign event loop, especially to interact with GUI toolkits. The module give access to the same methods as MPV::Simple. Furthermore, if the option $opt{event_handling} is passed to a true value, events are passed trough a pipe ($mpv->{evreader}) which can be accessed by $mpv->get_events(). In this case you can and must handle the events by a repeatly call of a subroutine. See the example above.
The following methods exist. See MPV::Simple for a detailled description. MPV::Simple::JSON orientates itself as far as possible to the original JSON IPC interface. Thererfore there are some differences to MPV::Simple and MPV::Simple::Pipe which are described hereafter. Furthermore you don't have to initialize the mpv player (and apart from that cannot).
my $mpv = MPV::Simple->new() IMPORTANT: Because the mpv process is in MPV::Simple::JSON multithreaded you MUST create the MPV::Simple::Object before creating the TCL interpreter and before doing any GUI work!!!
$mpv->set_property_string('name','value');
my $ret = $mpv->get_property_string('name'); IMPORTANT: the return value is contrary to MPV::Simple or MPV::Simple::Pipe a hashref. You can access the returned value with $ret-{data}.
$ret-
$mpv->observe_property_string(id,'name'); IMPORTANT: As Contrary to MPV::Simple and MPV::Simple::Pipe the order of the arguments is inverted, first id, then the name of the property.
$mpv->unobserve_property(registered_id);
$mpv->command($command, @args);
$mpv->terminate_destroy() Note: After terminating you cannot use the MPV object anymore. Instead you have to create a new MPV object.
Every MPV method will send back a hashref as a reply indicating whether the command was run correctly, and an additional field holding the command-specific return data (it can also be null). The error key can be accessed with $return-{error}> and is "success" if everything went well. See https://mpv.io/manual/stable/#json-ipc for details of the protocoll.
$return-
See also the manual of the mpv media player in http://mpv.io and especially the description of the JSON IPC at https://mpv.io/manual/stable/#json-ipc.
2 POD Errors
The following errors were encountered while parsing the POD:
'=item' outside of any '=over'
You forgot a '=back' before '=head2'
To install MPV::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MPV::Simple
CPAN shell
perl -MCPAN -e shell install MPV::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.