Skip to content

Instantly share code, notes, and snippets.

@CyberShadow
Forked from Hakkin/autosave.lua
Last active June 24, 2025 03:57
Show Gist options
  • Save CyberShadow/2f71a97fb85ed42146f6d9f522bc34ef to your computer and use it in GitHub Desktop.
Save CyberShadow/2f71a97fb85ed42146f6d9f522bc34ef to your computer and use it in GitHub Desktop.
-- autosave.lua
--
-- Periodically saves "watch later" data during playback, rather than only saving on quit.
-- 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 creating a "lua-settings" directory inside your mpv configuration directory.
-- Inside the "lua-settings" directory, create a file named "autosave.conf".
-- The save period can be set like so:
--
-- 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 30 seconds.
local options = require 'mp.options'
local o = {
save_period = 30
}
options.read_options(o)
local mp = require 'mp'
local function save()
mp.commandv("set", "msg-level", "cplayer=warn")
mp.command("write-watch-later-config")
mp.commandv("set", "msg-level", "cplayer=status")
end
local save_period_timer = mp.add_periodic_timer(o.save_period, save)
local function pause(name, paused)
save()
if paused then
save_period_timer:stop()
else
save_period_timer:resume()
end
end
mp.observe_property("pause", "bool", pause)
mp.register_event("file-loaded", save)
local function end_file(data)
if data.reason == 'eof' or data.reason == 'stop' then
local playlist = mp.get_property_native('playlist')
for i, entry in pairs(playlist) do
if entry.id == data.playlist_entry_id then
mp.commandv("delete-watch-later-config", entry.filename)
return
end
end
end
end
mp.register_event("end-file", end_file)
@CyberShadow
Copy link
Author

Yes, you can see it calls save() in the pause function.

@josch
Copy link

josch commented Jun 23, 2025

@Hakkin @CyberShadow what are the conditions for modifying and sharing your work, i.e. what license are you putting your work under and who would I write down as the copy right holder?

@Hakkin
Copy link

Hakkin commented Jun 23, 2025

@josch I wrote the original script this was forked from. My code can be licensed under Zero-Clause BSD.

@CyberShadow
Copy link
Author

My code can be licensed under Zero-Clause BSD.

Same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment