Skip to content

Instantly share code, notes, and snippets.

@tp7
Last active August 29, 2015 14:03
Show Gist options
  • Save tp7/abbaa0b196eb7123dc10 to your computer and use it in GitHub Desktop.
Save tp7/abbaa0b196eb7123dc10 to your computer and use it in GitHub Desktop.
'''
A simple script that can remove useless \iclip's and invisible lines from your ass subtitles.
Usage: clean-iclips.py script.ass
It will not overwrite the input file. Probably.
You have to have avisynth, avsmeter, masktools and vsfilter plugin installed.
Also be sure the fonts are loaded (e.g. using some font manager), otherwise it might remove wrong clips/lines.
Poorly written by tp7, must be used for no evil.
'''
from _subprocess import CREATE_NEW_CONSOLE
import codecs
import copy
import os
import re
from subprocess import Popen
import sys
class AssEvent(object):
def __init__(self, text):
split = text.split(':', 1)
self.kind = split[0]
split = [x.strip() for x in split[1].split(',', 9)]
self.layer = split[0]
self.start = self.parse_ass_time(split[1])
self.end = self.parse_ass_time(split[2])
self.style = split[3]
self.name = split[4]
self.margin_left = split[5]
self.margin_right = split[6]
self.margin_vertical = split[7]
self.effect = split[8]
self.text = split[9]
@staticmethod
def format_time(cs):
return u'{0}:{1:02d}:{2:02d}.{3:02d}'.format(
int(cs // 360000),
int((cs // 6000) % 60),
int((cs // 100) % 60),
int(cs % 100))
@staticmethod
def parse_ass_time(string):
hours, minutes, seconds = string.split(':')
seconds, ms = map(int, seconds.split('.'))
return (int(hours)*3600+int(minutes)*60+seconds) * 100 + ms
def __unicode__(self):
return u'{0}: {1},{2},{3},{4},{5},{6},{7},{8},{9},{10}'.format(self.kind, self.layer,
self.format_time(self.start),
self.format_time(self.end),
self.style, self.name,
self.margin_left, self.margin_right,
self.margin_vertical, self.effect,
self.text)
class AssScript(object):
def __init__(self):
super(AssScript, self).__init__()
self.script_info = []
self.styles = []
self.events = []
@staticmethod
def from_file(path):
script = AssScript()
parse_script_info_line = lambda x: script.script_info.append(x)
parse_styles_line = lambda x: script.styles.append(x)
parse_event_line = lambda x: script.events.append(AssEvent(x))
parse_function = None
try:
with codecs.open(path, encoding='utf-8-sig') as file:
for line in file:
line = line.strip()
if not line:
continue
low = line.lower()
if low == u'[script info]':
parse_function = parse_script_info_line
elif low == u'[v4+ styles]':
parse_function = parse_styles_line
elif low == u'[events]':
parse_function = parse_event_line
elif low.startswith(u'format:'):
continue # ignore it
elif not parse_function:
raise Exception("That's some invalid ASS script")
else:
parse_function(line)
return script
except IOError:
raise Exception("Script {0} not found".format(path))
def save_to_file(self, path):
lines = []
if self.script_info:
lines.append(u'[Script Info]')
for line in self.script_info:
lines.append(line)
lines.append('')
if self.styles:
lines.append(u'[V4+ Styles]')
lines.append(u'Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding')
for line in self.styles:
lines.append(line)
lines.append('')
if self.events:
lines.append(u'[Events]')
lines.append(u'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text')
for line in self.events:
lines.append(unicode(line))
with codecs.open(path, encoding='utf-8', mode= 'w') as file:
file.write(unicode(os.linesep).join(lines))
def write_all_text(path, text):
with open(path, 'w') as file:
file.write(text)
def read_numbers(path):
with open(path) as file:
return [int(x) for x in file.readlines() if x]
def run (input_path):
iclip_pattern = re.compile(r'\\iclip\(.+?\)')
input_path = os.path.abspath(input_path)
source_script_path = input_path + '.src.ass'
useless_clips_script = input_path + '.noclips.ass'
useless_clips_log = input_path + '.useless.clips.txt'
invisible_lines_log = input_path + '.useless.lines.txt'
useless_clips_avs = input_path + '.iclips.avs'
invisible_lines_avs = input_path + '.lines.avs'
script = AssScript.from_file(input_path)
clone = copy.deepcopy(script)
# assign some index to all lines so we can find the line in the source
for idx, e in enumerate(clone.events):
e.index = idx
clone.events = [e for e in clone.events if iclip_pattern.search(e.text)]
# make all lines last a single millisecond
for idx, e in enumerate(clone.events):
e.start = idx
e.end = idx+1
clone.save_to_file(source_script_path)
for e in clone.events:
e.text = iclip_pattern.sub('', e.text)
clone.save_to_file(useless_clips_script)
# compare frames with and without iclip, if there's no difference - we can remove the iclip
# likewise, compare frames with and without subs and remove all lines that are not visible
# the blackness clip has 100 fps so each frame properly maps to one event
avs_base = 'SetMemoryMax(64)\n' \
'Blackness({0}, 1920, 1080, "YV12", fps=100)\n' \
'original = TextSub("{1}")\n' \
'noclip = TextSub("{2}")\n'.format(len(clone.events), source_script_path, useless_clips_script)
avs_clips = avs_base + 'mt_logic(original, noclip, "xor").mt_binarize(0)\n' \
'writefileif(last, "{0}", "averageluma == 0", "current_frame", append=false)\n' \
.format(useless_clips_log)
avs_lines = avs_base + 'mt_logic(original, last, "xor").mt_binarize(0)\n' \
'writefileif(last, "{0}", "averageluma == 0", "current_frame", append=false)\n' \
.format(invisible_lines_log)
write_all_text(useless_clips_avs, avs_clips)
write_all_text(invisible_lines_avs, avs_lines)
p1 = Popen('avsmeter "{0}"'.format(useless_clips_avs), creationflags=CREATE_NEW_CONSOLE)
p2 = Popen('avsmeter "{0}"'.format(invisible_lines_avs), creationflags=CREATE_NEW_CONSOLE)
p1.wait()
p2.wait()
useless = read_numbers(useless_clips_log)
print('Removing {0} iclips'.format(len(useless)))
for x in useless:
event = script.events[clone.events[x].index]
event.text = iclip_pattern.sub('', event.text)
invisible = read_numbers(invisible_lines_log)
print('Removing {0} invisible lines'.format(len(invisible)))
for x in reversed(invisible):
del script.events[clone.events[x].index]
script.save_to_file(input_path + '.out.ass')
os.remove(source_script_path)
os.remove(useless_clips_script)
os.remove(useless_clips_avs)
os.remove(invisible_lines_avs)
os.remove(useless_clips_log)
os.remove(invisible_lines_log)
if __name__ == '__main__':
try:
run(sys.argv[1])
except IndexError:
print('Gotta specify input file')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment