Page 1 of 1

mpv.dll perfect picture/video control

Posted: 22 Mar 2023, 06:54
by Tulip
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) }
}

Re: mpv.dll perfect picture/video control

Posted: 23 Mar 2023, 09:02
by Tulip
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.

Re: mpv.dll perfect picture/video control

Posted: 23 Mar 2023, 12:20
by malcev
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

Re: mpv.dll perfect picture/video control

Posted: 13 May 2023, 14:46
by sanmaodo
There's something wrong with me. Can someone tell me why? :(
11.png
11.png (58.41 KiB) Viewed 2016 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) }
}

Re: mpv.dll perfect picture/video control

Posted: 14 May 2023, 06:10
by swagfag
probably was meant to be StrPtr, not buf

Re: mpv.dll perfect picture/video control

Posted: 14 May 2023, 06:49
by sanmaodo
@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.

Re: mpv.dll perfect picture/video control

Posted: 14 May 2023, 08:41
by malcev
Try to use strput instead of strbuf.
Tulip, why You didnot write which archive need to download to get libmpv-2.dll?

Re: mpv.dll perfect picture/video control

Posted: 14 May 2023, 12:13
by neogna2
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.

Re: mpv.dll perfect picture/video control

Posted: 14 May 2023, 12:17
by sanmaodo
@malcev Thanks!
I tried changing to strput, and tested all types of dlls in the website, but it still doesn't work,

Re: mpv.dll perfect picture/video control

Posted: 14 May 2023, 12:33
by malcev
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.

Re: mpv.dll perfect picture/video control

Posted: 14 May 2023, 14:14
by Tulip
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
}

Re: mpv.dll perfect picture/video control

Posted: 14 May 2023, 14:39
by sanmaodo
@Tulip Thank you so much!Everything is fine now. :P

Re: mpv.dll perfect picture/video control

Posted: 17 May 2023, 05:06
by sanmaodo
Tulip, Do you know how to make mpv receive keypress information, and how to display osc panel?
These problems seem intractable with no solutions.

Re: mpv.dll perfect picture/video control

Posted: 19 May 2023, 03:57
by sanmaodo
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")