VideoHelp Forum


Try StreamFab All-in-One and rip streaming video! Or Try DVDFab and copy Blu-rays! or rip iTunes movies!
+ Reply to Thread
Results 1 to 7 of 7
Thread
  1. Member
    Join Date
    Mar 2021
    Location
    Granada, Espaņa
    Search Comp PM
    Hello all,

    I want to use MPV player to perform manual tracking of object. I simply want to track the mouse click on a video window while a movie is running and copy to clipboard the video time (in number of frames if possible), the x and y absolute position (respect to the image corner so the value should not be affected by the zoom or the pan of the image).

    I see this code to obtain the time and I try to modify to also obtain the x and y position based on another ".lua" script I find in the web but obviously it doesnīt run (I am not programmer):
    ------------------------------------------------------------------------------------------------------------------------
    require 'mp'

    local function set_clipboard(text)
    mp.commandv("run", "powershell", "set-clipboard", text);
    end

    local function copyTime()
    local time_pos = mp.get_property_number("time-pos")
    local time_in_seconds = time_pos
    local time_seg = time_pos % 60
    time_pos = time_pos - time_seg
    local time_hours = math.floor(time_pos / 3600)
    time_pos = time_pos - (time_hours * 3600)
    local time_minutes = time_pos/60
    time_seg,time_ms=string.format("%.09f", time_seg):match"([^.]*).(.*)"
    time = string.format("%02d:%02d:%02d.%s", time_hours, time_minutes, time_seg, time_ms)
    mp.osd_message(string.format("Copied to Clipboard: %s", time))
    set_clipboard(time)
    end

    mp.add_key_binding("c", "copyTime", copyTime)
    -----------------------------------------------------------------------------------------------------------------


    I tried to "merge" this code and this other one code:
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------
    -[[
    usage: place script into $HOME/.mpv/lua/ directory (mpv 0.4 and later)
    or run mpv --lua path/to/capture.lua
    or add "lua=/absolute/path/to/capture.lua" to ~/.mpv/config
    keybindings:
    a - capture fragment: at first press script remembers start position, at second outputs command for encoding
    c - crop: first press remembers mouse cursor coords, second outputs crop filter parameters.
    ]]
    capture = {}
    local gp
    if mp.get_property == nil then
    gp = mp.property_get
    else
    gp = mp.get_property
    end
    function capture.handler()
    local c = capture
    if c.start then
    print(string.format("ffmpeg -ss %s -i '%s' -t %s",
    c.start, gp("path"), gp("time-pos")-c.start))
    print(string.format("WebMaster -s %s -t %s '%s'",
    c.start, gp("time-pos")-c.start, gp("path")))
    c.start = nil
    else
    c.start = gp("time-pos")
    end
    end
    function capture.crop()
    local x, y = mp.get_mouse_pos()
    local resX, resY = mp.get_osd_resolution()
    -- print(x .. ":" .. y .. " " .. resX .. "x" .. resY )
    local c = capture
    if c.pos then
    local pos2 = mp.get_mouse_pos()
    local w, h = gp('width'), gp('height')
    print(string.format("crop=%d:%d:%d:%d",
    (x - c.pos[1]) * w / resX, (y - c.pos[2]) * h / resY,
    c.pos[1] * w / resX, c.pos[2] * h / resY))
    c.pos = nil
    else
    c.pos = {x, y}
    end
    end
    --mp.add_key_binding("a", "capture", capture.handler)
    mp.set_key_bindings({
    {"c", capture.crop},
    {"a", capture.handler}
    })
    mp.enable_key_bindings()
    ------------------------------------------------------------------------------------------------------------------------------------------
    So I generate this code:

    --------------------------------------------------------------------------------------------------------------------------------------------
    require 'mp'

    local function set_clipboard(text)
    mp.commandv("run", "powershell", "set-clipboard", text);
    end

    local function copyTime_and_XY()

    local x, y = mp.get_mouse_pos()
    local resX, resY = mp.get_osd_resolution()
    -- print(x .. ":" .. y .. " " .. resX .. "x" .. resY )
    c.pos = {x, y, z}
    mp.osd_message(string.format("Copied to Clipboard: c.pos))
    local time_pos = mp.get_property_number("time-pos")
    local time_in_seconds = time_pos
    local time_seg = time_pos % 60
    time_pos = time_pos - time_seg
    local time_hours = math.floor(time_pos / 3600)
    time_pos = time_pos - (time_hours * 3600)
    local time_minutes = time_pos/60
    time_seg,time_ms=string.format("%.09f", time_seg):match"([^.]*).(.*)"
    time = string.format("%02d:%02d:%02d.%s", time_hours, time_minutes, time_seg, time_ms)
    mp.osd_message(string.format("Copied to Clipboard: %s", time, x, y))
    set_clipboard(time, x, y)

    end

    mp.add_key_binding("c", "copyTime_and_XY", copyTime_and_XY)
    ------------------------------------------------------------------------------------------------------------------

    I attach the files.

    If someone could help me I would appreciate a lot since I am not capable to perform this apparently simple task.

    Thanks,

    Gabriel
    Image Attached Files
    Quote Quote  
  2. - start mpv from command-line, it will report any errors there.

    1) get video time (in number of frames if possible), the x and y absolute mouse position
    try printing the properties to the OSD, console or window title.
    standard video time isn't difficult to obtain.

    2) copy a string to the Windows clipboard.
    certainly there are scripts which can do this.
    Quote Quote  
  3. Member
    Join Date
    Mar 2021
    Location
    Granada, Espaņa
    Search Comp PM
    Hi Butterw,

    Thanks a lot for the fast reply... I have tried to run mpv from the command line but I am not capable. I have no programming knowledge (I only know a little of Visual Basic for Applications, but very little).

    I do not know how to print the properties on the OSD (I do not know what is the OSD)

    The time is easy to obtain, I use the script I sent in the previous message and I obtain it. Do you think it is also possible to obtain the mouse coordinates when clicking? I haven't seen any script for this purpose...

    It would be fantastic to generate a .txt or .csv file with the values of the mouse clicks but I imagine that this is more difficult that only having it in the clipboard.

    Obtaining the x, y and time and having it recorded on the clipboard I would be very happy

    Thanks,

    Gabriel
    Quote Quote  
  4. There is a learning curve with mpv scripting.
    I'm afraid I am not familiar with .lua, and haven't used the mouse-pos property.

    mpv video.mp4 --title="${time-pos} ${mouse-pos}"
    Quote Quote  
  5. Member
    Join Date
    Mar 2021
    Location
    Granada, Espaņa
    Search Comp PM
    Hi Butter,

    Thanks a lot!!

    Wow, I didnīt knew the code was so easy... Where I have to paste this code? This is from the command prompt? Please could you show me a simple code to obtain the coordinates of a video with this path:
    C:\Users\gabri\OneDrive\Escritorio\Slow motion video of vertical jump with synchronized vertical force data.mp4

    I open the command prompt (do you recommend me to do this from the PowerShell?). And then what I have to do?

    I have tried this in the command prompt:

    C:\Users\gabri>C:\zportables\mwp\mpv.exe

    The software open and then I type this:

    C:\Users\gabri>mpv video.mp4 --title="${time-pos} ${mouse-pos}"
    "mpv" no se reconoce como un comando interno o externo,
    programa o archivo por lotes ejecutable.

    The error in english would say: mpv is not recognized as a internal or external command, software or file exectuable (or something similar)...

    Regards
    Quote Quote  
  6. Member
    Join Date
    Mar 2021
    Location
    Granada, Espaņa
    Search Comp PM
    I have tried to open the video from the command prompt but I have a message error:

    C:\zportables\mwp>mpv OP\C:\Users\gabri\OneDrive\Escritorio\idea Software digitalizacion comprimido.mp4/

    Cannot find main.* for any supported scripting backend in: C:\Users\gabri\AppData\Roaming/mpv/scripts/fuera de juego
    Can't load unknown script: C:\Users\gabri\AppData\Roaming/mpv/scripts/copyTime_and_XY - copia.txt
    Can't load unknown script: C:\Users\gabri\AppData\Roaming/mpv/scripts/Capture.txt
    [copyTime_and_XY]
    [copyTime_and_XY] stack traceback:
    [copyTime_and_XY] [C]: at 0x01041a50
    [copyTime_and_XY] Lua error: [string "C:\Users\gabri\AppData\Roaming/mpv/scripts/co..."]:13: unfinished string near '"Copied to Clipboard: c.pos)) '
    Playing: OP\C:\Users\gabri\OneDrive\Escritorio\idea
    [file] Cannot open file 'OP\C:\Users\gabri\OneDrive\Escritorio\idea': Invalid argument
    Failed to open OP\C:\Users\gabri\OneDrive\Escritorio\idea.

    Playing: Software
    [file] Cannot open file 'Software': No such file or directory
    Failed to open Software.

    Playing: digitalizacion
    [file] Cannot open file 'digitalizacion': No such file or directory
    Failed to open digitalizacion.

    Playing: comprimido.mp4/
    [file] Cannot open file 'comprimido.mp4/': No such file or directory
    Failed to open comprimido.mp4/.

    Exiting... (Errors when loading file)
    Quote Quote  
  7. Kawaiiii
    Join Date
    May 2021
    Location
    Italy
    Search Comp PM
    Originally Posted by Gabrivolea View Post
    C:\zportables\mwp>mpv OP\C:\Users\gabri\OneDrive\Escritorio\idea Software digitalizacion comprimido.mp4/
    As you can see here :

    Playing: OP\C:\Users\gabri\OneDrive\Escritorio\idea
    [file] Cannot open file 'OP\C:\Users\gabri\OneDrive\Escritorio\idea': Invalid argument
    Failed to open OP\C:\Users\gabri\OneDrive\Escritorio\idea.
    the issue is that you didn't enclose the file name into quotes, so the command line parser assumes that that first space found in the filename is the end of the argument (since it has no way to differentiate between the same character, a space, if you don't explicitly tell him where the filename ends and so to interpretate that spaces as a part of the filename, that's the reason why quotes are used for filenames).
    As you see the error message explains that MPV can't find a file.. that's because the name for it has cut to the first space in your characters sequence

    Simply enclosing the filename with quotes solve this issue, like this:

    Code:
    "C:\zportables\mwp>mpv" "OP\C:\Users\gabri\OneDrive\Escritorio\idea Software digitalizacion comprimido.mp4"
    (better to add quotes every time a filename/path is involved: if the name of the directory where your MPV is launched from had contained some spaces.. you would got the same issue as with the video file name)

    A better way to do this is to create a new batch file named "mpv_Command.bat" inside the folder where MPV executable is and modify/edit it as below (it is a text file):

    Code:
    "%~dp0\mpv" "%~1"
    pause
    then simply dragging any media to this bat file will launch MPV through the command interface. Between the two "quotes" groups you can provide every mpv parameter you should need, without quotes this time, since they're not file names, and adding a space between every one, quoted parts included.

    Getting to the code issue:

    [copyTime_and_XY] Lua error: [string "C:\Users\gabri\AppData\Roaming/mpv/scripts/co..."]:13: unfinished string near '"Copied to Clipboard: c.pos)) '
    The message points you to line 13.. that contains this:

    mp.osd_message(string.format("Copied to Clipboard: c.pos))
    Here there are three critical errors in one little short single line: first, you didn't closed the quotes for the string part of the argument; second .. you tried to concatenate that string with a table in the wrong way and, last, you completely misunderstood the meaning/purpose of the string.format() LUA function.

    The rest of your script was quite a mess, it had a lot of absolutely no purpose variables assigned and 100% useless operations performed, in addition to various critical errors in the end (essentially you were throwing a lot of things randomly here and there, mixing always apples with airplanes)

    I cleaned and fixed it.

    IF (and ONLY IF) the original function set_clipboard() works (I don't have powershell installed, so I can't verify that myself) my script will put into the clipboard a string with time and x,y mouse coordinates through powershell.

    So here's the script:

    Code:
    require 'mp'
    
    local function set_clipboard(text)
    mp.commandv("run", "powershell", "set-clipboard", text)
    end local function copyTime_and_XY() local x, y = mp.get_mouse_pos() local time_pos = mp.get_property_number("time-pos") local time_in_seconds = time_pos local time_seg = time_pos % 60 time_pos = time_pos - time_seg local time_hours = math.floor(time_pos / 3600) time_pos = time_pos - (time_hours * 3600) local time_minutes = time_pos/60 time_seg,time_ms=string.format("%.09f", time_seg):match"([^.]*).(.*)" time = string.format("%02d:%02d:%02d.%s", time_hours, time_minutes, time_seg, time_ms) mp.osd_message("Copied to Clipboard: t=" .. time .. " - X,Y = (".. x .. ", " .. y .. ")",5) local stringClipboard = string.format("%s", time) .. " - " .. string.format("%s", x) .. "," .. string.format("%s", y) set_clipboard(stringClipboard) end mp.add_forced_key_binding("c", "copyTime_and_XY", copyTime_and_XY)
    note: using mp.add_key_binding() only works if the specified key has a declaration somewhere before. "forced" does the key assignment whatever the case. I used this method to simplify the script, but as practice is always better declaring input commands in the "input.conf" and linking them to the script with script-message or script-binding options
    Quote Quote  



Similar Threads