MPV Video Player and Tk

208 views
Skip to first unread message

Westermeyer

unread,
Jun 28, 2022, 1:25:19 AMJun 28
to
Hello All,

I've got a question about the MPV video player and possible Tk bindings...

From their page, there is a link to the language bindings:
https://github.com/mpv-player/mpv/wiki/Language-bindings

Would it be possible to implement a player in Tcl/Tk?

Thanks

apn

unread,
Jun 29, 2022, 12:17:43 PMJun 29
to
There is an implementation somewhere on the wiki. Not sure how old or
how complete.

Also seems like something you could bind with either cffi or ffidl
without too much effort.

/Ashok

apn

unread,
Jun 29, 2022, 1:08:43 PMJun 29
to
On 6/29/2022 9:47 PM, apn wrote:
> Also seems like something you could bind with either cffi or ffidl
> without too much effort.
>
> /Ashok

To follow up on the above, here is an interactive session to play mp4
using cffi. This is of course very rudimentary, frail and probably not
optimal cffi definitions. A real package for mpv will need considerable
work to understand the api and map it to cffi. Still, just for grins...

(in wish)

% package req cffi
1.0.7
% cd d:/src/libmpv
% dir
D:/src/libmpv:
include libmpv.dll.a mpv-2.dll mpv.def
% cffi::Wrapper create libmpv mpv-2.dll
::libmpv
% libmpv function mpv_create pointer.mpv_handle {}
::mpv_create
% libmpv function mpv_initialize int {ctx pointer.mpv_handle}
::mpv_initialize
% libmpv function mpv_destroy void {ctx {pointer.mpb_handle dispose}}
::mpv_destroy
% set ctx [mpv_create]
0x000001841cb77a00^::mpv_handle
% mpv_initialize $ctx
0
% libmpv function mpv_command int {ctx pointer.mpv_handle args
{pointer[3] nullok}}
::mpv_command
% cd {C:\Users\ashok\Videos\Captures}
% dir
C:/Users/ashok/Videos/Captures:
mpv-test.mp4
% set cmd [cffi::memory fromstring "loadfile"]
0x000001841a32c570^
% set file [cffi::memory fromstring mpv-test.mp4]
0x000001841a32dd10^
% set args [list $cmd $file NULL]
0x000001841a32c570^ 0x000001841a32dd10^ NULL
% mpv_command $ctx $args
0

The video is playing at this point.

The above was translated from the C sample at
https://github.com/mpv-player/mpv-examples/blob/master/libmpv/simple/simple.c

The Rickster

unread,
Jun 30, 2022, 1:43:05 PMJun 30
to
A snit player is documented in the WIKI. It uses mplayer. sample code follows employing version from www.mplayerhq.hu

frame .tMPlayer.$f -class Mplayer -background yellow -container yes {*}$options
set bOK [button .tMPlayer.bOK -text OK]
pack $bOK .tMPlayer.$f -side left
pack propagate .tMPlayer 0
set wid [winfo id .tMPlayer.$f]

# have -colorkey match window background
set channel \
[open [concat | ../shared/snitMP/mplayer.exe -quiet -idle -slave -vo directx -colorkey 0xff0000 -wid $wid $args] r+]
flush $channel
bind .tMPlayer.$f <Destroy> [subst {
catch {
rename .tMPlayer.$f ""
puts $channel quit
flush $channel
close $channel
}
}]
#rename $f "_$f"

proc $f {cmd args} [subst -nocommand {
puts \$cmd
puts \$args
if {\$cmd eq "cmd"} then {
puts $channel \$args
flush $channel
} else {
_$f \$cmd [list \$args]
}
}]

Westermeyer

unread,
Jul 1, 2022, 9:17:52 AMJul 1
to
This is great - thanks for the leads!

et4

unread,
Jul 1, 2022, 6:06:13 PMJul 1
to
If you don't mind running an external program to handle the video, I
would recommend the VLC media player. I've written a remote controller
in tcl/tk for it that runs on windows but I'm sure you could also use
linux. Many of the functions you might want to use are available through
their http interface.

Being an http interface, one could even direct it from another machine.

I use a portable version, and the documentation for how to control it is
found in the readme here:

C:\PortableApps\VLCPortable\App\vlc\lua\http\requests\readme.txt

I wrote mine long ago, and I forget some details. You would have to
enable the http interface and I think you need to set a password too.

If you're interested, I can post a bit of the code I use to send http
requests to VLC.

Westermeyer

unread,
Jul 2, 2022, 1:05:15 AMJul 2
to
That''s a pretty interesting approach, using the portable version might mean a cross-platform solution - I'd be interested to see your code...

Alexandru

unread,
Jul 2, 2022, 3:43:50 AMJul 2
to
Setting up VLC to accept http requests is rather complicated: Activate http, restart VLC, setup a password...
As a personal solution no problem. As part of a public software, no go.

Alexandru

unread,
Jul 2, 2022, 3:49:09 AMJul 2
to
Tcl really lacks a ready to use solution for playing movies in an embeded way.

Christian Gollwitzer

unread,
Jul 2, 2022, 4:36:17 AMJul 2
to
Am 02.07.22 um 09:49 schrieb Alexandru:
The reason is that "playing videos" is a huge and complicated topic. The
easiest way to implement this in a cross-platform way would probably be
ffmpeg. In addition to the technical issues, however, there is an
enormous legal trap involved. Most video codecs are protected by
patents, and integrating ffmpeg into some software to play a video
strictly violates patents and licenses.

The other solution would be to interface the OSes native video solution,
which would be possible for Windows and macOS (there is no such thing
for Linux), and Android. This again means you need to design an
interface that works for all platforms etc. and lots of work to test this.

Christian

et4

unread,
Jul 2, 2022, 5:31:16 AMJul 2
to
Christian and Alexandru have excellent points.

Being retired, I write boss-free dangerous code for my own personal pleasure. With that caveat clearly understood:

Here's a tk example. The comments show how to get the VLC settings right. If you startup a single VLC with some video, the script has 3 buttons to skip and pause. It uses localhost ip and default port built in is 8080.

VLC portable (on windows, I don't use on linux) is self contained in it's own directory tree. The config is there, not in some windows directory. That's how it's portable. (I get the win vlc portable from portableapps.com)

I only run vlc on a fire-walled lan, and so I don't worry about hard-coding in the password, since it's not very secure anyway. Change it below in the argument {password xyz}. Leave username blank.


# vlc example http
# console show
# see this to set config https://wiki.videolan.org/Documentation:Modules/http_intf/
# for pref's all prefs -> main interfaces -> turn on Web, and enter http as shown

# Also, under main interfaces -> Lua, under lua http enter a password, example: xyz
# The username needs to be sent in, but must be blank
#
package require http
package require base64 ;#There's a built-in command now to do this, this code is old

set playerhost 127.0.0.1
set port 8080 ;# vlc default

button .b1 -text "\u2190 3s" -command {skip -1 3} -font {TkDefaultFont 24}
button .b2 -text "\u23f8/\u25b6" -command {pause} -font {TkDefaultFont 24}
button .b3 -text "3s \u2192" -command {skip 1 3} -font {TkDefaultFont 24}

pack .b1 .b2 .b3 -fill both -expand true -side left

proc http_geturl_auth {url {username {}} {password xyz}} { ;# blank user, and password xyz
set auth "Basic [base64::encode $username:$password]"
set headerl [list Authorization $auth]
set tok [http::geturl $url -headers $headerl -timeout 200]
set result [http::data $tok]
http::cleanup $tok
# puts "result= |$result| " ;# lots of player status returned here
return $result
}
proc pause {} {
http_geturl_auth "http://$::playerhost:$::port/requests/status.xml?command=pl_pause" ;# toggle pause/play
}

proc skip {direction secs} {
if { $direction < 0 } {
http_geturl_auth "http://$::playerhost:$::port/requests/status.xml?command=seek&val=-${secs}s"
} else {
http_geturl_auth "http://$::playerhost:$::port/requests/status.xml?command=seek&val=%2B${secs}s"
}
}

Westermeyer

unread,
Jul 2, 2022, 9:17:58 AMJul 2
to
Thanks very much for the suggestions, they're more than I was anticipating. I'm looking for a solution that is cross-platform and part of an educational package, so widely available. I'm not trained as a computer scientist, so I find that Tcl/Tk a bit overwhelming, but it becomes a bit much when the solution looks to involve sophisticated C programming (the cffi package looks promising, and might be a solution, thanks Ashok!).
The closest that I've come to a solution is simply using the default movie player on the user's machine, and this is launched with either auto_execok (win), open (mac), or xdg-open (linux). While not elegant, it is strangely robust...

Westermeyer

unread,
Jul 2, 2022, 1:40:13 PMJul 2
to
#!/bin/sh
# -*- tcl -*-
# The next line is executed by /bin/sh, but not tcl \
exec tclsh "$0" ${1+"$@"}

package require Tk

button .b -text "Launch Video" -command playMovie
button .c -text "Exit" -command exit

proc playMovie {} {

global tcl_platform

if {$tcl_platform(platform) eq "windows"} {

set filename "C:\\Users\\ssche\\Dropbox\\Videos\\Mp4\\test.mp4"
set command [list {*}[auto_execok start] {}]

} elseif {$tcl_platform(os) eq "Darwin"} {

set filename "/Users/stephenscherer/Dropbox/Videos/Mp4/test.mp4"
set command [list open]

} else {

set filename "/home/stephen/Dropbox/Videos/Ogg/test.ogg"
set command [list xdg-open]

}

exec {*}$command $filename &

}

pack .b
pack .c

et4

unread,
Jul 2, 2022, 1:46:25 PMJul 2
to
On 7/2/2022 6:17 AM, Westermeyer wrote:

> The closest that I've come to a solution is simply using the default movie player on the user's machine, and this is launched with either auto_execok (win), open (mac), or xdg-open (linux). While not elegant, it is strangely robust...

Seems like an acceptable solution to me :)

I mentioned VLC because it seems to be the most capable free-ware video player. And with the http interface, I've tinkered together a use for my old android phones, a vlc remote using androwish.

Since you're already launching a player by commandline, you could probably avoid all the config stuff using command line options for the interface and port number. And who knows, maybe some of your users are like me and have VLC as their default video player anyway.


The Rickster

unread,
Jul 5, 2022, 12:58:59 PMJul 5
to
On Monday, June 27, 2022 at 10:25:19 PM UTC-7, Westermeyer wrote:
the educational solution you seem to be seeking is an all tcl\tk training solution which can be seen at www.verbaltransacrions.com. It leverages the simulation training package developed by Advertech, Ltd. It provides for the needs you have described including a video widget, embeddable StroyLine, and browser. Additionally, an of particular relevance, it has been in trial at Microsoft, globally.
Reply all
Reply to author
Forward
0 new messages