mpv-player / mpv

🎥 Command line video player
https://mpv.io
Other
28.02k stars 2.88k forks source link

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

Open Mark-Joy opened 3 months ago

Mark-Joy commented 3 months ago

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

Reproduction Steps

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:

hooke007 commented 3 months ago

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

Mark-Joy commented 3 months ago

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

kasper93 commented 3 months ago

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.

na-na-hi commented 3 months ago

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.