Skip to content

Instantly share code, notes, and snippets.

@line0
Created November 20, 2015 16:47
Show Gist options
  • Save line0/d03117e79b953b5e27ee to your computer and use it in GitHub Desktop.
Save line0/d03117e79b953b5e27ee to your computer and use it in GitHub Desktop.
export script_name = "Insert Timecode"
export script_description = "Inserts a running timecode"
export script_version = "0.0.1"
export script_author = "line0"
export script_namespace = "l0.InsertTimeCode"
DependencyControl = require "l0.DependencyControl"
dep = DependencyControl {
feed: "https://raw.githubusercontent.com/TypesettingTools/line0-Aegisub-Scripts/master/DependencyControl.json",
{
"aegisub.util", "aegisub.re", "aegisub.unicode",
{"a-mo.LineCollection", version: "1.1.1", url: "https://github.com/torque/Aegisub-Motion",
feed: "https://raw.githubusercontent.com/TypesettingTools/Aegisub-Motion/DepCtrl/DependencyControl.json"},
{"a-mo.Line", version: "1.4.3", url: "https://github.com/TypesettingTools/Aegisub-Motion",
feed: "https://raw.githubusercontent.com/TypesettingTools/Aegisub-Motion/DepCtrl/DependencyControl.json"}
}
}
util, re, unicode, LineCollection, Line = dep\requireModules!
log = dep\getLogger!
-- to be moved into Functional library
unicode.toCharTable = (s) ->
charNum, charStart, uniChars = 1, 1, {}
while charStart <= #s
charEnd = charStart - 1 + unicode.charwidth s\sub charStart, charStart
uniChars[charNum] = s\sub charStart, charEnd
charStart, charNum = charEnd+1, charNum+1
return uniChars
-- to be moved into Functional library
re.ggsub = (str, pattern, callback, ...) ->
regex = re.compile pattern, ...
chars = unicode.toCharTable str
charCnt, last, replacements, r = #chars, 0, {}, 1
while last < charCnt
matches = regex\match table.concat chars, "", last+1, charCnt
break unless matches
matchCnt = #matches
start = matchCnt == 1 and 1 or 2
rep = {callback unpack [matches[i].str for i = start, matchCnt]}
for i = start, matchCnt
continue if "string" != type rep[i+1-start]
replacements[r] = first: matches[i].first+last, last: matches[i].last+last, str: rep[i+1-start]
r += 1
last += matches[1].last
fragments, f, last = {}, 0, 0
for rep in *replacements
fragments[f+c] = chars[c+last] for c = 1, rep.first-last-1
f += rep.first - last
fragments[f], last = rep.str, rep.last
fragments[f+c] = chars[c+last] for c = 1, #chars-last
return table.concat fragments
splitTime = (time, div) ->
split = time % div
return split, (time - split) / div
zeroPad = (num, charCnt) ->
str = tostring num
zeroCnt = charCnt - #str
if zeroCnt > 0
str = "0"\rep(zeroCnt) .. str
return str
formatTime = (time, format) ->
splits = {}
splits.f, time = splitTime time, 1000
splits.s, time = splitTime time, 60
splits.m, time = splitTime time, 60
splits.h, time = splitTime time, 24
return re.ggsub format, "(h+|m+|s+|f+)", (flag) ->
zeroPad splits[flag\sub 1, 1], #flag
process = (lines, res) ->
for f = res.first, res.last, res.interval
timecode = math.max aegisub.ms_from_frame(f), 0
line = {
start_time: timecode,
end_time: aegisub.ms_from_frame(f + res.interval)
layer: 0
style: res.style
text: res.format\gsub "@%{(.-)%}", (fmtStr) ->
formatTime timecode, fmtStr
section: "[Events]", class: "Dialogue"
actor: "", effect: "", comment: false, margin_l: 0, margin_r: 0, margin_t: 0, extra: {}
}
lines\addLine Line line, lines
lines\insertLines!
showDialog = (sub) ->
lines = LineCollection sub
lines\generateMetaAndStyles!
styles = [k for k, _ in pairs lines.styles]
btn, res = aegisub.dialog.display {
{ class: "label", label: "Format", x: 0, y: 0, width: 1, height: 1 },
{ class: "edit", name: "format", x: 1, y: 0, width: 1, height: 1,
value: "@{hh:mm:ss.fff}" },
{ class: "label", label: "Interval", x: 0, y: 1, width: 1, height: 1 },
{ class: "intedit", name: "interval", x: 1, y: 1, width: 1, height: 1,
value: 24 },
{ class: "label", label: "Start Frame", x: 0, y: 2, width: 1, height: 1 },
{ class: "intedit", name: "first", x: 1, y: 2, width: 1, height: 1,
value: 0 },
{ class: "label", label: "End Frame", x: 0, y: 3, width: 1, height: 1 },
{ class: "intedit", name: "last", x: 1, y: 3, width: 1, height: 1,
value: aegisub.project_properties!.video_position },
{ class: "label", label: "Style", x: 0, y: 4, width: 1, height: 1 },
{ class: "dropdown", name: "style", x: 1, y: 4, width: 1, height: 1,
value: styles[1], items: styles }
}
process lines, res if btn
dep\registerMacro showDialog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment