1
0

2 Коммиты 8cd8e12daf ... 67f1e7b867

Автор SHA1 Сообщение Дата
  kumaneru 67f1e7b867 修正 3 месяцев назад
  kumaneru ea8a306212 匹配完整链接 3 месяцев назад
1 измененных файлов с 28 добавлено и 24 удалено
  1. 28 24
      ytbchat2ass.py

+ 28 - 24
ytbchat2ass.py

@@ -13,9 +13,11 @@ def sec2hms(sec):  # 时间转换
     hms = str(int(sec//3600)).zfill(2)+':' + str(int((sec % 3600)//60)).zfill(2)+':'+str(round(sec % 60, 2))
     return hms
 
-
-def chat2ass(code, name, delay):
-    url = "https://www.youtube.com/watch?v="+code
+def chat2ass(link, name, delay):
+    pattern = re.compile(r"(?:.*?)youtube\.com/(?:v/|live/|watch\?(?:.*&)?v=)(?P<video_id>[\w-]{11})")
+    vid_match = pattern.split(link)
+    vid = [x for x in vid_match if x][0]
+    url = f"https://www.youtube.com/watch?v={vid}"
     html = urllib.request.urlopen(url).read().decode('utf-8')
     names = [name]
     title = re.findall("<title>(.+?)</title>", html)[0].replace(' - YouTube', '')
@@ -32,24 +34,26 @@ def chat2ass(code, name, delay):
     OfficeBgHeight = 72
     OfficeSize = 36
     fontSize = 58
-    head = '[Script Info]\n\
-; Script generated by Aegisub 3.2.2\n\
-; http://www.aegisub.org/\n\
-ScriptType: v4.00+\n\
-PlayResX: 1280\n\
-PlayResY: 720\n\
-\n\
-[V4+ Styles]\n\
-Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, marginL, marginR, marginV, Encoding\n\
-Style: Default,微软雅黑,54,&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,0,2,0,0,0,0\n\
-Style: Alternate,微软雅黑,36,&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,0,2,0,0,0,0\n\
-Style: Office,'+fontName+','+str(OfficeSize)+',&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,2,0,1,1.5,0,2,0,0,10,0\n\
-Style: Danmaku,'+fontName+','+str(fontSize)+',&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,2,0,1,1.5,0,2,0,0,10,0\n\n\
-[Events]\n\
-Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n\
-Comment: 0,00:00:00.0,00:00:00.0,Danmaku,标题,0,0,0,,'+title+'\n'
+    head = f'''[Script Info]
+    ; Script generated by Aegisub 3.2.2
+    ; http://www.aegisub.org/
+    ScriptType: v4.00+
+    PlayResX: {videoWidth}
+    PlayResY: {videoHeight}
+
+    [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,微软雅黑,54,&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,0,2,0,0,0,0
+    Style: Alternate,微软雅黑,36,&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,0,2,0,0,0,0
+    Style: Office,{fontName},{OfficeSize},&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,2,0,1,1.5,0,2,0,0,10,0
+    Style: Danmaku,{fontName},{fontSize},&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,2,0,1,1.5,0,2,0,0,10,0
+
+    [Events]
+    Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
+    Comment: 0,00:00:00.0,00:00:00.0,Danmaku,标题,0,0,0,,{title}
+    '''
 
-    f = open(code+'.ass', 'w', encoding='utf-8-sig')
+    f = open(vid+'.ass', 'w', encoding='utf-8-sig')
     f.write(head)
     for message in chat:
         vpos = message['time_in_seconds'] - float(delay)
@@ -110,7 +114,7 @@ Comment: 0,00:00:00.0,00:00:00.0,Danmaku,标题,0,0,0,,'+title+'\n'
             f.write('Dialogue: 0,'+sec2hms(vpos)+',' + sec2hms(vpos_end) + ',Danmaku,'+message['author']['name'].replace(',', '')+',0,0,0,,{\\an7\\move('+str(sx)+','+str(sy)+','+str(ex)+','+str(ey)+')}'+text+'\n')
             count += 1
     f.close()
-    print(title+'的弹幕已经存为'+code+'.ass,共'+str(count)+'条')
+    print(f'{title}的弹幕已经存为{vid}.ass,共{count}条')
 
 
 def main():
@@ -119,14 +123,14 @@ def main():
     parser = argparse.ArgumentParser()
     parser.add_argument('-n', '--name', metavar='str', help='除主播外,需将弹幕显示在上方的账号')
     parser.add_argument('-d', '--delay', metavar='str', help='弹幕延迟,一般适用于首播')
-    parser.add_argument('code', metavar='str', help='该视频的编号,如youtu.be/tWXPP1tvuIU或www.youtube.com/watch?v=tWXPP1tvuIU中的tWXPP1tvuIU')
+    parser.add_argument('link', metavar='str', help='视频链接或视频id')
     args = parser.parse_args()
-    if args.code:
+    if args.link:
         if not args.name:
             args.name = ''
         if not args.delay:
             args.delay = 0
-        chat2ass(args.code, args.name, args.delay)
+        chat2ass(args.link, args.name, args.delay)
 
 
 if __name__ == '__main__':