def _create_bullets(self, content, animation): """Add vocabulary information into subtitle object Args: content (list): vocabulary information with time stamp animation (bool): whether using animation in ass """ style = self._subs.styles["Default"].copy() style.alignment = 7 style.fontsize = 13 style.borderstyle = 1 style.shadow = 0.5 # shadow: 0.5 px style.backcolor = pysubs2.Color( 0, 0, 0, 100) # shadow color: black with (255-100)/255 transparent style.outline = 0.5 # font outline: 0.5 px style.outlinecolor = pysubs2.Color( 0, 0, 0, 20) # outline color: black with (255-20)/255 transparent style.marginl = 70 style.marginv = 30 style.primarycolor = pysubs2.Color( 255, 255, 255, 0) # font color: white, no transparent self._subs.styles["Bullet"] = style for s in self._subs: s.text = s.text.replace("\\N", " ") for bullet in content: ws = "\\N".join([ "\\h\\h\\h\\h".join([ "{\c&H58E08F&}" + w["word"], "{\\c&HFFFFFF&}" + w["meaning"], "{\\c&H2AD6C4&}" + "[" + w["dict_pos"] + "]" ]) for w in bullet["words"] ]) start = pysubs2.time.timestamp_to_ms( pysubs2.time.TIMESTAMP.match(bullet["start"]).groups()) end = pysubs2.time.timestamp_to_ms( pysubs2.time.TIMESTAMP.match(bullet["end"]).groups()) if animation: event = pysubs2.SSAEvent(start=start, end=end, text=ws, style="Bullet", effect="Scroll up;10;110;" + str(100000 / (0.90 * (end - start)))) else: event = pysubs2.SSAEvent(start=start, end=end, text=ws, style="Bullet") self._subs.append(event)
def corrigi_estilos_subs2(subs, temp_dir_salvar, temp_nome_salvar): # subs = pysubs2.load(temp_arq_de_legenda, encoding="utf-8") subs.info = { "Title": "[Legendas-Otaku] Português (Brasil)", "PlayResX": 640, "PlayResY": 360, "ScriptType": "v4.00+", "WrapStyle": "0" } subs.aegisub_project = {} novas_fontes_estilos = CONFIG["fontesEstilos"] altera_elementos = lambda x, y: x if x else y altera_cor = lambda x, y: pysubs2.Color(*x) if True else y for nome_estilo, estilo in zip(subs.styles.keys(), subs.styles.values()): for atributo in frozenset(estilo.FIELDS): try: if any(x == atributo for x in [ "backcolor", "outlinecolor", "secondarycolor", "primarycolor" ]): vars(estilo)[atributo] = altera_cor( CONFIG_ESTILOS_LEGENDAS[nome_estilo][atributo], vars(estilo)[atributo]) else: vars(estilo)[atributo] = altera_elementos( CONFIG_ESTILOS_LEGENDAS[nome_estilo][atributo], vars(estilo)[atributo]) except: estilo.fontname = CONFIG_ESTILOS_LEGENDAS["NaoAchou"][ "fontname"] continue
def any2ass(input_file, font_size = 18, encodings = ["utf-8", "cp1252", "cp1250" ]): if not os.path.isfile(input_file): print input_file + ' does not exist' #print(input_file + ' does not exist') return #try to detect proper encoding for enc in encodings: try: with codecs.open(input_file, mode="rb", encoding=enc) as fd: tmp = fd.read() break except: #SubsceneUtilities.log('SRT2ASS: ', enc + ' failed', 2) #print enc + ' failed' continue #load input_file into pysubs2 library subs = pysubs2.load(input_file, encoding=enc, fps=23.976) #construct output_file name output_file = '.'.join(input_file.split('.')[:-1]) output_file += '.ass' #change subs style subs.styles["Default"].primarycolor = pysubs2.Color(255, 255, 255, 0) subs.styles["Default"].secondarycolor = pysubs2.Color(255, 255, 255, 0) subs.styles["Default"].outlinecolor = pysubs2.Color(0, 0, 0, 0) subs.styles["Default"].backcolor = pysubs2.Color(0, 0, 0, 0) subs.styles["Default"].fontsize = int(float(font_size)) subs.styles["Default"].bold = -1 subs.styles["Default"].borderstyle = 3 subs.styles["Default"].shadow = 0 #save subs subs.save(output_file) return output_file
def altera_cor(x, y): return pysubs2.Color(*x) if True else y
g = 0 b = 0 a = 0 else: r = int( styling.get(u'{http://www.w3.org/ns/ttml#styling}color')[1:3], 16) g = int( styling.get(u'{http://www.w3.org/ns/ttml#styling}color')[3:5], 16) b = int( styling.get(u'{http://www.w3.org/ns/ttml#styling}color')[5:7], 16) a = 0 styledict[stylename] = (pysubs2.SSAStyle( primarycolor=pysubs2.Color(r=r, g=g, b=b, a=a))) for line in captions: start = pysubs2.time.timestamp_to_ms( pysubs2.time.TIMESTAMP.match(line.get('begin')).groups()) end = pysubs2.time.timestamp_to_ms( pysubs2.time.TIMESTAMP.match(line.get('end')).groups()) if len(list(line)) == 0: print('------') text = line.text style = line.get('style') sublist.append( pysubs2.SSAEvent(start=start, end=end, text=text, style=style)) else: for sentence in line: text = sentence.text
def _create_bullets(self, content, animation): """Add phrase information into subtitle object Args: content (list): phrase information with time stamp animation (bool): whether using animation in ass """ default_style = self._subs.styles["Default"] default_style.fontsize = 20 default_style.shadow = 0.3 # shadow: 0.3 px default_style.outline = 0.3 # font outline: 0.3 px default_style.italic = -1 default_style.bold = -1 default_style.marginl = 10 default_style.marginr = 10 default_style.marginv = 30 if self._cn_subs else 10 phrase_style = self._subs.styles["Default"].copy() phrase_style.italic = 0 phrase_style.bold = 0 phrase_style.alignment = 4 phrase_style.fontsize = 24 phrase_style.borderphrase_style = 1 phrase_style.shadow = 0.3 # shadow: 0.3 px phrase_style.backcolor = pysubs2.Color( 0, 0, 0, 100) # shadow color: black with (255-100)/255 transparent phrase_style.outline = 0.3 # font outline: 0.3 px phrase_style.outlinecolor = pysubs2.Color( 0, 0, 0, 20) # outline color: black with (255-20)/255 transparent phrase_style.marginl = 24 phrase_style.marginr = 10 phrase_style.marginv = 10 phrase_style.primarycolor = pysubs2.Color( 255, 255, 255, 0) # font color: white, no transparent self._subs.styles["Phrase"] = phrase_style verb_style = self._subs.styles["Default"].copy() verb_style.italic = 0 verb_style.bold = 0 verb_style.alignment = 7 verb_style.fontsize = 24 verb_style.borderverb_style = 1 verb_style.shadow = 0.3 # shadow: 0.3 px verb_style.backcolor = pysubs2.Color( 0, 0, 0, 100) # shadow color: black with (255-100)/255 transparent verb_style.outline = 0.3 # font outline: 0.3 px verb_style.outlinecolor = pysubs2.Color( 0, 0, 0, 20) # outline color: black with (255-20)/255 transparent verb_style.marginl = 24 verb_style.marginr = 10 verb_style.marginv = 44 verb_style.primarycolor = pysubs2.Color( 255, 255, 255, 0) # font color: white, no transparent self._subs.styles["Verb"] = verb_style cn_default_style = self._subs.styles["Default"].copy() cn_default_style.fontsize = 20 cn_default_style.shadow = 0.1 # shadow: 0.1 px cn_default_style.outline = 0.1 # font outline: 0.1 px cn_default_style.italic = -1 cn_default_style.bold = -1 cn_default_style.marginl = 10 cn_default_style.marginr = 10 cn_default_style.marginv = 3 self._subs.styles["CN"] = cn_default_style marker_colors = { "plain": "{\\c&HFFFFFF&}", "verbs": "{\\c&H7C94FF&}", "noun_phrases": "{\\c&H93F8E9&}", } self._subs.events = [] for bullet in content: phrases = bullet["noun_phrases"] _phrases = "\\N".join([ "\\h\\h\\h\\h".join([ marker_colors["noun_phrases"] + w["original"], marker_colors["plain"] + w["translated"] ]) for w in phrases ]) _verbs = "\\N".join([ "\\h\\h\\h\\h".join([ marker_colors["verbs"] + w["text"], marker_colors["plain"] + "(" + w["lemma"] + ")", marker_colors["plain"] + w["meaning"] ]) for w in bullet["verbs"] ]) start = pysubs2.time.timestamp_to_ms( pysubs2.time.TIMESTAMP.match(bullet["start"]).groups()) end = pysubs2.time.timestamp_to_ms( pysubs2.time.TIMESTAMP.match(bullet["end"]).groups()) if animation: phrase_event = pysubs2.SSAEvent(start=start, end=end, text=_phrases, style="Phrase", effect="Scroll up;10;110;" + str(100000 / (0.90 * (end - start)))) verb_event = pysubs2.SSAEvent(start=start, end=end, text=_verbs, style="Verb", effect="Scroll up;10;110;" + str(100000 / (0.90 * (end - start)))) else: phrase_event = pysubs2.SSAEvent(start=start, end=end, text=_phrases, style="Phrase") verb_event = pysubs2.SSAEvent(start=start, end=end, text=_verbs, style="Verb") _markers = " ".join([ marker_colors[w[1]] + _underline(w) for w in bullet["markers"] ]) event = pysubs2.SSAEvent(start=start, end=end, text=_markers, style="Default") self._subs.append(event) self._subs.append(phrase_event) self._subs.append(verb_event) if self._cn_subs: for e in self._cn_subs: e.style = "CN" self._subs.append(e)