Skip to content

Instantly share code, notes, and snippets.

@The0x539
Last active July 16, 2020 20:16
Show Gist options
  • Save The0x539/f829e073740307b1bac47e0de7486aa9 to your computer and use it in GitHub Desktop.
Save The0x539/f829e073740307b1bac47e0de7486aa9 to your computer and use it in GitHub Desktop.
local tr = aegisub.gettext
script_name = tr'k-retime'
script_description = tr'Set start/end timestamps while preserving syl timing'
script_author = 'The0x539'
script_version = '0.1.0'
script_namespace = '0x.k-retime'
local DependencyControl = require 'l0.DependencyControl'
local rec = DependencyControl {
feed = 'https://raw.githubusercontent.com/TypesettingTools/line0-Aegisub-Scripts/master/DependencyControl.json',
{{"a-mo.LineCollection", version = "1.0.1", url = "https://github.com/TypesettingTools/Aegisub-Motion",
feed = "https://raw.githubusercontent.com/TypesettingTools/Aegisub-Motion/DepCtrl/DependencyControl.json"},
{"l0.ASSFoundation", version = "0.2.2", url = "https://github.com/TypesettingTools/ASSFoundation",
feed = "https://raw.githubusercontent.com/TypesettingTools/ASSFoundation/master/DependencyControl.json"}
}
}
local LineCollection, assf = rec:requireModules()
local function process(line, delta_start, delta_end)
if delta_start % 10 ~= 0 or delta_end % 10 ~= 0 then
error('deltas must not have millisecond precision')
end
line.start_time = line.start_time + delta_start
line.end_time = line.end_time + delta_end
local data = assf:parse(line)
local ktags = data:getTags({'k_fill', 'k_sweep', 'k_bord'})
if #ktags > 0 then
ktags[1].value = ktags[1].value - delta_start
ktags[#ktags].value = ktags[#ktags].value + delta_end
end
data:commit()
end
local function process_all(subs, sel, changing_start)
local video_position = aegisub.ms_from_frame(aegisub.project_properties().video_position)
video_position = video_position - (video_position % 10)
local function cb(_, line, _)
local delta_start, delta_end = 0, 0
if changing_start then
delta_start = video_position - line.start_time
else
delta_end = video_position - line.end_time
end
process(line, delta_start, delta_end)
end
local lines = LineCollection(subs, sel, function() --[[this is so dumb]] return true end)
lines:runCallback(cb)
lines:replaceLines()
end
local function set_start(subs, sel, i)
process_all(subs, sel, true)
aegisub.set_undo_point('Snap start to video, preserving k-timing')
end
local function set_end(subs, sel, i)
process_all(subs, sel, false)
aegisub.set_undo_point('Snap end to video, preserving k-timing')
end
aegisub.register_macro('k-retime/Set Start', script_description, set_start)
aegisub.register_macro('k-retime/Set End', script_description, set_end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment