Пример #1
0
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
Пример #2
0
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))