-- This turns your \fad tag into \t\alpha, letting you add accel for the fade in and fade out. -- Supports present alpha tags. Supports multiple alpha tags in line (at least it should). -- If there already is an alpha transform, expect things to break. -- You can also fade to/from any colour. Should support inline \c and \3c tags. -- Does NOT support \2c, \4c, \1a, \2a, \3a, \4a. script_name="Turn fade into transform" script_description="Turn fade into alpha transform" script_author="unanimated" script_version="1.7" include("karaskel.lua") function fadalpha(subs, sel) for z, i in ipairs(sel) do local line=subs[i] local text=subs[i].text styleref=stylechk(subs,line.style) dur=line.end_time-line.start_time col1=res.c1:gsub("#(%x%x)(%x%x)(%x%x)","&H%3%2%1&") col2=res.c2:gsub("#(%x%x)(%x%x)(%x%x)","&H%3%2%1&") if text:match("\\fad%(") then fadin,fadout=text:match("\\fad%((%d+)%,(%d+)") primary=styleref.color1:gsub("H%x%x","H") pri=text:match("^{\\[^}]-\\c(&H%x+&)") if pri~=nil then primary=pri end outline=styleref.color3:gsub("H%x%x","H") out=text:match("^{\\[^}]-\\3c(&H%x+&)") if out~=nil then outline=out end border=styleref.outline bord=text:match("^{[^}]-\\bord([%d%.]+)") if bord~=nil then border=tonumber(bord) end text=text:gsub("\\1c","\\c") kolora1="\\c"..col1 kolora3="\\3c"..col1 kolora="\\c"..col1.."\\3c"..col1 kolorb1="\\c"..col2 kolorb3="\\3c"..col2 kolorb="\\c"..col2.."\\3c"..col2 a00="\\alpha&H00&" aff="\\alpha&HFF&" -- with alpha in line if text:match("\\alpha&H%x%x&") then if fadin~="0" then -- fade from colour if res.crl then text=text:gsub("^({\\[^}]-)\\c&H%x+&","%1") text=text:gsub("^({\\[^}]-)\\3c&H%x+&","%1") text=text:gsub("^({\\[^}]-)}", "%1"..kolora.."\\t(0,"..fadin..","..res.inn..",\\c"..primary.."\\3c"..outline..")}") -- inline colour tags for t in text:gmatch("({\\[^}]-})") do if t~=text:match("^{\\[^}]-}") and t:match("\\[13]?c") then col1="" col3="" if t:match("\\c&") then col1=t:match("(\\c&H%x+&)") end if t:match("\\3c") then col3=t:match("(\\3c&H%x+&)") end t2=t:gsub("\\c&H%x+&",kolora1) t2=t2:gsub("\\3c&H%x+&",kolora3) t2=t2:gsub("({[^}]-)}","%1\\t(0,"..fadin..","..res.inn..","..col1..col3..")}") t=esc(t) text=text:gsub(t,t2) end end -- fade from alpha else if text:match("^{\\[^}]-\\alpha&H%x%x&") then text=text:gsub("^{(\\[^}]-)(\\alpha&H%x%x&)([^}]-)}","{%1%3\\alpha&HFF&\\t(0,"..fadin..","..res.inn..",%2)}") else text=text:gsub("^{(\\[^}]-)}","{%1"..aff.."\\t(0,"..fadin..","..res.inn..","..a00..")}") end -- inline alpha tags for t in text:gmatch("({\\[^}]-})") do if t~=text:match("^{\\[^}]-}") and t:match("\\alpha") then arfa=t:match("(\\alpha&H%x+&)") t2=t:gsub("\\alpha&H%x+&",aff) t2=t2:gsub("({[^}]-)}","%1\\t(0,"..fadin..","..res.inn..","..arfa..")}") t=esc(t) text=text:gsub(t,t2) end end end end if fadout~="0" then -- fade to colour if res.clr then text=text:gsub("^({\\[^}]-)}","%1\\t("..dur-fadout..",0,"..res.ut..","..kolorb..")}") -- inline colour tags for t in text:gmatch("({\\[^}]-})") do if t~=text:match("^{\\[^}]-}") and t:match("\\[13]?c") then t2=t:gsub("({\\[^}]-)}","%1\\t("..dur-fadout..",0,"..res.ut..","..kolorb..")}") if not t:match("\\c&") then t2=t2:gsub("\\c&H%x+&","") end if not t:match("\\3c") then t2=t2:gsub("\\3c&H%x+&","") end t=esc(t) text=text:gsub(t,t2) end end -- fade to alpha else text=text:gsub("^({\\[^}]-)}","%1\\t("..dur-fadout..",0,"..res.ut..","..aff..")}") -- inline alpha tags for t in text:gmatch("({\\[^}]-})") do if t~=text:match("^{\\[^}]-}") and t:match("\\alpha") then t2=t:gsub("({\\[^}]-)}","%1\\t("..dur-fadout..",0,"..res.ut..","..aff..")}") t=esc(t) text=text:gsub(t,t2) end end end end -- without alpha else if fadin~="0" then -- fade from colour if res.crl then text=text:gsub("^({\\[^}]-)\\c&H%x+&","%1") text=text:gsub("^({\\[^}]-)\\3c&H%x+&","%1") text=text:gsub("^({\\[^}]-)}", "%1"..kolora.."\\t(0,"..fadin..","..res.inn..",\\c"..primary.."\\3c"..outline..")}") -- inline colour tags for t in text:gmatch("({\\[^}]-})") do if t~=text:match("^{\\[^}]-}") and t:match("\\[13]?c") then col1="" col3="" if t:match("\\c&") then col1=t:match("(\\c&H%x+&)") end if t:match("\\3c") then col3=t:match("(\\3c&H%x+&)") end t2=t:gsub("\\c&H%x+&",kolora1) t2=t2:gsub("\\3c&H%x+&",kolora3) t2=t2:gsub("({[^}]-)}","%1\\t(0,"..fadin..","..res.inn..","..col1..col3..")}") t=esc(t) text=text:gsub(t,t2) end end -- fade from alpha else text=text:gsub("^({\\[^}]-)}","%1"..aff.."\\t(0,"..fadin..","..res.inn..","..a00..")}") end end if fadout~="0" then -- fade to colour if res.clr then text=text:gsub("^({\\[^}]-)}","%1\\t("..dur-fadout..",0,"..res.ut..","..kolorb..")}") -- inline colour tags for t in text:gmatch("({\\[^}]-})") do if t~=text:match("^{\\[^}]-}") and t:match("\\[13]?c") then t2=t:gsub("({\\[^}]-)}","%1\\t("..dur-fadout..",0,"..res.ut..","..kolorb..")}") if not t:match("\\c&") then t2=t2:gsub("\\c&H%x+&","") end if not t:match("\\3c") then t2=t2:gsub("\\3c&H%x+&","") end t=esc(t) text=text:gsub(t,t2) end end -- fade to alpha else text=text:gsub("^({\\[^}]-)}","%1\\t(" .. dur-fadout ..",0," .. res.ut .. ","..aff..")}") end end end if border==0 then text=text:gsub("\\3c&H%x+&","") end if not text:match("\\fad%(0,0%)") then text=text:gsub("\\fad%(%d+,%d+%)","") end -- nuke the fade end line.text=text subs[i]=line end end function esc(str) str=str :gsub("%%","%%%%") :gsub("%(","%%%(") :gsub("%)","%%%)") :gsub("%[","%%%[") :gsub("%]","%%%]") :gsub("%.","%%%.") :gsub("%*","%%%*") :gsub("%-","%%%-") :gsub("%+","%%%+") :gsub("%?","%%%?") return str end function stylechk(subs,stylename) for i=1, #subs do if subs[i].class=="style" then local st=subs[i] if stylename==st.name then styleref=st end end end return styleref end function konfig(subs, sel) dialog_config= { {x=0,y=0,width=3,height=1,class="label",label="This will turn \\fad into \\t\\alpha",}, {x=0,y=1,width=1,height=1,class="label",label="accel in:",}, {x=0,y=2,width=1,height=1,class="label",label="accel out:",}, {x=1,y=1,width=2,height=1,class="floatedit",name="inn",value=1}, {x=1,y=2,width=2,height=1,class="floatedit",name="ut",value=1}, {x=0,y=3,width=3,height=1,class="label",label=" <1 starts fast, ends slow \n >1 starts slow, ends fast",}, {x=0,y=4,width=2,height=1,class="checkbox",name="crl",label="fade from: ",value=false}, {x=0,y=5,width=2,height=1,class="checkbox",name="clr",label="fade to:",value=false}, {x=2,y=4,width=1,height=1,class="color",name="c1"}, {x=2,y=5,width=1,height=1,class="color",name="c2"}, } pressed, res=aegisub.dialog.display(dialog_config,{"Transform","Cancel"},{ok='Transform',close='Cancel'}) if pressed=="Cancel" then aegisub.cancel() end if pressed=="Transform" then fadalpha(subs, sel) end end function fade2alpha(subs, sel) konfig(subs, sel) aegisub.set_undo_point(script_name) return sel end aegisub.register_macro(script_name, script_description, fade2alpha)