Skip to content

Instantly share code, notes, and snippets.

@maz-1
Last active June 16, 2017 03:35
Show Gist options
  • Save maz-1/3c3d12439bf702c4c9315bd498db92ef to your computer and use it in GitHub Desktop.
Save maz-1/3c3d12439bf702c4c9315bd498db92ef to your computer and use it in GitHub Desktop.
Import Lyric file for Aegisub. Depends on https://github.com/davidm/lua-compress-deflatelua
local tr = aegisub.gettext
script_name = tr"Import Lyric file"
script_description = tr"Import Lyric file for Aegisub"
script_author = "domo & SuJiKiNen & maz_1"
script_version = "1.03"
local deflate = require "compress.deflatelua"
local bit = require("bit")
local Kugoo_keys = {64, 71, 97, 119, 94, 50, 116, 71, 81, 54, 49, 45, 206, 210, 110, 105}
local function round(x, dec)
-- Check argument
if type(x) ~= "number" or dec ~= nil and type(dec) ~= "number" then
error("number and optional number expected", 2)
end
-- Return number
if dec and dec >= 1 then
dec = 10^math.floor(dec)
return math.floor(x * dec + 0.5) / dec
else
return math.floor(x + 0.5)
end
end
function lyric_to_ass(subtitles)
local decoded_p = nil
local filename = aegisub.dialog.open('Select Krc File', '', '',
'Lyrics File (.krc,*lrc)|*.krc;*lrc', false, true)
if not filename then
aegisub.cancel()
end
local encoded_file = io.open(filename,"rb")
if not encoded_file then
aegisub.debug.out("Failed to load file")
aegisub.cancel()
end
if string.lower(string.sub(filename,-4,-1)) == ".krc" then
--Krc decode
local header = encoded_file:read(4)
if header ~= "krc1" then
aegisub.debug.out("Invalid krc file")
aegisub.cancel()
end
local temp = nil
local decoded = io.tmpfile()
local dbyte = encoded_file:read(1)
while dbyte do
temp = bit.bxor(Kugoo_keys[(encoded_file:seek()-5) % 16 + 1], string.byte(dbyte))
decoded:write(string.char(temp))
dbyte = encoded_file:read(1)
end
decoded:seek("set")
decoded_p = io.tmpfile()
deflate.inflate_zlib{input=decoded, output=decoded_p, disable_crc=true}
decoded:close()
--Krc decode end
elseif string.lower(string.sub(filename,-4,-1)) == ".lrc" then
str=encoded_file:read("*all")
end
encoded_file:close()
if decoded_p then
decoded_p:seek("set")
str=decoded_p:read("*all")
decoded_p:close()
k_tag="\\k"
end
l={}
for j=1, #subtitles do
if subtitles[j].class == "dialogue" then
l=subtitles[j]
break
end
end
--Krc to ASS
if string.lower(string.sub(filename,-4,-1)) == ".krc" then
for t1,t2,line_lyric in string.gmatch(str,"%[(%d+),(%d+)%]([^%[]*)") do
if (t1~=nil and t2~=nil and line_lyric~=nil) then
ls_t=t1
le_t=t1+t2
k_lrc=""
for t3,t4,syl_text in string.gmatch(line_lyric,"<(%d+),(%d+),%d+>([^<]+)") do
kdur=round(t4/10)
syl_text=string.gsub(syl_text," ",string.format("{%s%d} ",k_tag,"0"))
syl_text=string.gsub(string.gsub(syl_text,string.format("{%s0} ",k_tag).."[\r\n]+",""),"[\r\n]+","")
k_lrc=k_lrc..string.format("{%s%d}",k_tag,kdur)..syl_text
end
l.text = k_lrc
l.start_time = ls_t
l.end_time = le_t
l.layer = 0
subtitles[0]=l
end
end
--Lrc to ASS
elseif string.lower(string.sub(filename,-4,-1)) == ".lrc" then
i=1
ls_t={}
le_t={}
l_lrc={}
for st_min,st_sec,st_ms,line_lyric in string.gmatch(str,"%[(%d+):(%d+)%.(%d+)%]([^\n]+)") do
if (st_min==nil and st_sec==nil and st_ms==nil) then
ls_t[i]=0
else
ls_t[i]=st_min*60*1000+st_sec*1000+st_ms*10
l_lrc[i]=line_lyric
i=i+1
end
end
ls_t[#ls_t+1]=3599990 --for end time of last line in lrc
ls_t[#ls_t+2]=3599990
for j=1,#ls_t-3 do --for multi language lrc
le_t[j]=ls_t[j+1]
if le_t[j]==ls_t[j] then
le_t[j]=ls_t[j+2]
end
l.text = l_lrc[j]
l.start_time = ls_t[j]
l.end_time = le_t[j]
l.layer = 0
subtitles.append(l)
end
else
end
end
aegisub.register_macro(script_name, script_description, lyric_to_ass)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment