mpv.dll perfect picture/video control

Post your working scripts, libraries and tools.
Tulip
Posts: 3
Joined: 22 Mar 2023, 06:50

mpv.dll perfect picture/video control

Post by Tulip » 22 Mar 2023, 06:54

Use mpv as a gif control. It performs better and saves memory compared to ActiveX controls, or using timers and GDIP.

Code: Select all

ui := Gui()
pic := ui.Add("Picture", "w300 h200")
mpv := gif(pic.hwnd, 'loadfile "D:\test.gif" replace loop-file=inf')
ui.show()
return

gif(hwnd, path) {	;https://github.com/shinchiro/mpv-winbuild-cmake/releases
	static _ := DllCall("LoadLibrary", "str", A_AhkDir "\libmpv-2.dll", "Cdecl UPtr")
	mpvhd := DllCall("libmpv-2.dll\mpv_create", "Cdecl UPtr")
	DllCall("libmpv-2.dll\mpv_set_option", "ptr", mpvhd, "astr", "wid", "int64", 4, "ptr*", hwnd, "Cdecl int")
	DllCall("libmpv-2.dll\mpv_initialize", "ptr", mpvhd, "Cdecl int")
	DllCall("libmpv-2.dll\mpv_command_string", "ptr", mpvhd, "ptr", strbuf(StrReplace(path, "\", "\\")), "Cdecl int")
	return { ptr: mpvhd, __Delete: t => DllCall("libmpv-2.dll\mpv_destroy", "ptr", t) }
}

Tulip
Posts: 3
Joined: 22 Mar 2023, 06:50

Re: mpv.dll perfect picture/video control

Post by Tulip » 23 Mar 2023, 09:02

Full-featured player with mpv.dll, replacing VLC, MPC, PotPlayer.
This is a complex subject, but I achieved this in about a thousand lines of code, thus getting rid of MPC and PotPlayer, which I had used for years. it was surprisingly simple, because it was almost indistinguishable from loading a jpg image, with mpvdll taking care of the underlying operations of decoding and drawing, and AutoHotkey taking care of scheduling and interaction. The whole process requires less than 10 mpv-api, and all these functions are just input and output strings, with no complex structure definition and no read/write errors.

Code: Select all

	mpv_com_st(mhd, st) => DllCall("libmpv-2.dll\mpv_command_string", "ptr", mhd, "ptr", strbuf(st), "Cdecl int")
	mpv_set_opt_st(mhd, opt, st) => DllCall("libmpv-2.dll\mpv_set_option_string", "ptr", mhd, "astr", opt, "ptr", strbuf(st), "Cdecl int")

	mpv_set_pro_st(mhd, name, st) => DllCall("libmpv-2.dll\mpv_set_property_string", "ptr", mhd, "astr", name, "ptr", strbuf(st), "Cdecl int")
	mpv_get_pro_st(mhd, name, sz := 10240) => (h := DllCall("libmpv-2.dll\mpv_get_property_string", "ptr", mhd, "astr", name, "ptr", t := Buffer(sz), "Cdecl int")) ? StrGet(h, "utf-8") : ''

	mpv_set_pro(mhd, name, flag, t, ty := "int64P") => DllCall("libmpv-2.dll\mpv_set_property", "ptr", mhd, "astr", name, "int64", flag, ty, &t, "Cdecl int")
	mpv_get_pro(mhd, name, flag, ty := "int64P") => (DllCall("libmpv-2.dll\mpv_get_property", "ptr", mhd, "astr", name, "int64", flag, ty, &t := 0, "Cdecl int"), t)

Setting the volume
mpv_set_pro_st(mhd, "volume", 22)

fast forward
mpv_com_st(mhd, 'osd-bar seek 5')

Get a list of data tracks
mpv_get_pro_st(mhd, "track-list")

The only difficulty to overcome is the need for a separate thread to receive the event callbacks, which requires the creation of a sub-thread using AutoHotkey-H.

If you are unhappy with the player you are using, or if there are any features you cannot implement, I recommend trying to package a player that is entirely yours with mpv.dll.

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: mpv.dll perfect picture/video control

Post by malcev » 23 Mar 2023, 12:20

The only difficulty to overcome is the need for a separate thread to receive the event callbacks
You can try to convert it to V2.
viewtopic.php?f=6&t=21223

sanmaodo
Posts: 45
Joined: 28 Aug 2020, 01:39

Re: mpv.dll perfect picture/video control

Post by sanmaodo » 13 May 2023, 14:46

There's something wrong with me. Can someone tell me why? :(
11.png
11.png (58.41 KiB) Viewed 1675 times

Code: Select all

ui := Gui()
pic := ui.Add("Picture", "w300 h200")
A_AhkDir := "D:\Downloads\mpv-dev-x86_64-v3-20230507-git-a1580b6"
mpv := gif(pic.hwnd, 'loadfile "D:\Pictures\2200.gif" replace loop-file=inf')
ui.show()
return

gif(hwnd, path) {	;https://github.com/shinchiro/mpv-winbuild-cmake/releases
	static _ := DllCall("LoadLibrary", "str", A_AhkDir "\libmpv-2.dll", "Cdecl UPtr")
	mpvhd := DllCall("libmpv-2.dll\mpv_create", "Cdecl UPtr")
	DllCall("libmpv-2.dll\mpv_set_option", "ptr", mpvhd, "astr", "wid", "int64", 4, "ptr*", hwnd, "Cdecl int")
	DllCall("libmpv-2.dll\mpv_initialize", "ptr", mpvhd, "Cdecl int")
	DllCall("libmpv-2.dll\mpv_command_string", "ptr", mpvhd, "ptr", strbuf(StrReplace(path, "\", "\\")), "Cdecl int")
	return { ptr: mpvhd, __Delete: t => DllCall("libmpv-2.dll\mpv_destroy", "ptr", t) }
}

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: mpv.dll perfect picture/video control

Post by swagfag » 14 May 2023, 06:10

probably was meant to be StrPtr, not buf

sanmaodo
Posts: 45
Joined: 28 Aug 2020, 01:39

Re: mpv.dll perfect picture/video control

Post by sanmaodo » 14 May 2023, 06:49

@swagfag Thank you for your repl.
After changing as you said, there will no longer be an error message, but the image cannot be displayed normally.

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: mpv.dll perfect picture/video control

Post by malcev » 14 May 2023, 08:41

Try to use strput instead of strbuf.
Tulip, why You didnot write which archive need to download to get libmpv-2.dll?

neogna2
Posts: 586
Joined: 15 Sep 2016, 15:44

Re: mpv.dll perfect picture/video control

Post by neogna2 » 14 May 2023, 12:13

malcev wrote:
14 May 2023, 08:41
Tulip, why You didnot write which archive need to download to get libmpv-2.dll?
There's a libmpv-2.dll file in the mpv-dev-x86_64 releases on the URL that's in a code comment in OP
https://github.com/shinchiro/mpv-winbuild-cmake/releases
I haven't tried using the code or the dll though.

sanmaodo
Posts: 45
Joined: 28 Aug 2020, 01:39

Re: mpv.dll perfect picture/video control

Post by sanmaodo » 14 May 2023, 12:17

@malcev Thanks!
I tried changing to strput, and tested all types of dlls in the website, but it still doesn't work,

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: mpv.dll perfect picture/video control

Post by malcev » 14 May 2023, 12:33

If You just change strbuf to strput - of course it willnot work.
You need to create buffer and so on...
Look at examples of strput.

Tulip
Posts: 3
Joined: 22 Mar 2023, 06:50

Re: mpv.dll perfect picture/video control

Post by Tulip » 14 May 2023, 14:14

If your CPU is newer, it is recommended to download "mpv-dev-x86_64-v3", otherwise please download "mpv-dev-x86_64"

Code: Select all

strbuf(str, enc := "UTF-8"){
	buf := Buffer(StrPut(str, enc))
	StrPut(str, buf, enc)
	return buf
}

sanmaodo
Posts: 45
Joined: 28 Aug 2020, 01:39

Re: mpv.dll perfect picture/video control

Post by sanmaodo » 14 May 2023, 14:39

@Tulip Thank you so much!Everything is fine now. :P

sanmaodo
Posts: 45
Joined: 28 Aug 2020, 01:39

Re: mpv.dll perfect picture/video control

Post by sanmaodo » 17 May 2023, 05:06

Tulip, Do you know how to make mpv receive keypress information, and how to display osc panel?
These problems seem intractable with no solutions.

sanmaodo
Posts: 45
Joined: 28 Aug 2020, 01:39

Re: mpv.dll perfect picture/video control

Post by sanmaodo » 19 May 2023, 03:57

Adding the following settings, the problem improved.

Code: Select all

mpv_set_opt_st(mpvhd, "input-default-bindings", "yes") 
mpv_set_opt_st(mpvhd, "input-vo-keyboard", "yes") 
mpv_set_opt_st(mpvhd, "osc", "yes")

Post Reply

Return to “Scripts and Functions (v2)”