Search code examples
winapicontrolsmedia-playerautohotkey

media player classic - jump to point in video/audio programmatically


In Media Player Classic I found a way to jump to a point in a video/audio programmatically, avoiding the Go To... box. The jump distances are available at OptionsTweaks, and HKEY_CURRENT_USER\Software\MPC-HC\MPC-HC\Settings (JumpDistL/JumpDistM/JumpDistS).

What I do is find the jump distances in the address space of Media Player Classic, and set the value of the large jump distance such that if you applied it to the elapsed time you would get the desired time.

I then send a WM_COMMAND message with parameter 903/904 (all via AutoHotkey. I get the elapsed time by retrieving/parsing the contents of the Edit control.)

Because the jump is relative to the current point, it is imprecise, and arrives within a second of the right time, but doesn't arrive at exactly the same point each time.

Is there a more direct way of accomplishing this and if not, would any Media Player Classic users/programmers consider discussing on the forum, introducing new WM_COMMAND messages that allow jump to point (in milliseconds), or that retrieve the numerical values listed here (state, position, duration, volumelevel, muted, playbackrate, reloadtime). (The method found here is too slow to get the time accurately, and requires special options be set).


Solution

  • Thanks to the message from wOxxOm, below the question, I have been able to create this AutoHotkey script, which solves my original problem: to set the elapsed time in Media Player Classic programmatically, directly, without using the Go To... box.

    It also solves the problem of retrieving information about the video.

    The hotkeys are:
    - Ctrl+Q to start the MPC API,
    - Ctrl+W to retrieve information,
    - the number keys to jump partway through the video.

    ;==================================================
    
    ^q:: ;start MPC API
    hWnd := A_ScriptHwnd+0
    OnMessage(WM_COPYDATA:=74, "On_WM_COPYDATA")
    ;64-bit
    Run, "C:\Program Files (x86)\K-Lite Codec Pack\MPC-HC64\mpc-hc64.exe" /slave %hWnd%
    ;32-bit
    ;Run, "C:\Program Files (x86)\K-Lite Codec Pack\MPC-HC\mpc-hc.exe" /slave %hWnd%
    Return
    
    ;==================================================
    
    ^w:: ;display information
    Send(vMPCApiHWnd, 0xA0003004, "") ;CMD_GETCURRENTPOSITION := 0xA0003004
    
    vElapsed := 19990101
    vDuration := 19990101
    vElapsed += vMPCApiCurrent, S
    vDuration += vMPCApiDuration, S
    
    if (vMPCApiCurrent >= 3600) OR (vMPCApiDuration >= 3600)
    vFormat := "HH:mm:ss"
    else
    vFormat := "mm:ss"
    
    FormatTime, vElapsed, %vElapsed%, %vFormat%
    FormatTime, vDuration, %vDuration%, %vFormat%
    
    SplitPath, vMPCApiPath, vName, vDir, vExt, vNameNoExt, vDrive
    
    vText = ;continuation section
    (
    title: %vMPCApiTitle%
    author: %vMPCApiAuthor%
    description: %vMPCApiDesc%
    name: %vName%
    path: %vMPCApiPath%
    elapsed: %vElapsed% (%vMPCApiCurrent%)
    duration: %vDuration% (%vMPCApiDuration%)
    )
    
    MsgBox %vText%
    Return
    
    ;==================================================
    
    #IfWinActive, ahk_class MediaPlayerClassicW
    0:: ;skip to point
    1::
    2::
    3::
    4::
    5::
    6::
    7::
    8::
    9::
    vNum := SubStr(A_ThisHotkey, 1-1)
    vElapsed2 := Round(vMPCApiDuration*(vNum/10))
    
    Send(vMPCApiHWnd, 0xA0002000, "" vElapsed2) ;CMD_SETPOSITION := 0xA0002000
    Return
    #IfWinActive
    
    ;==================================================
    
    On_WM_COPYDATA(wParam, lParam, msg, hwnd)
    {
    global vMPCApiHWnd
    global vMPCApiTitle
    global vMPCApiAuthor
    global vMPCApiDesc
    global vMPCApiPath
    global vMPCApiDuration
    global vMPCApiCurrent
    
    dwData := NumGet(lParam+0, 0)
    cbData := NumGet(lParam+A_PtrSize)
    lpData := NumGet(lParam + 2*A_PtrSize)
    lpData := StrGet(lpData)
    
    if (dwData = 0x50000000) ;CMD_CONNECT := 0x50000000
    {
    vMPCApiHWnd := lpData
    WinGetClass, vWinClass, ahk_id %vMPCApiHWnd%
    if (vWinClass = "MediaPlayerClassicW")
    MsgBox, , , MPC API on, 3
    }
    
    if (dwData = 0x50000003) ;CMD_NOWPLAYING := 0x50000003
    {
    StringSplit, lpData, lpData, |
    vMPCApiTitle := lpData1
    vMPCApiAuthor := lpData2
    vMPCApiDesc := lpData3
    vMPCApiPath := lpData4
    vMPCApiDuration := lpData5
    }
    
    if (dwData = 0x50000007) ;CMD_CURRENTPOSITION := 0x50000007
    vMPCApiCurrent := lpData
    
    Return true
    }
    
    ;==================================================
    
    Send(Hwnd, dwData, lpData)
    {
    static WM_COPYDATA := 0x4a
    
    VarSetCapacity(COPYDATASTRUCT, 3*A_PtrSize, 0)
    cbData := (StrLen(lpData) + 1) * (A_IsUnicode ? 2 : 1)
    NumPut(dwData, COPYDATASTRUCT, 0)
    NumPut(cbData, COPYDATASTRUCT, A_PtrSize)
    NumPut(&lpData, COPYDATASTRUCT, 2*A_PtrSize)
    
    SendMessage, % WM_COPYDATA, % A_ScriptHwnd , &COPYDATASTRUCT,, % "ahk_id " Hwnd
    return ErrorLevel == "FAIL" ? false : true
    }
    
    ;==================================================
    
    ;USEFUL LINKS
    ;Sending Strings Via SendMessage - Ask for Help - AutoHotkey Community
    ;https://autohotkey.com/board/topic/98334-sending-strings-via-sendmessage/
    
    ;Media Player Classic - Homecinema MPC remote API (via WM_COPYDATA) - AutoIt Example Scripts - AutoIt Forums
    ;https://www.autoitscript.com/forum/topic/85354-media-player-classic-homecinema-mpc-remote-api-via-wm_copydata/
    
    ;mpcapi.h
    ;https://raw.githubusercontent.com/jeeb/mpc-be/master/src/apps/mplayerc/mpcapi.h
    
    ;winapi - media player classic - jump to point in video/audio programmatically - Stack Overflow
    ;http://stackoverflow.com/questions/41310778/media-player-classic-jump-to-point-in-video-audio-programmatically
    
    ;==================================================
    

    USEFUL LINKS:

    Sending Strings Via SendMessage - Ask for Help - AutoHotkey Community
    https://autohotkey.com/board/topic/98334-sending-strings-via-sendmessage/

    Media Player Classic - Homecinema MPC remote API (via WM_COPYDATA) - AutoIt Example Scripts - AutoIt Forums
    https://www.autoitscript.com/forum/topic/85354-media-player-classic-homecinema-mpc-remote-api-via-wm_copydata/

    mpcapi.h
    https://raw.githubusercontent.com/jeeb/mpc-be/master/src/apps/mplayerc/mpcapi.h