Skip to content

Conversation

NamelessAssassin
Copy link
Contributor

@NamelessAssassin NamelessAssassin commented Oct 1, 2025

This PR adds the ability for users to create their own script to process subtitles.

Mpvacious now can load an optional module.

The custom script returns a table that optionally includes two functions, preprocess(text) and trim(text), either of them could be nil (i.e not implemented).

The preprocess(text) is executed before trimming, and trim(text) is used to replace the default helpers.trim(text)function.

In my use case, I need to extract the Japanese subtitle from a bilingual subtitle line.

I have pasted my script and attached a sample of the bilingual subtitle file for reference.

local M = {}

local isKana = function(char)
    local byte1, byte2, byte3 = string.byte(char, 1, 3)
    -- 平假名范围: ぁ (U+3041) 到 ゖ (U+3096)
    if byte1 == 227 and byte2 == 129 and (byte3 >= 128 and byte3 <= 191) then
        return true
    end
    -- 片假名范围: ァ (U+30A1) 到 ヶ (U+30F6)
    if byte1 == 227 and byte2 == 130 and (byte3 >= 128 and byte3 <= 191) then
        return true
    end
    -- 片假名扩展范围: ㇰ (U+31F0) 到 ㇿ (U+31FF)
    if byte1 == 227 and byte2 == 131 and (byte3 >= 128 and byte3 <= 191) then
        return true
    end
    return false
end

-- 检查字符串是否包含假名
local containsKana = function(str)
    for i = 1, #str do
        local char = str:sub(i, i + 2)
        if isKana(char) then
            return true
        end
    end
    return false
end

-- 判断字符串是否包含非简体的汉字(根据实际需要可调整范围)
local containsNonSimplifiedChinese = function(str)
    -- 简单判断是否包含日文汉字的范围,例如,常用日文汉字 (这个范围可能需要根据需求进一步细化)
    return str:match("[\228\184\128-\233\191\191]")
end

local contains_non_latin_letters = function(str)
    return str:match("[^%c%p%s%w—]")
end

local capitalize_first_letter = function(string)
    return string:gsub("^%l", string.upper)
end

local remove_leading_trailing_spaces = function(str)
    return str:gsub('^%s*(.-)%s*$', '%1')
end

local remove_leading_trailing_dashes = function(str)
    return str:gsub('^[%-_]*(.-)[%-_]*$', '%1')
end


local get_japanese = function(str1, str2)
    if containsKana(str1) then
        return str1
    elseif containsKana(str2) then
        return str2
    elseif containsNonSimplifiedChinese(str1) then
        return str1
    elseif containsNonSimplifiedChinese(str2) then
        return str2
    else
        return str1
    end
end

local get_last_two_parts = function(input)
    local lines = {}
    -- 使用 string.gmatch 分割字符串并存入表
    for line in string.gmatch(input, "[^\n]+") do
        table.insert(lines, line)
    end

    -- 获取最后两段,如果不足两段,则根据情况返回
    local count = #lines
    if count >= 2 then
        return lines[count - 1], lines[count]
    elseif count == 1 then
        return lines[1], ""  -- 如果只有一段,返回该段和空字符串
    else
        return "", ""  -- 如果没有段落,返回两个空字符串
    end
end

local get_japanese_from_subtext = function(text)
    if text == nil or text == "" then
        return ""
    end

    return get_japanese(get_last_two_parts(text))
end

M.preprocess = get_japanese_from_subtext

return M

[KitaujiSub] Make Heroine ga Oosugiru! [01][WebRip][HEVC_AAC][CHS_JP&CHT_JP].zip

@tatsumoto-ren
Copy link
Member

Additional scripts should be placed outside of the source directory. The principle is that end users shouldn't mess with the source code. Also, if you install mpvacious from the AUR, you won't have permissions to modify the files (unless using sudo).

I suggest using ~/.config/mpv/subs2srs_sub_filter

local function prepare_for_exporting(sub_text)
if not h.is_empty(sub_text) then
sub_text = h.trim(sub_text)
sub_text = subs_observer.clipboard_prepare(sub_text)
Copy link
Member

Choose a reason for hiding this comment

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

Is this unintentional?

Copy link
Contributor Author

@NamelessAssassin NamelessAssassin Oct 3, 2025

Choose a reason for hiding this comment

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

I dId his because I think the text exported to the card should be the same as what is prepared for clipboard.
Could this cause any issues?

@NamelessAssassin
Copy link
Contributor Author

I suggest using ~/.config/mpv/subs2srs_sub_filter

I prefer to use ~/.config/mpv/scripts/subs2srs_sub_filter folder instead, because scripts is a natural place for scripts.
But I'm not sure if this will affect the AUR installation, though.

@tatsumoto-ren
Copy link
Member

tatsumoto-ren commented Oct 4, 2025

I prefer to use ~/.config/mpv/scripts/subs2srs_sub_filter folder instead, because scripts is a natural place for scripts.

mpv will try to run it as if it was a user script for mpv. But you can avoid it if there's no main.lua file or if main.lua doesn't execute anything by itself.

If the scripts are saved to the user's home directory, the AUR installation will work, but mpvacious will probably fail to discover the script.

@NamelessAssassin
Copy link
Contributor Author

mpv will try to run it as if it was a user script for mpv. But you can avoid it if there's no main.lua file or if main.lua doesn't execute anything by itself.

According to this part of the manual, I can use an entry like ~/.config/mpv/scripts/subs2srs_sub_filter.disable to avoid mpv from autoloading this script.
But the .disable extension doesn't look good.

subs2srs.lua Outdated
mp.add_key_binding("Ctrl+C", "mpvacious-copy-secondary-sub-to-clipboard", subs_observer.copy_current_secondary_to_clipboard)
mp.add_key_binding("Ctrl+t", "mpvacious-autocopy-toggle", subs_observer.toggle_autocopy)
mp.add_key_binding("Ctrl+g", "mpvacious-animated-snapshot-toggle", encoder.snapshot.toggle_animation)
mp.add_key_binding("Alt+m", "mpvacious-toggle-custom-sub-filter", subs_observer.toggle_custom_sub_filter)
Copy link
Member

Choose a reason for hiding this comment

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

I think we don't need a key binding to change this. The user can just edit the config file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants