Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
-- 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
local mp = require 'mp'
local function save()
mp.commandv("set", "msg-level", "cplayer=warn")
mp.commandv("set", "msg-level", "cplayer=status")
local save_period_timer = mp.add_periodic_timer(o.save_period, save)
local function pause(name, paused)
if paused then
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 == data.playlist_entry_id then
mp.commandv("delete-watch-later-config", entry.filename)
mp.register_event("end-file", end_file)
Copy link

dguan4 commented May 20, 2022

Wanted to ask - how would you implement a matcher to prevent saving for certain file path/names? For instance there's a folder of videos where I always want to start at the beginning regardless of where I end. Thanks for this though - super useful!

Copy link

CyberShadow commented May 21, 2022

Probably by checking mp.get_property("filename") in the save function?

Copy link

dguan4 commented May 22, 2022

Yep I ended up creating another variable in the o object to store a list of Folder names and calling mp.get_property("working-directory") instead and looping through it for excluded filepath/folder names. One thing I couldn't get working is having an override variable in autosave.conf for the excluded names since it's in format {'Test', 'Test2', ...} but it seems to parse the entire thing as a string instead, though I suppose that's more of a lua question. Thanks!

Copy link

CyberShadow commented May 22, 2022

Yeah, I think it reads the file with the same parser as for mpv.conf, which doesn't seem to support things like lists.

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