统计ass字幕中样式使用情况

2089天前 · 分享 · python · 829次阅读

需求

统计ass字幕中样式使用情况

缘由

最近要对几十个字幕文件做更改,字幕文件中有预设各种样式,可以视情况使用不同字幕样式。虽然大体上有几种样式都会用,不过偶尔有少数地方用少用的样式。昨晚做好的压制脚本,预计是能处理20+视频的,结果早上起来发现处理了9个就停了。因为第10个字幕使用了一个没有预设的样式(之前统一更换了全部字幕的预设样式,那种只用了一两次的搞掉了)...

代码

# -*- coding: utf-8 -*-

#import re
import os


def count_assstyle(ass_name = "filename.ass"):
    com_num = 0
    count_flag = False
    style_dict = dict()
    with open(ass_name,"r",encoding="utf-8") as ass:
        for line in ass.readlines():
            if "[Events]" == line.strip():
                count_flag = True
                continue
            elif count_flag == False:
                continue
            if "Dialogue" in line.strip():
                style = line.strip().split(",")[3]
                #style = re.findall(".*?,.*?,.*?,(.*?),.*",line.strip())[0]
                if style in style_dict:
                    style_dict[style]+=1
                else:
                    style_dict.update({style:1})
            elif "Comment" in line.strip():
                com_num+=1
            else:
                pass
        print(ass_name, dict(sorted(style_dict.items())))
        
_ = [count_assstyle(_) for _ in os.listdir() if ".ass" in _]

其他

字幕内容开始的地方是

[Events]

其下一行是

Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text

其中Style位置是样式名
一开始用正则

style = re.findall(".?,.?,.?,(.?),.*",line.strip())[0]

不过这样有点不合理,直接分割更好。

style = line.strip().split(",")[3]

脚本是统计当前文件夹下的全部字幕的样式,一目了然。
count_style

👍 0

python 字幕

最后修改于2089天前

评论

贴吧 狗头 原神 小黄脸
收起

贴吧

狗头

原神

小黄脸

目录

avatar

未末

迷失

126

文章数

275

评论数

7

分类