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
+ Reply to Thread
Results 1 to 7 of 7
-
-
- 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. -
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 -
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}" -
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 -
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) -
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.
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"
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
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)) '
mp.osd_message(string.format("Copied to Clipboard: c.pos))
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)
Similar Threads
-
Your recommended Video Organizer, local playback only, NO streaming
By lingyi in forum Newbie / General discussionsReplies: 8Last Post: 8th Jan 2021, 14:28 -
Trouble using subtitle edit with srt and video residing on local N.A.S.
By hibbs in forum SubtitleReplies: 5Last Post: 26th Jul 2020, 21:07 -
Playing local video files
By victor in forum Newbie / General discussionsReplies: 12Last Post: 27th May 2020, 02:11 -
How would I broadcast my video library LIVE over the local network?
By ProxyCell in forum Newbie / General discussionsReplies: 1Last Post: 18th Dec 2017, 15:45 -
Why does local TV news video often look so bad?
By brassplyer in forum EditingReplies: 5Last Post: 20th Mar 2016, 08:36