Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Hakkin
Created July 27, 2017 04:12
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save Hakkin/5489e511bd6c8068a0fc09304c9c5a82 to your computer and use it in GitHub Desktop.
Save Hakkin/5489e511bd6c8068a0fc09304c9c5a82 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.command("write-watch-later-config")
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)
@Hakkin
Copy link
Author

Hakkin commented Nov 1, 2017

@gwarser Unfortunately there's no way to delete watch-later data from user scripts as far as I know, only save it. A "work around" is to re-open the movie (mpv removes the watch-later data when you open the file), then ctrl+c out of mpv, which should exit without re-creating the watch later data.
It might be worth opening an issue to request a way to remove the watch-later data programmatically.

@TheAMM
Copy link

TheAMM commented Dec 31, 2017

You can (ab)use the undocumented mp.find_config_file() to get paths to files near or about the mpv executable or config directories. Here's how mp.options uses it - if you replicate the MD5sum process for generating the watch-later filename, you can find_config_file it and remove it (or, if lazy, clear all files in the watch_later directory).

@jgreco
Copy link

jgreco commented Dec 7, 2018

What about setting the watch-data back to zero instead of deleting it? In other words seek to zero, then save it.

I'm not sure how to actually trigger this though, since seeking won't work from an on_unload hook.

@garoto
Copy link

garoto commented Jan 10, 2019

Unfortunately there's no way to delete watch-later data from user scripts as far as I know, only save it.

Anyone knows if this is by design? Seems odd if true to me.

Edit: The same behavior happens with keybinds in input.conf, like CTRL+Q write-watch-later-config ; show-text "Wrote watch-later data". So programatically invoking the write-watch-later-config function wont erase the data even when doing a simple quit during the same run. Odd indeed.

@john-x
Copy link

john-x commented Feb 3, 2019

Is there a way to suppress the "Saving state" message that prints in the terminal?

@garoto
Copy link

garoto commented Mar 2, 2019

@john-x:

local function save()
	mp.commandv("set", "msg-level", "cplayer=warn")
	mp.command("write-watch-later-config")
	mp.commandv("set", "msg-level", "cplayer=status")

@rodrigo-sys
Copy link

rodrigo-sys commented Apr 18, 2021

still works?
I put the script in mpv/scripts but not seem to work

edit:
I think dont work in folders/playlist

@CyberShadow
Copy link

It might be worth opening an issue to request a way to remove the watch-later data programmatically.

There is now delete-watch-later-config, you can use this version of the script which takes advantage of it:
https://gist.github.com/CyberShadow/2f71a97fb85ed42146f6d9f522bc34ef

More details in the commit message here: mpv-player/mpv@3ccf70b

Copy link

ghost commented Sep 15, 2021

@CyberShadow thank you!

@daxman1129
Copy link

Can you copy an autosave lua script because i can't

@speedace1
Copy link

How to copy it from where should I start copying local options.

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