Skip to content

Instantly share code, notes, and snippets.

@zhongfly
Last active February 11, 2023 16:42
Show Gist options
  • Save zhongfly/05f07b0392929c6612a8c2a68a51df8d to your computer and use it in GitHub Desktop.
Save zhongfly/05f07b0392929c6612a8c2a68a51df8d to your computer and use it in GitHub Desktop.
mpv script
-- autosave.lua
--
-- Instead of saving only on quit, 'watch later' data is saved periodically during playback and when a file is unloaded.
-- This lets you easily recover your position in the case of an ungraceful shutdown of mpv (crash, power failure, etc.).
--
-- You can configure the save period by editing the follow line.
--
-- local save_period = 60
--
-- This will set the save period to once every 60 seconds of playback, time while paused is not counted towards the save period timer.
-- The default save period is 60 seconds.
-- If you want to disable this script, set the value of save_period to 0.
local save_period = 60
local mp = require 'mp'
local function save()
mp.command("write-watch-later-config")
end
save_period_timer = nil
local function pause(name, paused)
if paused then
save_period_timer:stop()
else
save_period_timer:resume()
end
end
local function loaded(event)
if save_period_timer then
save_period_timer:resume()
else
save_period_timer = mp.add_periodic_timer(save_period, save)
end
mp.observe_property("pause", "bool", pause)
end
local function unload(hook)
if save_period_timer then
save_period_timer:kill()
end
mp.unobserve_property(pause)
save()
end
if (save_period and save_period>0) then
mp.register_event("file-loaded", loaded)
mp.add_hook("on_unload", 50, unload)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment