From b158fafb89977952e2fe2e2c19cb1f93e203757d Mon Sep 17 00:00:00 2001 From: dyphire Date: Sun, 10 Mar 2024 00:23:48 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mpv.conf | 5 ++- script-opts/fuzzydir.conf | 4 +- scripts/autosubsync/autosubsync.lua | 10 ++++- scripts/fuzzydir.lua | 2 +- scripts/subtitle-lines.lua | 69 ++++++++++++++++------------- 5 files changed, 52 insertions(+), 38 deletions(-) diff --git a/mpv.conf b/mpv.conf index b8dd8745..44050609 100644 --- a/mpv.conf +++ b/mpv.conf @@ -35,8 +35,9 @@ hwdec=d3d12va-copy,auto-copy # 指定应使用的硬件视频解码 API ## 硬解模式可能会遇到一些问题,推荐优先使用软解(官方手册不推荐使用硬解) ## 经测试发现--vo=gpu 下的 copy 硬解模式在 4K 及以上视频帧率>=60fps 时将会产生异常掉帧,建议使用软解模式或无 copy 硬解模式 #hwdec-codecs=all # 在选定范围内的编码格式尝试硬解,当前版本默认值为 --hwdec-codecs="h264,vc1,hevc,vp8,vp9,av1,prores" -#vd-lavc-dr=no # [当 gpu-api=d3d11 时] 启用直接渲染(默认:yes)。如果设置为 yes,视频将直接解码到 GPU 视频内存(或暂存缓冲区) +#vd-lavc-dr=no # [当 gpu-api=d3d11 时] 启用直接渲染(默认:auto)。如果设置为 yes,视频将直接解码到 GPU 视频内存(或暂存缓冲区) # 如果设置为 no,视频将解码到系统内存,然后复制到 GPU 内存。这可能会导致性能下降,但可能会解决某些低端 intel GPU 上的问题 + # 默认值 auto 会根据解码器和 GPU 的能力自动选择(即对于低端 intel GPU 设备自动禁用) #vd-queue-enable=yes # 启用视频解码器队列(默认:no)。如果设置为 yes,视频解码器将在单独的线程上运行,这可能会导致性能下降,但可能会解决某些低端 intel GPU 上的问题 #vd-lavc-threads=8 # 用于解码的线程数。是否实际支持线程取决于编解码器(默认值:0)。0 表示自动检测机器上的内核数并使用它,最多为 16。您可以手动设置超过 16 个线程 #vd-lavc-assume-old-x264=yes # 假设视频是由旧的、有缺陷的 x264 版本编码的(默认:no)。仅当 x264 yuv444p10 格式的视频解码错误时尝试启用此项解决 ffmpeg 对旧的 x264 编码的兼容问题 @@ -809,7 +810,7 @@ profile=NNEDI3 # 适用于大多数场景(NNEDI #[DoVi-P5] #profile-desc=DoVi-P5 转换 HDR 直通 -#profile-cond=p["video-out-params/max-luma"] > 203 and p["video-params/gamma"] == "bt.1886" +#profile-cond=get("video-params/colormatrix") == "dolbyvision" and get("video-params/colorlevels") == "full" #profile-restore=copy #target-trc=bt.2020 #target-gamut=dci-p3 diff --git a/script-opts/fuzzydir.conf b/script-opts/fuzzydir.conf index 740ef785..02c9c15c 100644 --- a/script-opts/fuzzydir.conf +++ b/script-opts/fuzzydir.conf @@ -1,5 +1,5 @@ -## 指定查找的最大路径深度,默认值:3 -#max_search_depth=3 +## 指定查找的最大路径深度,默认值:1 +max_search_depth=1 ## 脚本将跳过大于此选项数量的目录的路径,默认值:10 #discovery_threshold=10 ## 指定需忽略的共享盘(挂载盘)的路径。windows 示例:excluded_dir=["X:", "Z:", "F:/Download/", "Download"] diff --git a/scripts/autosubsync/autosubsync.lua b/scripts/autosubsync/autosubsync.lua index 9a6f8e7c..82fd3207 100644 --- a/scripts/autosubsync/autosubsync.lua +++ b/scripts/autosubsync/autosubsync.lua @@ -211,6 +211,8 @@ local function extract_to_file(subtitle_track) end local temp_sub_fp = utils.join_path(os_temp(), 'autosubsync_extracted.' .. ext) notify("提取内封字幕...", nil, 3) + local screenx, screeny, aspect = mp.get_osd_size() + mp.set_osd_ass(screenx, screeny, "{\\an9}● ") local ret = subprocess { config.ffmpeg_path, "-hide_banner", @@ -224,6 +226,7 @@ local function extract_to_file(subtitle_track) "-f", ext, temp_sub_fp } + mp.set_osd_ass(screenx, screeny, "") if ret == nil or ret.status ~= 0 then return notify("无法提取内封字幕.\n请先确保在脚本配置文件中为 ffmpeg 指定了正确的路径\n并确保视频有内封字幕.", "error", 7) end @@ -256,15 +259,20 @@ local function sync_subtitles(ref_sub_path) notify(string.format("开始 %s...", engine_name), nil, 2) local ret + local screenx, screeny, aspect = mp.get_osd_size() if engine_name == "ffsubsync" then local args = { config.ffsubsync_path, reference_file_path, "-i", subtitle_path, "-o", retimed_subtitle_path } if not ref_sub_path then table.insert(args, '--reference-stream') table.insert(args, '0:' .. get_active_track('audio')) end + mp.set_osd_ass(screenx, screeny, "{\\an9}● ") ret = subprocess(args) + mp.set_osd_ass(screenx, screeny, "") else + mp.set_osd_ass(screenx, screeny, "{\\an9}● ") ret = subprocess { config.alass_path, reference_file_path, subtitle_path, retimed_subtitle_path } + mp.set_osd_ass(screenx, screeny, "") end if ret == nil then @@ -283,7 +291,7 @@ local function sync_subtitles(ref_sub_path) notify("错误: 不能添加同步字幕.", "error", 3) end else - notify(string.format("字幕同步失败.\n请确保在脚本配置文件中为 %s 指定了正确的路径.", engine_name), "error", 3) + notify(string.format("字幕同步失败.\n请确保在脚本配置文件中为 %s 指定了正确的路径.\n或音轨提取失败", engine_name), "error", 3) end end diff --git a/scripts/fuzzydir.lua b/scripts/fuzzydir.lua index a8fe2dbe..2703d80e 100644 --- a/scripts/fuzzydir.lua +++ b/scripts/fuzzydir.lua @@ -51,7 +51,7 @@ local utils = require 'mp.utils' local options = require 'mp.options' o = { - max_search_depth = 3, + max_search_depth = 1, discovery_threshold = 10, excluded_dir = [[ [] diff --git a/scripts/subtitle-lines.lua b/scripts/subtitle-lines.lua index 547f7c1e..99ed51d4 100644 --- a/scripts/subtitle-lines.lua +++ b/scripts/subtitle-lines.lua @@ -1,4 +1,4 @@ --- subtitle-lines 1.1.0 - 2024-Feb-02 +-- subtitle-lines 1.1.1 - 2024-Mar-08 -- https://github.com/christoph-heinrich/mpv-subtitle-lines -- -- List and search subtitle lines of the selected subtitle track. @@ -50,7 +50,7 @@ local sub_strings_available = { } } ----@alias Subtitle {start:number;stop:number;line:string} +---@alias Subtitle {start:number;stop:number;line:string;timespan:string} local sub_strings = sub_strings_available.primary local function get_current_subtitle_lines() @@ -68,14 +68,14 @@ end ---Merge lines with already collected subtitles ---removes merged lines from the lines array ----@param subtitles Subtitle[] +---@param prev_subs_visible Subtitle[] ---@param start number ---@param stop number ---@param lines string[] ---@return string[] -local function merge_subtitle_lines(subtitles, start, stop, lines) +local function merge_subtitle_lines(prev_subs_visible, start, stop, lines) -- merge identical lines that overlap or are right after each other - for _, subtitle in ipairs(subtitles) do + for _, subtitle in ipairs(prev_subs_visible) do if subtitle.stop >= start or same_time(subtitle.stop, start) then for i = #lines, 1, -1 do if lines[i] == subtitle.line then @@ -91,13 +91,12 @@ end ---Fix end time of already collected subtitles and finds the currect start time ---for current lines ----@param subtitles Subtitle[] ----@param prev_lines string[] +---@param prev_subs_visible Subtitle[] ---@param lines string[] ---@param start number ---@param prev_start number ---@return number -local function fix_line_timing(subtitles, prev_lines, lines, start, prev_start) +local function fix_line_timing(prev_subs_visible, lines, start, prev_start) -- detect subtitles appearing after their reported sub-start time if start == prev_start then local start_approx = mp.get_property_number('time-pos', 0) - mp.get_property_number(sub_strings.delay) - SUB_SEEK_OFFSET @@ -106,21 +105,17 @@ local function fix_line_timing(subtitles, prev_lines, lines, start, prev_start) start = start_approx end end - -- detect subtitle lines disappearing before their reported sub-end time. - for j = #prev_lines, 1, -1 do - local prev_line = prev_lines[j] - for _, line in ipairs(lines) do - if prev_line == line then - table.remove(prev_lines, j) + for _, subtitle in ipairs(prev_subs_visible) do + if subtitle.stop > start then + local still_visible = false + for _, line in ipairs(lines) do + if subtitle.line == line then + still_visible = true + break + end end - end - end - for j = #prev_lines, 1, -1 do - local prev_line = prev_lines[j] - for _, subtitle in ipairs(subtitles) do - if subtitle.line == prev_line and subtitle.stop > start then + if not still_visible then subtitle.stop = start - break end end end @@ -156,12 +151,13 @@ local function acquire_subtitles() local prev_start = -1 local prev_stop = -1 local prev_text = nil - local prev_lines = {} + ---@type Subtitle[] + local prev_subs_visible = {} retry_delay = nil while true do local start, stop, text, lines = get_current_subtitle_lines() - if start and (text ~= prev_text or not same_time(start, prev_start) or not same_time(stop, prev_stop)) then + if start and stop and text and (text ~= prev_text or start ~= prev_start or stop ~= prev_stop) then -- remove empty lines for j = #lines, 1, -1 do if not lines[j]:find('[^%s]') then @@ -180,19 +176,23 @@ local function acquire_subtitles() ---mpv reports the earliest sub-start and the latest sub-end of all ---current lines, so a line that's there for a long time ---can mess up the timing of all other current lines - local start_fixed = fix_line_timing(subtitles, prev_lines, lines, start, prev_start) + local start_fixed = fix_line_timing(prev_subs_visible, lines, start, prev_start) - for j = #prev_lines, 1, -1 do - prev_lines[j] = nil - end - for j, line in ipairs(lines) do - prev_lines[j] = line + merge_subtitle_lines(prev_subs_visible, start_fixed, stop, lines) + + for j = #prev_subs_visible, 1, -1 do + if prev_subs_visible[j].stop <= start_fixed then + table.remove(prev_subs_visible, j) + end end - merge_subtitle_lines(subtitles, start_fixed, stop, lines) + local j = #prev_subs_visible for _, line in ipairs(lines) do i = i + 1 - subtitles[i] = { start = start_fixed, stop = stop, line = line } + j = j + 1 + local subtitle = { start = start_fixed, stop = stop, line = line } + subtitles[i] = subtitle + prev_subs_visible[j] = subtitle end else local delay = mp.get_property_number(sub_strings.delay) @@ -209,6 +209,11 @@ local function acquire_subtitles() mp.set_property_number(sub_strings.delay, sub_delay) mp.set_property_bool(sub_strings.visibility, sub_visibility) + + for _, subtitle in ipairs(subtitles) do + subtitle.timespan = mp.format_time(subtitle.start) .. '-' .. mp.format_time(subtitle.stop) + end + return subtitles end @@ -252,7 +257,7 @@ local function show_subtitle_list(subtitles) local is_active = has_started and not has_ended menu.items[i] = { title = subtitle.line, - hint = mp.format_time(subtitle.start) .. '-' .. mp.format_time(subtitle.stop), + hint = subtitle.timespan, active = is_active, value = { 'seek',