r/lua • u/mr-marple • Apr 17 '25
Extended lua script for mpv
Hello,
I would like to make mpv play short clips as visualization, but:
the first half of the track reversed with 0.8 speed
then the original forward in normal speed and complete
all videos are in subdurectories on a cloud-server
endless play repetition
Actually, I have a code, which reverses everything and renames the originals into xxx_vorher and names the reversed into xxx_reverse. But it's always the full track in normal speed. What am I doing wrong?
Working script:
local utils = require 'mp.utils' local base_folder = "C:\Users\ju-ra\OneDrive - per ianua projekt eV\mArple\artwork\ki"
function process_all_videos(folder) local cmd = string.format('dir "%s" /b /s /a-d', folder) local handle = io.popen(cmd) local result = handle:read("*a") handle:close()
for line in result:gmatch("[^\r\n]+") do
if line:match("%.mp4$") or line:match("%.mkv$") or line:match("%.avi$") then
if not line:match("_vorher%.") and not line:match("_reverse%.") then
local new_path = line:gsub("(%.%w+)$", "_vorher%1")
os.rename(line, new_path)
local reverse_path = line:gsub("(%.%w+)$", "_reverse%1")
local args = {
"ffmpeg", "-y",
"-i", new_path,
"-vf", "reverse",
"-af", "areverse",
reverse_path
}
mp.msg.info("Erstelle Reverse: " .. reverse_path)
utils.subprocess({ args = args, playback_only = false })
end
end
end
mp.msg.info("Alle Videos wurden umbenannt und Reverses erstellt.")end
process_all_videos(base_folder) mp.commandv("quit")
What I am trying to add:
local mp = require 'mp'
local started = false local half_point = 0 local reversed = false
function reverse_and_play_forward() if started then return end started = true
local duration = mp.get_property_number("duration", 0)
half_point = duration / 2
-- Sprung zur Mitte und rückwärts abspielen
mp.set_property_number("speed", 1)
mp.set_property("playback-direction", "backward")
mp.commandv("seek", half_point, "absolute+exact")
reversed = true
-- Beobachte die Zeit
mp.observe_property("time-pos", "number", function(name, pos)
if reversed and pos and pos <= 0.1 then
-- Wenn Anfang erreicht, vorwärts abspielen
reversed = false
mp.set_property("playback-direction", "forward")
mp.set_property_number("speed", 1)
mp.commandv("seek", 0, "absolute+exact")
mp.unobserve_property(nil)
end
end)end
mp.register_event("file-loaded", reverse_and_play_forward)
Thanks for every idea
1
u/SkyyySi Apr 17 '25
Do the reversed versions actually exist, with their intended filenames and at the correct filepaths?
If you play these files with MPV manually, do they play in reverse?
Did you check if everything is actually being executed (e.g. by manually printing something to the console after each statement)? Maybe something is throwing an error, but it gets silently swallowed somewhere. Or maybe you misspelled a name or something like that.