From 7a59a12b764369cebda0e5097401856a8c0e0a6a Mon Sep 17 00:00:00 2001 From: Guido Cella Date: Tue, 31 Dec 2024 10:26:10 +0100 Subject: [PATCH] select.lua: select files with watch later files with g-w Implement selection of files to resume playing from watch later config files. Requires --write-filename-in-watch-later-config. --- DOCS/man/mpv.rst | 5 ++++ etc/input.conf | 1 + player/lua/select.lua | 56 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/DOCS/man/mpv.rst b/DOCS/man/mpv.rst index 22f5f951fded5..7c67cf0d96d18 100644 --- a/DOCS/man/mpv.rst +++ b/DOCS/man/mpv.rst @@ -328,6 +328,11 @@ g-l g-d Select an audio device. +g-w + Select a file from watch later config files (see `RESUMING PLAYBACK`_) to + resume playing. Requires ``--write-filename-in-watch-later-config``. This + doesn't work with ``--ignore-path-in-watch-later-config``. + g-b Select a defined input binding. diff --git a/etc/input.conf b/etc/input.conf index 3f712d430a39b..2faa88c233b28 100644 --- a/etc/input.conf +++ b/etc/input.conf @@ -187,6 +187,7 @@ #g-e script-binding select/select-edition #g-l script-binding select/select-subtitle-line #g-d script-binding select/select-audio-device +#g-w script-binding select/select-watch-later #g-b script-binding select/select-binding #g-r script-binding select/show-properties diff --git a/player/lua/select.lua b/player/lua/select.lua index e4ab95bbaef4f..7f20602aff098 100644 --- a/player/lua/select.lua +++ b/player/lua/select.lua @@ -353,6 +353,62 @@ mp.add_key_binding(nil, "select-audio-device", function () }) end) +mp.add_key_binding(nil, "select-watch-later", function () + local watch_later_dir = mp.get_property("current-watch-later-dir") + + if not watch_later_dir then + show_warning("No watch later files found.") + return + end + + local watch_later_files = {} + + for i, file in ipairs(utils.readdir(watch_later_dir, "files") or {}) do + watch_later_files[i] = watch_later_dir .. "/" .. file + end + + if #watch_later_files == 0 then + show_warning("No watch later files found.") + return + end + + local files = {} + for _, watch_later_file in pairs(watch_later_files) do + local file_handle = io.open(watch_later_file) + if file_handle then + local line = file_handle:read() + if line and line ~= "# redirect entry" and line:find("^#") then + files[#files + 1] = {line:sub(3), utils.file_info(watch_later_file).mtime} + end + file_handle:close() + end + end + + if #files == 0 then + show_warning(mp.get_property_native("write-filename-in-watch-later-config") + and "No watch later files found." + or "Enable --write-filename-in-watch-later-config to select recent files.") + return + end + + table.sort(files, function (i, j) + return i[2] > j[2] + end) + + local items = {} + for i, file in ipairs(files) do + items[i] = os.date("(%Y-%m-%d) ", file[2]) .. file[1] + end + + input.select({ + prompt = "Select a file:", + items = items, + submit = function (i) + mp.commandv("loadfile", files[i][1]) + end, + }) +end) + mp.add_key_binding(nil, "select-binding", function () local bindings = {}