Skip to content

Instantly share code, notes, and snippets.

@ipid
Last active April 21, 2023 15:16
Show Gist options
  • Save ipid/6dc35455a0b55bcea428714e826254f8 to your computer and use it in GitHub Desktop.
Save ipid/6dc35455a0b55bcea428714e826254f8 to your computer and use it in GitHub Desktop.
Aegisub Lua 自动化脚本「还原卡拉 OK 特效」,用于将字幕恢复到应用卡拉 OK 模板前的状态。
script_name = '还原卡拉 OK 特效'
script_description = '将字幕恢复到应用卡拉 OK 模板前的状态。'
script_author = 'ipid君 <https://github.com/ipid>'
script_version = '1.0'
--[[ 判断 text 是否以 suffix 为结尾 ]]
local function endsWith(text, suffix)
return text:sub(-#suffix) == suffix
end
--[[ 解析所有的 Style: 行 ]]
local function parseStyles(subs)
local styles = {}
for i = 1, #subs do
local sub = subs[i]
if sub.class == 'style' then
styles[sub.name] = i
elseif sub.class == 'dialogue' then
return styles
end
end
return styles
end
--[[ 对于特定的一行字幕,返回是否应该保留 ]]
local function shouldDelete(sub, styles)
local isFx = (sub.class == 'dialogue' and sub.effect == 'fx' and not sub.comment)
if isFx then
return true
end
local isFurigana = (sub.class == 'style' and endsWith(sub.name, '-furigana') and styles[sub.name:sub(1, -10)] ~= nil)
return isFurigana
end
--[[ 如果遇到被设为 karaoke 的普通轴,就删掉 karaoke 这个特效值,然后把注释改回正常状态 ]]
local function processFunc(sub)
if sub.class == 'dialogue' and sub.effect == 'karaoke' then
sub.effect = ''
sub.comment = false
return sub
end
return nil
end
local function revertKaraokeFx(subs)
local styles = parseStyles(subs)
local deleteIndexes = {}
aegisub.set_undo_point('还原卡拉 OK 特效')
for i = 1, #subs do
local sub = subs[i]
if shouldDelete(sub, styles) then
table.insert(deleteIndexes, i)
else
local newSub = processFunc(sub)
if newSub ~= nil then
subs[i] = newSub
end
end
end
subs.delete(deleteIndexes)
end
aegisub.register_macro(script_name, script_description, revertKaraokeFx)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment