FichteFoll

Aegisub Lua - Fadings

Jun 2nd, 2011
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.52 KB | None | 0 0
  1. addfade = {--[[
  2.  
  3.     Adds fadings to the start or the and of selected lines,
  4.     with or without using \fad.
  5.     Also supports \k-tagged karaoke lines.
  6.  
  7.     1.1
  8.     01-06-2011
  9.     FichteFoll
  10. ]]}
  11.  
  12. -- Variables
  13. script_name = "Add Fadings"
  14. script_description = "Adds (alpha) fadings to selected lines, also karaoke."
  15. script_author = "FichteFoll"
  16. script_version = "1.1"
  17. script_modified = "1st June 2011"
  18.  
  19. addfade.script_name = script_name
  20. addfade.script_description = script_description
  21. addfade.script_author = script_author
  22. addfade.script_version = script_version
  23. addfade.script_modified = script_modified
  24.  
  25.  
  26. -- Dlg
  27. addfade.config = {
  28.         { -- 1
  29.             class = "label",
  30.             x = 0, y = 0, width = 1, height = 1,
  31.             label = "Fadein:"
  32.         },
  33.         { -- 2
  34.             class = "intedit", name = "fadein",
  35.             x = 1, y = 0, width = 1, height = 1,
  36.             value = 0, hint = "Amount of time to add before; in ms"
  37.         },
  38.         { -- 3
  39.             class = "label",
  40.             x = 0, y = 1, width = 1, height = 1,
  41.             label = "Fadeout:"
  42.         },
  43.         { -- 4
  44.             class = "intedit", name = "fadeout",
  45.             x = 1, y = 1, width = 1, height = 1,
  46.             value = 0, hint = "Amount of time to append behind; in ms"
  47.         },
  48.         { -- 5
  49.             class = "checkbox", name = "fade",
  50.             x = 0, y = 2, width = 2, height = 1,
  51.             value = true, label = "Fade transparency"
  52.         }
  53.     }
  54.  
  55. -- Add the fadings
  56. addfade.process = function(subs,sel,config)
  57.         for x, i in ipairs(sel) do
  58.             local l = subs[i]
  59.             if l.class == "dialogue" and not l.comment then
  60.                 local kara = l.text:match("{[^}]-\\k(%d+).-}")
  61.                 l.start_time = l.start_time - config.fadein
  62.                 l.end_time = l.end_time + config.fadeout
  63.  
  64.                 if config.fade then
  65.                     local prevfadein, prevfadeout = l.text:match("{[^}]-\\fad%((%d+),(%d+)%)")
  66.                     if prevfadein and prevfadeout then
  67.                         l.text = l.text:gsub("({[^}]-\\fad)%((%d+),(%d+)%)",
  68.                                 string.format("%%1(%d,%d)",tonumber(prevfadein)+config.fadein, tonumber(prevfadeout)+config.fadeout), 1)
  69.                     else
  70.                         l.text = string.format("{\\fad(%d,%d)}", config.fadein, config.fadeout) .. l.text
  71.                     end
  72.                 end
  73.  
  74.                 if kara and config.fadein >= 10 then
  75.                     l.text = l.text:gsub("({[^}]-\\k%d*.*})", "{\\k".. config.fadein/10 .."}%1", 1)
  76.                 end
  77.             end
  78.  
  79.             subs[i] = l
  80.         end
  81.     end
  82.  
  83. -- Macro callback
  84. addfade.load_macro = function(subs,sel)
  85.         local result, config
  86.         repeat
  87.             result, config = aegisub.dialog.display(addfade.config)
  88.             if not result then return end
  89.         until (config.fadein >= 0 and config.fadeout >= 0) and (config.fadein ~= 0 or config.fadeout ~= 0)
  90.  
  91.         addfade.process(subs,sel,config)
  92.         aegisub.set_undo_point("\""..addfade.script_name.."\"")
  93.     end
  94.  
  95. --#####################################################################################--
  96. -----------------------------------------------------------------------------------------
  97. --#####################################################################################--
  98.  
  99. remfade = {--[[
  100.  
  101.     Removes fadings at start or end of selected lines,
  102.     by adjusting \fad or just cutting down.
  103.     Also supports \k-tagged karaoke lines but may be
  104.     brocken with non-faded lines.
  105.  
  106.     1.1
  107.     02-06-2011
  108.     FichteFoll
  109. ]]}
  110.  
  111. -- Variables
  112. script_name = "Remove Fadings"
  113. script_description = "Removes (alpha) fadings of selected lines."
  114. script_author = "FichteFoll"
  115. script_version = "1.1"
  116. script_modified = "2nd June 2011"
  117.  
  118. remfade.script_name = script_name
  119. remfade.script_description = script_description
  120. remfade.script_author = script_author
  121. remfade.script_version = script_version
  122. remfade.script_modified = script_modified
  123.  
  124.  
  125. -- Dlg
  126. remfade.config = {
  127.         { -- 1
  128.             class = "label",
  129.             x = 0, y = 0, width = 1, height = 1,
  130.             label = "Fadein:"
  131.         },
  132.         { -- 2
  133.             class = "intedit", name = "fadein",
  134.             x = 1, y = 0, width = 1, height = 1,
  135.             value = 0, hint = "Amount of time to remove at the start; in ms"
  136.         },
  137.         { -- 3
  138.             class = "label",
  139.             x = 0, y = 1, width = 1, height = 1,
  140.             label = "Fadeout:"
  141.         },
  142.         { -- 4
  143.             class = "intedit", name = "fadeout",
  144.             x = 1, y = 1, width = 1, height = 1,
  145.             value = 0, hint = "Amount of time to remove at the end; in ms"
  146.         },
  147.         { -- 5
  148.             class = "checkbox", name = "auto",
  149.             x = 0, y = 2, width = 2, height = 1,
  150.             value = 1, label = "Auto",hint = "Just removes previous fadeings"
  151.         }
  152.     }
  153.  
  154. -- Remove the fadings
  155. remfade.process = function(subs, sel, config)
  156.         for x, i in ipairs(sel) do
  157.             local l = subs[i]
  158.             if l.class == "dialogue" and not l.comment then
  159.                 local before, prevfadein, prevfadeout, after = l.text:match("{([^}]-)\\fad%((%d+),(%d+)%)(.-)}")
  160.                 prevfadein, prevfadeout = tonumber(prevfadein), tonumber(prevfadeout)
  161.                 local newfadein, newfadeout = 0, 0
  162.  
  163.                 if config.auto then
  164.                     local kara_before, kara, kara_after = l.text:match("{([^}]-)\\k(%d+)(.-)}{[^}]-\\k%d+.-}")
  165.                     local kara = tonumber(kara)
  166.                     if prevfadein and prevfadein > 0 and prevfadeout and prevfadeout > 0 then
  167.                         newfadein, newfadeout = prevfadein, prevfadeout
  168.                         if before:len() > 0 or after:len() > 0 then
  169.                             aegisub.log("found some weird stuff that should not be there\n")
  170.                             l.text = l.text:gsub("({[^}]-)\\fad%((%d+),(%d+)%)", "%1", 1)
  171.                         else
  172.                             l.text = l.text:gsub("{\\fad%((%d+),(%d+)%)}", "", 1)
  173.                         end
  174.                     end
  175.                     if kara and kara > 0 then
  176.                         if kara*10 < prevfadein then
  177.                             newfadein = kara*10
  178.                             if kara_before:len() > 0 or kara_after:len() > 0 then
  179.                                 l.text = l.text:gsub("({[^}]-)\\k%d+(.-}{[^}]-\\k%d+.-})", "%1%2", 1)
  180.                             else
  181.                                 l.text = l.text:gsub("{\\k%d+}({[^}]-\\k%d+.-})", "%1", 1)
  182.                             end
  183.                         end
  184.                     end
  185.  
  186.                     l.start_time = l.start_time + newfadein
  187.                     l.end_time = l.end_time - newfadeout
  188.                 else
  189.                     local kara = tonumber(l.text:match("{[^}]-\\k(%d+).-}"))
  190.                     l.start_time = l.start_time + config.fadein
  191.                     l.end_time = l.end_time - config.fadeout
  192.  
  193.                     if prevfadein and prevfadein > 0 and prevfadeout and prevfadeout > 0 then
  194.                         local newfadein, newfadeout = prevfadein - config.fadein, prevfadeout - config.fadeout
  195.                         if newfadein > 0 or newfadeout > 0 then
  196.                             l.text = l.text:gsub("({[^}]-\\fad)%((%d+),(%d+)%)",
  197.                                     string.format("%%1(%d,%d)", (newfadein > 0 and newfadein or 0), (newfadeout > 0 and newfadeout or 0)), 1)
  198.                         else
  199.                             l.text = l.text:gsub("({[^}]-)\\fad%((%d+),(%d+)%)", "%1", 1)
  200.                         end
  201.                     end
  202.  
  203.                     if kara then
  204.                         local newkara = kara-config.fadein / 10
  205.                         if newkara > 0 then
  206.                             l.text = l.text:gsub("({[^}]-)\\k%d+(.-})", "%1\\k".. kara/10 .."%2", 1)
  207.                         else
  208.                             local spacer, kara2 = l.text:match("{[^}]-\\k%d+.-}(.-){[^}]-\\k(%d+).-}")
  209.                             local newkara2 = tonumber(kara2) + newkara -- supposed to be negative here
  210.                             if newkara2 > 0 and spacer:len() > 0 then
  211.                                 l.text = l.text:gsub("({[^}]-)\\k%d+(.-}.-{[^}]-)\\k%d+(.-})", "%1\\k0%2\\k".. newkara2/10 .."%3", 1)
  212.                             elseif newkara2 > 0 then
  213.                                 l.text = l.text:gsub("({[^}]-)\\k%d+(.-}.-{[^}]-)\\k%d+(.-})", "%1%2\\k".. newkara2/10 .."%3", 1)
  214.                             elseif spacer:len() == 0 then
  215.                                 l.text = l.text:gsub("({[^}]-)\\k%d+(.-}.-{[^}]-\\k%d+.-})", "%1%2", 1)
  216.                             else
  217.                                 -- no idea what to do in this case...
  218.                             end
  219.                         end
  220.                     end
  221.                 end
  222.             end
  223.  
  224.             subs[i] = l
  225.         end
  226.     end
  227.  
  228. -- Macro callback
  229. remfade.load_macro = function(subs,sel)
  230.         local result, config
  231.  
  232.         -- Check for default values in first line
  233.         for x, i in ipairs(sel) do
  234.             local l = subs[i]
  235.             if l.class == "dialogue" and not l.comment then
  236.                 local prevfadein, prevfadeout = l.text:match("{[^}]-\\fad%((%d+),(%d+)%)")
  237.                 prevfadein, prevfadeout = tonumber(prevfadein), tonumber(prevfadeout)
  238.                 local kara = tonumber(l.text:match("{[^}]-\\k(%d+).-}{[^}]-\\k%d+.-}"))
  239.                 if kara and kara > 0 then
  240.                     remfade.config[2].value = kara*10
  241.                 elseif prevfadein and prevfadein > 0 then
  242.                     remfade.config[2].value = prevfadein
  243.                 end
  244.                 if prevfadeout and prevfadeout > 0 then remfade.config[4].value = prevfadeout end
  245.  
  246.                 break
  247.             end
  248.         end
  249.  
  250.         -- Show dialog
  251.         repeat
  252.             result, config = aegisub.dialog.display(remfade.config)
  253.             if not result then return end
  254.         until (config.fadein >= 0 and config.fadeout >= 0) and (config.fadein ~= 0 or config.fadeout ~= 0)
  255.  
  256.         remfade.process(subs, sel, config)
  257.         aegisub.set_undo_point("\""..remfade.script_name.."\"")
  258.     end
  259.  
  260.  
  261. -- Register macros
  262. aegisub.register_macro(addfade.script_name, addfade.script_description, addfade.load_macro)
  263. aegisub.register_macro(remfade.script_name, remfade.script_description, remfade.load_macro)
  264.  
  265. -- Display
  266. script_name = "Fadings"
  267. script_description = "Adds and removes (alpha) fadings of selected lines, also karaoke."
Advertisement
Add Comment
Please, Sign In to add comment