def fix_useless_ass_tags(text: str, style: T.Optional[AssStyle] = None) -> str: try: ass_line = ass_tag_parser.parse_ass(text) except ass_tag_parser.ParseError as ex: return text def remove_useless_italics( ass_line: T.Iterable[ass_tag_parser.AssItem], ) -> T.Iterable[ass_tag_parser.AssItem]: last_state = style.italic if style else None for item in ass_line: if isinstance(item, ass_tag_parser.AssTagItalic): if last_state == item.enabled: continue last_state = item.enabled yield item def remove_useless_bold( ass_line: T.Iterable[ass_tag_parser.AssItem], ) -> T.Iterable[ass_tag_parser.AssItem]: last_state = (style.bold if style else None), None for item in ass_line: if isinstance(item, ass_tag_parser.AssTagBold): if last_state == (item.enabled, item.weight): continue last_state = item.enabled, item.weight yield item def remove_useless_alignment( ass_line: T.Iterable[ass_tag_parser.AssItem], ) -> T.Iterable[ass_tag_parser.AssItem]: last_state = style.alignment if style else None for item in ass_line: if isinstance(item, ass_tag_parser.AssTagAlignment): if last_state == item.alignment: continue last_state = item.alignment yield item ass_line = remove_useless_italics(ass_line) ass_line = remove_useless_bold(ass_line) ass_line = remove_useless_alignment(ass_line) return ass_tag_parser.compose_ass(ass_line)
def fix_whitespace(text: str) -> str: try: ass_line = ass_tag_parser.parse_ass(text) except ass_tag_parser.ParseError as ex: # dumb replace return re.sub(" *\n *", "\n", text.strip(), flags=re.M) for item in ass_line: if isinstance(item, ass_tag_parser.AssText): item.text = item.text.lstrip() break for item in reversed(ass_line): if isinstance(item, ass_tag_parser.AssText): item.text = item.text.rstrip() break for item in ass_line: if isinstance(item, ass_tag_parser.AssText): item.text = re.sub(" *\n *", "\n", item.text, flags=re.M) return ass_tag_parser.compose_ass(ass_line)
def _rescale_ass_tags(api: Api, x_factor: float, y_factor: float) -> None: for event in api.subs.events: try: ass_line = ass_tag_parser.parse_ass(event.text) except ass_tag_parser.ParseError: return for item in ass_line: if isinstance( item, ( ass_tag_parser.AssTagBorder, ass_tag_parser.AssTagXBorder, ass_tag_parser.AssTagYBorder, ass_tag_parser.AssTagShadow, ass_tag_parser.AssTagXShadow, ass_tag_parser.AssTagYShadow, ), ): item.size *= y_factor elif isinstance( item, ( ass_tag_parser.AssTagPosition, ass_tag_parser.AssTagRotationOrigin, ), ): item.x = int(item.x * x_factor) item.y = int(item.y * y_factor) elif isinstance(item, ass_tag_parser.AssTagMove): item.x1 = int(item.x1 * x_factor) item.y1 = int(item.y1 * y_factor) item.x2 = int(item.x2 * x_factor) item.y2 = int(item.y2 * y_factor) elif isinstance(item, ass_tag_parser.AssTagFontSize): item.size = int(item.size * y_factor) event.text = ass_tag_parser.compose_ass(ass_line)
def parse(self, ass_string, sub_format="ass"): self.format = sub_format subs = pysubs2.SSAFile.from_string(ass_string, sub_format) self.subs = subs input_hold = StringHolder() for line in subs: try: subdoc = parse_ass(line.text) except errors.ParseError: input_hold.string += translation_delimeter foundTextTag = False for tag in subdoc: found_tag = readTagText(tag, input_hold) if found_tag is not None: foundTextTag = found_tag if foundTextTag is not None: #if input_hold.string[-1] != '\n': #input_hold.string += '\n' input_hold.string += translation_delimeter line.text = compose_ass(subdoc) return input_hold.string
def compose(self, translatedString, sub_format="ass"): subs = self.subs translatedLines = translatedString.split(translation_delimeter) #print(translatedString) line_i = 0 for sub in subs: translated_string = translatedLines[line_i] if self.format == "ass": translated_string = autoLineBreaks(translated_string, '\\N') elif self.format == "srt": translated_string = autoLineBreaks(translated_string, '\n') subdoc = parse_ass(sub.text) for tag in subdoc: x = readTagText(tag, None) if x is not None: x.text = translated_string line_i += 1 break sub.text = compose_ass(subdoc) subs.info["Original Translation"] = "Líný překlad z dulik.net/aichan" return subs.to_string(format_=sub_format)
def _get_syllables(self, text: str) -> T.Iterable[_Syllable]: chunks: T.List[T.List[ass_tag_parser.AssItem]] = [[]] durations: T.List[int] = [0] for item in ass_tag_parser.parse_ass(text): if isinstance(item, ass_tag_parser.AssTagKaraoke): durations.append(item.duration) chunks.append([]) elif not isinstance( item, ( ass_tag_parser.AssTagListOpening, ass_tag_parser.AssTagListEnding, ), ): chunks[-1].append(item) while chunks and durations and not chunks[0] and not durations[0]: chunks.pop(0) durations.pop(0) for duration, chunk in zip(durations, chunks): text = ass_tag_parser.compose_ass(chunk) yield _Syllable(text, duration)