From 63148ee601f58ac4748015aead8eb52a54e6aba4 Mon Sep 17 00:00:00 2001 From: rydesun Date: Wed, 7 Jul 2021 19:45:40 +0800 Subject: [PATCH] feat(mpv): subtitle script --- .config/mpv/mpv.conf | 8 ++- .../mpv/script-opts/auto_add_subtitles.conf | 1 + .config/mpv/scripts/auto_add_subtitles.lua | 52 +++++++++++++++++++ 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 .config/mpv/script-opts/auto_add_subtitles.conf create mode 100644 .config/mpv/scripts/auto_add_subtitles.lua diff --git a/.config/mpv/mpv.conf b/.config/mpv/mpv.conf index 2441cfb..2b96a2c 100644 --- a/.config/mpv/mpv.conf +++ b/.config/mpv/mpv.conf @@ -13,11 +13,9 @@ osd-bar-h=2 osd-bar-w=60 # ==== 字幕 ==== -# 自动模糊匹配字幕文件 -sub-auto=fuzzy -# 额外指定目录寻找字幕文件 -sub-file-paths=Subs:字幕 -# 优先使用中文字幕 +# 使用脚本寻找外置字幕 +sub-auto=no +# 内置字幕优先使用中文 slang=sc,chs,chi,zh # 尽量让ASS字幕在画面外 sub-ass-force-margins diff --git a/.config/mpv/script-opts/auto_add_subtitles.conf b/.config/mpv/script-opts/auto_add_subtitles.conf new file mode 100644 index 0000000..edb90fc --- /dev/null +++ b/.config/mpv/script-opts/auto_add_subtitles.conf @@ -0,0 +1 @@ +another_dir=~/Userdata/Downloads/subtitles/ diff --git a/.config/mpv/scripts/auto_add_subtitles.lua b/.config/mpv/scripts/auto_add_subtitles.lua new file mode 100644 index 0000000..10740fb --- /dev/null +++ b/.config/mpv/scripts/auto_add_subtitles.lua @@ -0,0 +1,52 @@ +local mp = require 'mp' +local utils = require 'mp.utils' +local opt = require 'mp.options' + +local user_opts = { + subtitle_ext_pattern = "(srt|ass|sub)", -- 字幕文件名后缀 + dir_depth = 2, -- 搜索目录的深度 + another_dir = "", -- 额外指定的目录 + subtitle_pattern = "[0-9]+_chinese", -- 额外匹配的字幕 +} + +local script_name = mp.get_script_name() +opt.read_options(user_opts, script_name) + +local function get_parrent_dir(filepath) + return utils.split_path(utils.join_path( + mp.get_property("working-directory"), filepath)) +end + +local function add_subtitles(dir, prefix) + local cmd = {'find', dir, '-maxdepth', tostring(user_opts.dir_depth), + '-regextype', 'posix-egrep', '-regex', ".*\\."..user_opts.subtitle_ext_pattern} + if user_opts.another_dir ~= "" then + local another_dir = user_opts.another_dir:gsub('^~/', os.getenv("HOME")) + table.insert(cmd, 2, another_dir) + end + local res = utils.subprocess({args=cmd}) + + local prefix_lower = prefix:lower() + for subtitle_path in res.stdout:gmatch("[^\r\n]+") do + local _, subtitle_name = utils.split_path(subtitle_path) + if subtitle_name:lower():find(user_opts.subtitle_pattern) or + subtitle_name:lower():find(prefix_lower, 1, true) then + mp.commandv('sub-add', subtitle_path, 'select') + end + end +end + +local function add_current_subs() + local filepath = mp.get_property("path") + if filepath == nil or filepath:find("^http[s]?://") then + return + end + local _, filename = utils.split_path(filepath) + local prefix = filename:match("(.+%.[12][0-9][0-9][0-9])%..+") or + filename:match("(.+%.[0-9]+p)%..+") or + filename:match("(.+)%..+") + local dir = get_parrent_dir(filepath) + add_subtitles(dir, prefix) +end + +mp.register_event("file-loaded", add_current_subs)