mpv icon indicating copy to clipboard operation
mpv copied to clipboard

Please don't use powershell Get-Clipboard in console.lua. It can take up to 1x seconds on first run

Open Mark-Joy opened this issue 1 year ago • 4 comments

mpv Information

mpv v0.38.0-476-gd2bd77ad Copyright © 2000-2024 mpv/MPlayer/mplayer2 projects
 built on Jun  9 2024 00:05:18
libplacebo version: v7.349.0 (v7.349.0-rc1-2-gbc9de9c-dirty)
FFmpeg version: N-115628-g658439934
FFmpeg library versions:
   libavutil       59.21.100
   libavcodec      61.7.100
   libavformat     61.3.104
   libswscale      8.2.100
   libavfilter     10.2.102
   libswresample   5.2.100

Other Information

  • Source of mpv: Windows builds by shinchiro
  • Windows version: Windows 10
  • GPU model, driver and version: Intel HD Graphics 530
  • CPU: Intel core i3-6100

Reproduction Steps

  • Restart computer, wait for it to completely boot up. Check Task manager to ensure there is no process hogging CPU/Disk/Ram
  • Copy a text like "Hello World"
  • Open mpv > open mpv console (press `) > Paste clipboard text (Press ctrl+v)

Expected Behavior

The clipboard text is pasted with a small delay, like below 1 second..

Actual Behavior

On my system, the clipboard text took 1x seconds (more than 10 seconds) to be pasted into mpv console on first run. This is because console.lua uses powershell command to getclipboard data. On first run, powershell can be really slow.

Log File

output.txt

Sample Files

I pressed ctrl+v at 00:13, text showed up at 00:22

https://github.com/mpv-player/mpv/assets/22109528/8ca34d29-4bfa-48c3-8f32-4c93e2acbbc3

I carefully read all instruction and confirm that I did the following:

  • [X] I tested with the latest mpv version to validate that the issue is not already fixed.
  • [X] I provided all required information including system and mpv version.
  • [X] I produced the log file with the exact same set of files, parameters, and conditions used in "Reproduction Steps", with the addition of --log-file=output.txt.
  • [X] I produced the log file while the behaviors described in "Actual Behavior" were actively observed.
  • [X] I attached the full, untruncated log file.
  • [X] I attached the backtrace in the case of a crash.

Mark-Joy avatar Jun 17 '24 08:06 Mark-Joy

Do win-users have the better native alternatives to powershell?

hooke007 avatar Jun 17 '24 09:06 hooke007

Do win-users have the better native alternatives to powershell?

Implement paste function it in C/C++, then export to lua: https://github.com/sindresorhus/windows-clipboard/blob/0ed96922fd080040135488a9b24be6992155b366/paste.c

int wpaste() {
	OpenClipboard(NULL);

	HANDLE hData = GetClipboardData(CF_UNICODETEXT);
	if (hData) {
		wchar_t *pText = (wchar_t *) GlobalLock(hData);
		if (pText) {
			_setmode(_fileno(stdout), _O_U8TEXT);
			fputws(pText, stdout);
			GlobalUnlock(hData);
		}
	}

	CloseClipboard();
	return 0;
}

or use prebuilt-tool: windows-clipboard or only paste

Mark-Joy avatar Jun 17 '24 09:06 Mark-Joy

Powershell likes to spin up HDDs for some ungodly reason it tries to enumerate them which wakes them up and it can take seconds. I think this is what you see.

I think it is good idea to add internal native method to get clipboard contents, generally spinning up subprocesses in scripts should be avoided, it will always be slower.

kasper93 avatar Jun 17 '24 19:06 kasper93

Native clipboard support is added by https://github.com/mpv-player/mpv/pull/13837. Once the API design is finalized, a win32 implementation can be added. The console then needs to update to use the new API.

na-na-hi avatar Jun 17 '24 20:06 na-na-hi