From d10672ef3db5c9d9cbb63318c61989bcd86d7f94 Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Sat, 20 Jan 2024 21:48:48 +0800 Subject: [PATCH] add more open actions support --- lua/dialog.lua | 128 ++++++++++++++++++++++++------------------------- 1 file changed, 62 insertions(+), 66 deletions(-) diff --git a/lua/dialog.lua b/lua/dialog.lua index 34244a0..a9cbcce 100644 --- a/lua/dialog.lua +++ b/lua/dialog.lua @@ -3,85 +3,57 @@ local utils = require('mp.utils') --- pre-defined file types +-- common file extensions local file_types = { - video = table.concat({ '*.mpeg4', '*.m4v', '*.mp4', '*.mp4v', '*.mpg4', '*.h264', '*.avc', '*.x264', '*.264', - '*.hevc', '*.h265', '*.x265', '*.265', '*.m2ts', '*.m2t', '*.mts', '*.mtv', '*.ts', '*.tsv', '*.tsa', '*.tts', - '*.mpeg', '*.mpg', '*.mpe', '*.mpeg2', '*.m1v', '*.m2v', '*.mp2v', '*.mkv', '*.mk3d', '*.wm', '*.wmv', '*.asf', - '*.vob', '*.vro', '*.evob', '*.evo', '*.ogv', '*.ogm', '*.ogx', '*.webm', '*.avi', '*.vfw', '*.divx', '*.3iv', - '*.xvid', '*.nut', '*.flic', '*.fli', '*.flc', '*.nsv', '*.gxf', '*.mxf', '*.dvr-ms', '*.dvr', '*.wtv', '*.dv', - '*.hdv', '*.flv', '*.f4v', '*.qt', '*.mov', '*.hdmov', '*.rm', '*.rmvb', '*.3gpp', '*.3gp', '*.3gp2', '*.3g2', - '*.yuv', '*.y4m' }, ';'), - audio = table.concat({ '*.mp3', '*.m4a', '*.aac', '*.flac', '*.ac3', '*.a52', '*.eac3', '*.mpa', '*.m1a', '*.m2a', - '*.mp1', '*.mp2', '*.oga', '*.ogg', '*.wav', '*.mlp', '*.dts', '*.dts-hd', '*.dtshd', '*.true-hd', '*.thd', - '*.truehd', '*.thd+ac3', '*.tta', '*.pcm', '*.aiff', '*.aif', '*.aifc', '*.amr', '*.awb', '*.au', '*.snd', - '*.lpcm', '*.ape', '*.wv', '*.shn', '*.adts', '*.adt', '*.opus', '*.spx', '*.mka', '*.weba', '*.wma', '*.f4a', - '*.ra', '*.ram', '*.3ga', '*.3ga2', '*.ay', '*.gbs', '*.gym', '*.hes', '*.kss', '*.nsf', '*.nsfe', '*.sap', - '*.spc', '*.vgm', '*.vgz', '*.m3u', '*.m3u8', '*.pls', '*.cue' }, ';'), - image = table.concat({ '*.jpg', '*.bmp', '*.png', '*.gif', '*.webp' }, ';'), - iso = table.concat({ '*.iso' }, ';'), - subtitle = table.concat( - { '*.srt', '*.ass', '*.idx', '*.sub', '*.sup', '*.ttxt', '*.txt', '*.ssa', '*.smi', '*.mks' }, ';'), - playlist = table.concat({ '*.m3u', '*.m3u8', '*.pls', '*.cue' }, ';'), + video = '*.mp4;*.m4v;*.mkv;*.h264;*.h265;*.m2ts;*.ts;*.mpeg;*.wmv;*.webm;*.avi;*.flv;*.f4v;*.mov;*.rm;*.rmvb;*.3gp', + audio = '*.mp3;*.m4a;*.aac;*.flac;*.ac3;*.ogg;*.wav;*.dts;*.tta;*.amr;*.ape;*.wv;*.mka;*.weba;*.wma;*.f4a', + image = '*.jpg;*.jpeg;*.bmp;*.png;*.apng;*.gif;*.tiff;*.webp', + subtitle = '*.srt;*.ass;*.idx;*.sub;*.sup;*.txt;*.ssa;*.smi;*.mks', + playlist = '*.m3u;*.m3u8;*.pls;*.cue', + iso = '*.iso', } +local open_action = '' --- set file type filters -local function update_default_filters() - local dialog_filters = { - { name = 'All Files (*.*)', spec = '*.*' }, - { name = 'Video Files', spec = file_types['video'] }, - { name = 'Audio Files', spec = file_types['audio'] }, - { name = 'Image Files', spec = file_types['image'] }, - { name = 'ISO Image Files', spec = file_types['iso'] }, - { name = 'Subtitle Files', spec = file_types['subtitle'] }, - { name = 'Playlist Files', spec = file_types['playlist'] }, - } - mp.set_property_native('user-data/menu/dialog/filters', dialog_filters) -end - --- open Bluray iso or dir +-- open bluray iso or dir local function open_bluray(path) mp.commandv('set', 'bluray-device', path) mp.commandv('loadfile', 'bd://') end --- open DVD iso or dir +-- open dvd iso or dir local function open_dvd(path) mp.commandv('set', 'dvd-device', path) mp.commandv('loadfile', 'dvd://') end --- open callback with support for Bluray/DVD iso and subtitle file -local function open_cb(...) - local function check_file_type(ext, type) - return ext ~= '' and file_types[type]:find(ext) +-- open a single file +local function open_file(i, path, action) + if action == 'add-sub' then + mp.commandv('sub-add', path, 'auto') + elseif action == 'add-video' then + mp.commandv('video-add', path, 'auto') + elseif action == 'add-audio' then + mp.commandv('audio-add', path, 'auto') + elseif action == 'bd-iso' then + open_bluray(path) + elseif action == 'dvd-iso' then + open_dvd(path) + elseif action == 'append' then + mp.commandv('loadfile', path, 'append') + else + mp.commandv('loadfile', path, i > 1 and 'append-play' or 'replace') end +end +-- open callback +local function open_cb(...) for i, v in ipairs({ ... }) do local path = tostring(v) - local ext = path:match('^.+(%..+)$') or '' - - if check_file_type(ext, 'iso') then - local info = utils.file_info(path) - if info and info.is_file then - if info.size > 4.7 * 1000 * 1000 * 1000 then - open_bluray(path) - else - open_dvd(path) - end - break -- do not load other files - end - end - - if check_file_type(ext, 'subtitle') then - mp.commandv('sub-add', path, 'auto') - else - mp.commandv('loadfile', path, i > 1 and 'append' or 'replace') - end + open_file(i, path, open_action) end end --- open folder callback with support for Bluray/DVD Folder +-- open folder callback local function open_folder_cb(path) if utils.file_info(utils.join_path(path, 'BDMV')) then open_bluray(path) @@ -94,21 +66,44 @@ end -- clipboard callback local function clipboard_cb(clipboard) - mp.commandv('loadfile', clipboard, 'append-play') + open_file(clipboard, open_action) mp.osd_message('clipboard: ' .. clipboard) end --- message replies +-- 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('clipboard-get-reply', clipboard_cb) +-- open dialog +mp.register_script_message('open', function(action) + local function append(filters, name, type) + filters[#filters + 1] = { name = name, spec = file_types[type] } + end --- init default file types -update_default_filters() + open_action = action + local filters = {} + + if action == 'add-sub' then + append(filters, 'Subtitle Files', 'subtitle') + elseif action == 'add-video' then + append(filters, 'Video Files', 'video') + elseif action == 'add-audio' then + append(filters, 'Audio Files', 'audio') + elseif action == 'bd-iso' or action == 'dvd-iso' then + append(filters, 'ISO Files', 'iso') + else + append(filters, 'Video Files', 'video') + append(filters, 'Audio Files', 'audio') + append(filters, 'Image Files', 'image') + append(filters, 'Subtitle Files', 'subtitle') + end --- open dialog -mp.register_script_message('open', function() + if action ~= 'bd-iso' and action ~= 'dvd-iso' then + filters[#filters + 1] = { name = 'All Files', spec = '*.*' } + end + + mp.set_property_native('user-data/menu/dialog/filters', filters) mp.commandv('script-message-to', 'menu', 'dialog/open-multi', mp.get_script_name()) end) @@ -118,6 +113,7 @@ mp.register_script_message('open-folder', function() end) -- open clipboard -mp.register_script_message('open-clipboard', function() +mp.register_script_message('open-clipboard', function(action) + open_action = action mp.commandv('script-message-to', 'menu', 'clipboard/get', mp.get_script_name()) end)