Skip to content

Commit

Permalink
add more open actions support
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Jan 22, 2024
1 parent 9da7fa6 commit 306b958
Showing 1 changed file with 73 additions and 66 deletions.
139 changes: 73 additions & 66 deletions lua/dialog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -94,21 +66,48 @@ end

-- clipboard callback
local function clipboard_cb(clipboard)
mp.commandv('loadfile', clipboard, 'append-play')
mp.osd_message('clipboard: ' .. clipboard)
local i = 1
for line in string.gmatch(clipboard, '[^\n]+') do
open_file(i, line, open_action)
i = i + 1
end
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)

Expand All @@ -118,6 +117,14 @@ 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)

--set clipboard
mp.register_script_message('set-clipboard', function(prop)
if not prop then return end
local value = utils.to_string(mp.get_property_native(prop))
mp.commandv('script-message-to', 'menu', 'clipboard/set', value, mp.get_script_name())
end)

1 comment on commit 306b958

@hooke007
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议不要把 ts 这种和别的格式高度重合的加进列表里,对写typescript的来讲是个灾难。。。

Please sign in to comment.