From 94f4bf48750e174ad62497f6f38d31315320ce34 Mon Sep 17 00:00:00 2001 From: Dan Zwell Date: Sun, 21 Sep 2025 16:01:03 +0800 Subject: [PATCH] Save resume time to disk periodically This fixes #182. The config option `periodic_save_min_offset` controls this feature. If the offset is 5, then the video position is only saved if it differs by more than 5 seconds from the previous saved video position. This keeps the log file from being constantly filled, particularly when the video is paused. If 0, the feature is turned off. It's necessary to not call `read_log_table()` when restoring history position, because the current history log has been updated, but we really want to load from the history log that was read into memory during initialization. This conflicts with features that modify the history during playback, but I don't use those features so I can't porperly test them. Instead, `read_log_table()` is now called only if the new periodic position logging is turned off. --- script-opts/SimpleHistory.conf | 3 +++ scripts/SimpleHistory.lua | 25 +++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/script-opts/SimpleHistory.conf b/script-opts/SimpleHistory.conf index a6d3656..1069a5d 100644 --- a/script-opts/SimpleHistory.conf +++ b/script-opts/SimpleHistory.conf @@ -13,6 +13,9 @@ toggle_idlescreen=no #--change to 0 so item resumes from the exact position, or decrease the value so that it gives you a little preview before loading the resume point resume_offset=-0.65 +#--periodically save if the current playback position has changed by more than this many seconds. Set to 0 to totally disable periodic saving. This must be disabled to dynamically edit the history file and resume from the edited version. +periodic_save_min_offset=0 + #--yes is for displaying osd messages when actions occur. Change to no will disable all osd messages generated from this script osd_messages=yes diff --git a/scripts/SimpleHistory.lua b/scripts/SimpleHistory.lua index df1b320..1b32ac3 100644 --- a/scripts/SimpleHistory.lua +++ b/scripts/SimpleHistory.lua @@ -14,6 +14,7 @@ local o = { startup_idle_behavior = 'none', --The behavior when mpv launches and nothing is loaded. 'none' for disabled. 'resume' to automatically resume your last played item. 'resume-notime' to resume your last played item but starts from the beginning. toggle_idlescreen = false, --hides OSC idle screen message when opening and closing menu (could cause unexpected behavior if multiple scripts are triggering osc-idlescreen off) resume_offset = -0.65, --change to 0 so item resumes from the exact position, or decrease the value so that it gives you a little preview before loading the resume point + periodic_save_min_offset = 0, --periodically save if the current playback position has changed by more than this many seconds. Set to 0 to totally disable periodic saving. This must be disabled to dynamically edit the history file and resume from the edited version. osd_messages = true, --true is for displaying osd messages when actions occur. Change to false will disable all osd messages generated from this script resume_option = 'notification', --'none': for disabled. 'notification': a message to resume the previous reached time will be triggered. 'force': to forcefully resume last playback based on threshold resume_option_threshold = 2, --0 to always trigger the resume option when the same video has been played previously, a value such as 5 will only trigger the resume option if the last played time starts after 5% of the video and ends before completion by 5% @@ -2183,10 +2184,15 @@ end function history_resume() if filePath == nil then - list_contents = read_log_table() + --# periodic save isn't compatible with the log table being updated during playback + if o.periodic_save_min_offset == 0 then + list_contents = read_log_table() + end load(1) elseif filePath ~= nil then - list_contents = read_log_table() + if o.periodic_save_min_offset == 0 then + list_contents = read_log_table() + end if list_contents ~= nil and list_contents[1] then for i = #list_contents, 1, -1 do if list_contents[i].found_path == filePath and tonumber(list_contents[i].found_time) > 0 then @@ -2220,6 +2226,21 @@ function history_load_last() end end +mp.add_periodic_timer(1, function() + if o.periodic_save_min_offset > 0 and not incognito_mode then + -- Don't fill the log file needlessly, especially if the player is paused + local prevLogTime = logTime + local newLogTime = (mp.get_property_number('time-pos') or 0) + if math.abs(newLogTime - prevLogTime) > o.periodic_save_min_offset then + logTime = newLogTime + local f = io.open(log_fullpath, "a+") + f:write(("[%s] \"%s\" | %s | %s | %s"):format(os.date(o.date_format), fileTitle, filePath, log_length_text .. tostring(fileLength), log_time_text .. tostring(logTime))) + f:write('\n') + f:close() + end + end +end) + mp.register_event('file-loaded', function() list_close_and_trash_collection() filePath, fileTitle, fileLength = get_file()