def create_ass_file(subtitle_blocks: List[SubtitleBlock], ass_file, styles: Dict[str, StyleConfig]): subs = SSAFile() colors = [Color(255, 255, 255), Color(100, 100, 255), Color(255, 100, 100)] for k, name in enumerate(subtitle_blocks[0].names): my_style = subs.styles["Default"].copy() my_style.primarycolor = colors[k] my_style.fontsize = styles[name].fontsize my_style.shadow = 0 subs.styles[name] = my_style for sb in subtitle_blocks: start, end = None, None for name, text in sb.name_texts: if len(text) > 0: text = text.replace("_", " ") if start is None: start = sb.start end = sb.end sub_line = SSAEvent( start=start, end=end, text=text, ) sub_line.style = name subs.append(sub_line) else: print(f"WARNING: got empty block! {name} ") subs.save(ass_file)
def test_color_argument_validation(): Color(r=0, g=0, b=0) # does not raise with assert_raises(ValueError): Color(r=0, g=0, b=256) with assert_raises(ValueError): Color(r=0, g=0, b=-1)
def create_ssa_file(): subs = SSAFile() subs.styles['top'] = SSAStyle(alignment=8) subs.styles['bottom'] = SSAStyle(alignment=2) subs.styles['left'] = SSAStyle(alignment=4) subs.styles['left_red'] = SSAStyle(alignment=4, primarycolor=Color(255, 0, 0, 0)) subs.styles['red'] = SSAStyle(primarycolor=Color(255, 0, 0, 0)) subs.styles['right'] = SSAStyle(alignment=6) return subs
def test_color_parsing(): solid_color = Color(r=1, g=2, b=3) transparent_color = Color(r=1, g=2, b=3, a=4) assert rgba_to_color(color_to_ssa_rgb(solid_color)) == solid_color assert rgba_to_color(color_to_ass_rgba(solid_color)) == solid_color assert rgba_to_color(color_to_ass_rgba(transparent_color)) == transparent_color assert rgba_to_color("&HAABBCCDD") == Color(r=0xDD, g=0xCC, b=0xBB, a=0xAA) assert color_to_ass_rgba(Color(r=0xDD, g=0xCC, b=0xBB, a=0xAA)) == "&HAABBCCDD"
def build(self, **extra): from pysubs2 import SSAFile, SSAEvent, Color # type: ignore[import] millis = lambda td: td / timedelta(milliseconds=1) subs = ( SSAEvent( start=millis(t - self.start), end =millis(t - self.start + timedelta(seconds=length)), text=text.replace('\n', r'\N'), # \N necessary for SSA files ) for t, text, length in self.l ) sf = SSAFile() style = sf.styles['Default'].copy() style.fontsize = 16 # default is 20, bit too much?? style.outlinecolor = Color(0, 0, 0, 50) # semitransparent style.shadow = 0.0 style.outline = 0.1 style.borderstyle = 3 # no idea why 3, but it makes the background apperar in conjunction with outline for k, v in extra.items(): setattr(style, k, v) sf.styles['Default'] = style for s in subs: sf.append(s) return sf.to_string('ass')
def add_styles(subs, style_list=None): """ Add styles to the subtitle file based on the style strings in each individual subtitle """ if style_list is None: style_list = [] for style in style_list: new_style = SSAStyle() # Number for position refers to the number on a keypad if 'top_left' in style: new_style.alignment = 7 elif 'top_right' in style: new_style.alignment = 9 elif 'bottom_left' in style: new_style.alignment = 1 elif 'bottom_right' in style: new_style.alignment = 3 elif 'left' in style: new_style.alignment = 4 elif 'right' in style: new_style.alignment = 6 elif 'top' in style: new_style.alignment = 8 elif 'bottom' in style: new_style.alignment = 2 # Setting the RGB values for the text if 'pred' in style: new_style.primarycolor = Color(255, 0, 0, 0) elif 'pblue' in style: new_style.primarycolor = Color(0, 0, 255, 0) elif 'pgreen' in style: new_style.primarycolor = Color(0, 255, 0, 0) elif 'pwhite' in style: new_style.primarycolor = Color(255, 255, 255, 0) # Setting the RGB values for the text's background if 'bred' in style: new_style.backcolor = Color(255, 0, 0, 0) elif 'bblue' in style: new_style.backcolor = Color(0, 0, 255, 0) elif 'bgreen' in style: new_style.backcolor = Color(0, 255, 0, 0) elif 'bwhite' in style: new_style.backcolor = Color(255, 255, 255, 0) # Setting different font types if 'bold' in style: new_style.bold = True if 'italic' in style: new_style.italic = True subs.styles[style] = new_style return subs
def merge(top_file_path: Path, bot_file_path: Path, outfile: Path): top_sub = pysubs2.load(str(top_file_path), encoding=charset_detect(top_file_path)) bot_sub = pysubs2.load(str(bot_file_path), encoding=charset_detect(bot_file_path)) '''[V4+ Styles] Format: Name,Fontname,Fontsize,PrimaryColour,SecondaryColour,OutlineColour,BackColour,Bold,Italic,Underline,StrikeOut,ScaleX,ScaleY,Spacing,Angle,BorderStyle,Outline,Shadow,Alignment,MarginL,MarginR,MarginV,Encoding Style: Default,Arial, 16, &H00FFFFFF, &H00FFFFFF, &H00000000, &H00000000,-1, 0, 0, 0 ,100 ,100 ,0 ,0 ,1 ,3 ,0 ,2 ,10 ,10 ,10 ,0 Style: Top ,Arial ,16 ,&H00F9FFFF ,&H00FFFFFF ,&H00000000 ,&H00000000,-1 ,0 ,0 ,0 ,100 ,100 ,0 ,0 ,1 ,3 ,0 ,8 ,10 ,10 ,10 ,0 Style: Mid ,Arial ,16 ,&H0000FFFF ,&H00FFFFFF ,&H00000000 ,&H00000000,-1 ,0 ,0 ,0 ,100 ,100 ,0 ,0 ,1 ,3 ,0 ,5 ,10 ,10 ,10 ,0 Style: Bot ,Arial ,16 ,&H00F9FFF9 ,&H00FFFFFF ,&H00000000 ,&H00000000,-1 ,0 ,0 ,0 ,100 ,100 ,0 ,0 ,1 ,3 ,0 ,2 ,10 ,10 ,10 ,0 ''' style_top = SSAStyle() style_bot = SSAStyle() style_top.primarycolor = Color(249, 255, 255) style_bot.primarycolor = Color(249, 255, 249) style_bot.secondarycolor = Color(255, 255, 255) style_top.secondarycolor = Color(255, 255, 255) style_top.outline = 1.0 style_bot.outline = 1.0 style_top.shadow = 0.0 style_bot.shadow = 0.0 style_top.alignment = 8 style_bot.alignment = 2 # style_top.encoding # не знаю что значит, по умолчанию 1, в примере 0, оставлю 1 bot_sub.styles['bot'] = style_bot bot_sub.styles['top'] = style_top for line in bot_sub: line.style = 'bot' for line in top_sub: line.style = 'top' bot_sub.append(line) bot_sub.styles["Default"].fontsize = 14.0 bot_sub.styles["Default"].shadow = 0.5 bot_sub.styles["Default"].outline = 1.0 bot_sub.save(str(outfile))
def test_write_read(): subs = SSAFile() e1 = SSAEvent(text="Hello, world!") e2 = SSAEvent(text="The other subtitle.\\NWith two lines.", style="custom style") s1 = SSAStyle(italic=True, primarycolor=Color(r=255, g=0, b=0, a=0)) subs.append(e1) subs.append(e2) subs.styles["custom style"] = s1 json_text = subs.to_string("json") subs2 = SSAFile.from_string(json_text, "json") assert subs2.equals(subs)
def test_hex_color_in_ssa(): # see issue #32 subs = SSAFile.from_string(HEX_COLOR_IN_SSA) style = subs.styles["Default"] assert style.primarycolor == Color(r=0xff, g=0xff, b=0xff) assert style.secondarycolor == Color(r=0xff, g=0xff, b=0x00)