Skip to content

Instantly share code, notes, and snippets.

@TheDrHax
Forked from Hakkin/autosave.lua
Last active February 7, 2024 18:40
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save TheDrHax/fa2dba9023aace168de029f51799948d to your computer and use it in GitHub Desktop.
Save TheDrHax/fa2dba9023aace168de029f51799948d to your computer and use it in GitHub Desktop.
MPV script that periodically saves "watch later" data during playback
-- 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.command("write-watch-later-config")
end
local function init()
if not mp.get_property_bool("seekable", true) then
return
end
local save_period_timer = mp.add_periodic_timer(o.save_period, save)
local function pause(name, paused)
if paused then
save_period_timer:stop()
else
save_period_timer:resume()
end
end
mp.observe_property("pause", "bool", pause)
end
mp.register_event("file-loaded", init)
@Ione15
Copy link

Ione15 commented Aug 8, 2023

Very nice, i wonder why this is not part of mpv

@Redjard
Copy link

Redjard commented Jan 31, 2024

I was looking for this to make my audiobook mpv instances resilient against system crashes. As per such, a very long timer would be appropriate, since the crash is most likely to hit when the book is paused anyway.
In the current implementation however, a high save_period will lead to the saved progress lagging behind greatly.
A simple approach would be saving when pausing, but when spamming pause this spams save, which does not seem right.

What I have done is move the save_period_timer:stop() into the save function, applying it only after the first save in the paused state. When spamming pause, the timer will continuously reset and not save until the player remains in a paused or playing state for the length of save_period.

6,7c6,7
< -- 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".
---
> -- You can configure the save period by creating a "script-opts" directory inside your mpv configuration directory.
> -- Inside the "script-opts" directory, create a file named "autosave.conf".
24a25,26
> local doStop = false
> local save_period_timer
26a29,33
>   
>   if doStop then
>     save_period_timer:stop()
>     doStop = false
>   end
27a35
> save_period_timer = mp.add_periodic_timer(o.save_period, save)
33,34c41
< 
<   local save_period_timer = mp.add_periodic_timer(o.save_period, save)
38c44
<       save_period_timer:stop()
---
>       doStop = true

This also uses script-opts instead of lua-settings, which has been changed 5.5 years ago with v0.29.0 by now, the old dir generates a warning in mpv

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