have an mpv lua script that switches images when a timestamp is reached while playing music.
run: mpv --force-window --no-border --window-maximized --no-config --script=script.lua music.mp3
script.lua:
local last_img
local last_time = 0.0
local function show(img)
if last_img == img then return end
last_img = img
mp.commandv("video-remove", 1)
mp.commandv("video-add", img)
mp.set_property("vid", "1")
end
local timeline = {
{ time = 0.01, action = function() show("image1.jpg") end },
{ time = 3.5, action = function() show("image2.jpg") end },
{ time = 10.5, action = function() show("image3.jpg") end },
}
local function on_time_update(name, current_time)
if not current_time then return end
local is_seeking = math.abs(current_time - last_time) > 1.5
if is_seeking then
local last_valid_action = nil
for _, event in ipairs(timeline) do
if current_time >= event.time then
last_valid_action = event.action
else
break
end
end
if last_valid_action then last_valid_action() end
else
for _, event in ipairs(timeline) do
if last_time < event.time and current_time >= event.time then
event.action()
end
end
end
last_time = current_time
end
local function on_file_loaded()
last_time = 0.0
end
mp.observe_property("time-pos", "number", on_time_update)
mp.register_event("file-loaded", on_file_loaded)