Skip to content

Instantly share code, notes, and snippets.

@codl
Last active July 3, 2021 18:16
Show Gist options
  • Save codl/586ffad427cbc74249dc170795ee00b1 to your computer and use it in GitHub Desktop.
Save codl/586ffad427cbc74249dc170795ee00b1 to your computer and use it in GitHub Desktop.
--[[
Copyright (c) 2021 codl
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
]]--
script_name = "Decontiguouize"
script_description = "Keeps the end of cues 500ms away from scene changes and the start of other cues"
script_author = "codl"
script_version = "1.1.0"
filter_description = "Keeps the end of cues 500ms away from the start of other cues"
gap_size = 500
function add_gap(subtitles, i, do_cues, do_scenechanges)
local changed = false
local line = subtitles[i]
if line.class == "dialogue" then
if do_cues then
if i < #subtitles then
for j = i+1, #subtitles do
if subtitles[j].class == "dialogue"
and line.start_time < subtitles[j].start_time
and subtitles[j].start_time - gap_size <= line.end_time then
line.end_time = subtitles[j].start_time - gap_size
changed = true
end
end
end
end
if do_scenechanges then
keyframes = aegisub.keyframes()
if keyframes ~= nil then
for j = 1, #keyframes do
local frametime = aegisub.ms_from_frame(keyframes[j])
if frametime >= line.end_time
and frametime < line.end_time + gap_size then
-- check that the previous scene change is far enough that we
-- don't cross it
local prev = 0
if j > 1 then
prev = aegisub.ms_from_frame(keyframes[j-1])
end
if frametime - gap_size > prev then
line.end_time = frametime - gap_size
changed = true
end
end
end
end
end
if changed then
subtitles[i] = line
end
end
end
function add_gap_filter(subtitles, _)
for i = 1, #subtitles do
aegisub.progress.set(i*100/#subtitles)
add_gap(subtitles, i, true, false)
end
end
function add_gap_macro(subtitles, selected)
for i = 1, #selected do
aegisub.progress.set(i*100/#selected)
add_gap(subtitles, selected[i], true, true)
end
end
function add_gap_cues_only_macro(subtitles, selected)
for i = 1, #selected do
aegisub.progress.set(i*100/#selected)
add_gap(subtitles, selected[i], true, false)
end
end
function add_gap_sc_only_macro(subtitles, selected)
for i = 1, #selected do
aegisub.progress.set(i*100/#selected)
add_gap(subtitles, selected[i], false, true)
end
end
aegisub.register_filter(script_name, filter_description, 100, add_gap_filter)
aegisub.register_macro(script_name, script_description, add_gap_macro)
aegisub.register_macro(script_name.." (cues only)", script_description, add_gap_cues_only_macro)
aegisub.register_macro(script_name.." (scene changes only)", script_description, add_gap_sc_only_macro)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment