AN3223 / dotfiles

MIT License
35 stars 9 forks source link

Auto-save-state issue #13

Open uugaabuugaa opened 6 months ago

uugaabuugaa commented 6 months ago

As I understand it deletes the saved state when I go to the next file in a playlist. From the config, it says "deleting state (end-file stop)". How can I make it so that it doesn't delete the state when I go to next file in playlist or drop a random file in mpv?

I have kept these enabled in mpv.conf if that matters. keep-open = yes watch-later-options = sid,start,volume,speed,aid save-position-on-quit = yes

AN3223 commented 2 months ago

Hi, sorry, somehow this issue slipped through the cracks.

I'm not sure that what you're asking for is what you really want. If all states are kept then the earliest state (or rather the first state found when traversing the playlist) would be loaded when mpv loads the playlist, if I'm remembering correctly and nothing has changed since then. This is generally not desired behavior, however it's not useless and may be worth implementing if it's what you really want.

uugaabuugaa commented 2 months ago

Hi, sorry, somehow this issue slipped through the cracks.

It's alright because I'd already found a solution after a while.

Since it's been a long time I'm not 100% this is the issue. But IIRC in the case of having multiple files in a playlist, say I go to next file before I've finished watching the current one, it used to delete state of the previous one which I didn't want to happen. So I deleted a few lines in your script so that only when a file is watched completely and moves on to the next file naturally, only then the script deletes the state of that file. My version of script looks like this now:


-- Runs write-watch-later-config periodically

local options = require 'mp.options'
local o = { save_interval = 60 }
options.read_options(o)

local function save()
    if mp.get_property_bool("resume-playback") then
        mp.command("write-watch-later-config")
    end
end

local function save_if_pause(_, pause)
    if pause then save() end
end

local function pause_timer_while_paused(_, pause)
    if pause then timer:stop() else timer:resume() end
end

-- This function runs on file-loaded, registers two callback functions, and 
-- then they run delete-watch-later-config when appropriate.
local function delete_watch_later(event)
    local path = mp.get_property("path")

    -- Temporarily disables save-position-on-quit while eof-reached is true, so 
    -- state isn't saved at EOF when keep-open=yes
    local function eof_reached(_, eof)
        if not can_delete then
            return
        elseif eof then
                        timer:stop()
            print("Deleting state (eof-reached)")
            mp.commandv("delete-watch-later-config", path)
            mp.set_property("save-position-on-quit", "no")
        else
            mp.set_property("save-position-on-quit", "yes")
        end
    end

    mp.observe_property("eof-reached", "bool", eof_reached)
    mp.register_event("end-file", end_file)
end

mp.set_property("save-position-on-quit", "yes")

can_delete = true
mp.register_script_message("skip-delete-state", function() can_delete = false end)

timer = mp.add_periodic_timer(o.save_interval, save)
mp.observe_property("pause", "bool", pause_timer_while_paused)

mp.observe_property("pause", "bool", save_if_pause)
mp.register_event("file-loaded", delete_watch_later)

EDIT: I also added timer:(stop) here so that when there is no more files in the playlist and keep-open=yes is enabled the script doesn't save state anymore on that EOF position after the save interval time passes. Although the script says state isn't saved at EOF when keep-open=yes, but for me it kept saving state after the time of save interval passed at EOF for some reason.


-- Temporarily disables save-position-on-quit while eof-reached is true, so 
-- state isn't saved at EOF when keep-open=yes
local function eof_reached(_, eof)
        if not can_delete then
            return
        elseif eof then
                        timer:stop()
            print("Deleting state (eof-reached)")
            mp.commandv("delete-watch-later-config", path)
            mp.set_property("save-position-on-quit", "no")
        else
            mp.set_property("save-position-on-quit", "yes")
        end
    end