From 0ee94da7d8a93e456e62585fd9011619b6da2d36 Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Wed, 24 Jan 2024 21:54:32 +0800 Subject: [PATCH] add save screenshot and playlist --- lua/dialog.lua | 77 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 5 deletions(-) diff --git a/lua/dialog.lua b/lua/dialog.lua index 6988d31..8d86c63 100644 --- a/lua/dialog.lua +++ b/lua/dialog.lua @@ -3,6 +3,7 @@ local opts = require('mp.options') local utils = require('mp.utils') +local msg = require('mp.msg') -- user options local o = { @@ -15,6 +16,8 @@ local o = { opts.read_options(o) local open_action = '' +local save_action = '' +local save_arg1 = nil -- open bluray iso or dir local function open_bluray(path) @@ -28,6 +31,36 @@ local function open_dvd(path) mp.commandv('loadfile', 'dvd://') end +-- check if path is url +local function check_url(path) + return type(path) == 'string' and (path:find("^%a[%w.+-]-://") or path:find("^%a[%w.+-]-:%?")) +end + +-- write m3u8 playlist +local function write_playlist(path) + local playlist = mp.get_property_native('playlist') + if #playlist == 0 then return end + + local file, err = io.open(path, 'w') + if not file then + msg.error('save: failed to open file: ' .. err) + return + end + file:write('#EXTM3U\n') + local pwd = mp.get_property("working-directory") + for _, item in ipairs(playlist) do + local fullpath = item.filename + if not check_url(fullpath) then + fullpath = utils.join_path(pwd, fullpath) + end + if item.title and item.title ~= '' then + file:write('#EXTINF:-1, ', item.title, '\n') + end + file:write(fullpath, '\n') + end + file:close() +end + -- open a single file local function open_file(i, path, action) if action == 'add-sub' then @@ -66,6 +99,16 @@ local function open_folder_cb(path) end end +-- save callback +local function save_cb(path) + if save_action == 'screenshot' then + local arg = save_arg1 or 'subtitles' + mp.commandv('screenshot-to-file', path, arg) + elseif save_action == 'playlist' then + write_playlist(path) + end +end + -- clipboard callback local function clipboard_cb(clipboard) mp.osd_message('clipboard: ' .. clipboard) @@ -79,6 +122,7 @@ end -- handle message replies mp.register_script_message('dialog-open-multi-reply', open_cb) mp.register_script_message('dialog-open-folder-reply', open_folder_cb) +mp.register_script_message('dialog-save-reply', save_cb) mp.register_script_message('clipboard-get-reply', clipboard_cb) -- open dialog @@ -93,13 +137,13 @@ mp.register_script_message('open', function(action) open_action = action local filters = {} - if action == 'add-sub' then + if open_action == 'add-sub' then append(filters, 'Subtitle Files', 'subtitle_exts') - elseif action == 'add-video' then + elseif open_action == 'add-video' then append(filters, 'Video Files', 'video_exts') - elseif action == 'add-audio' then + elseif open_action == 'add-audio' then append(filters, 'Audio Files', 'audio_exts') - elseif action == 'bd-iso' or action == 'dvd-iso' then + elseif open_action == 'bd-iso' or open_action == 'dvd-iso' then append_raw(filters, 'ISO Files', '*.iso') else append(filters, 'Video Files', 'video_exts') @@ -108,7 +152,7 @@ mp.register_script_message('open', function(action) append(filters, 'Playlist Files', 'playlist_exts') end - if action ~= 'bd-iso' and action ~= 'dvd-iso' then + if open_action ~= 'bd-iso' and open_action ~= 'dvd-iso' then append_raw(filters, 'All Files', '*.*') end @@ -121,6 +165,29 @@ mp.register_script_message('open-folder', function() mp.commandv('script-message-to', 'menu', 'dialog/open-folder', mp.get_script_name()) end) +-- save dialog +mp.register_script_message('save', function(action, arg1) + save_action = action + save_arg1 = arg1 + if save_action == 'screenshot' then + mp.set_property_native('user-data/menu/dialog/filters', { + { name = 'JPEG Image', spec = '*.jpg' }, + { name = 'PNG Image', spec = '*.png' }, + { name = 'WebP Image', spec = '*.webp' }, + }) + local filename = mp.get_property('filename/no-ext') or ('screenshot-' .. os.time()) + mp.set_property_native('user-data/menu/dialog/default-name', filename) + elseif save_action == 'playlist' then + mp.set_property_native('user-data/menu/dialog/filters', { + { name = 'M3U8 Playlist', spec = '*.m3u8' }, + }) + mp.set_property_native('user-data/menu/dialog/default-name', 'playlist-' .. os.time()) + else + return + end + mp.commandv('script-message-to', 'menu', 'dialog/save', mp.get_script_name()) +end) + -- open clipboard mp.register_script_message('open-clipboard', function(action) open_action = action