From 442fccbda69b40838d5a2f879e38c849fcf039b4 Mon Sep 17 00:00:00 2001 From: Guido Cella Date: Thu, 26 Dec 2024 23:12:43 +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 | 55 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/DOCS/man/mpv.rst b/DOCS/man/mpv.rst index e82996c3758e5..d252bb315fb02 100644 --- a/DOCS/man/mpv.rst +++ b/DOCS/man/mpv.rst @@ -321,6 +321,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 2407e23f44ed6..299dd1132c5f7 100644 --- a/etc/input.conf +++ b/etc/input.conf @@ -186,6 +186,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 9b8a9ca544985..cfc3f7b58d90d 100644 --- a/player/lua/select.lua +++ b/player/lua/select.lua @@ -353,6 +353,61 @@ 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) + + for i, file in ipairs(files) do + files[i] = file[1] + end + + input.select({ + prompt = "Select a file:", + items = files, + submit = function (i) + mp.commandv("loadfile", files[i]) + end, + }) +end) + mp.add_key_binding(nil, "select-binding", function () local bindings = {}