2
0

ytbchat2ass.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # -*- coding:utf-8 -*-
  2. from chat_downloader import ChatDownloader
  3. import sys
  4. import math
  5. import urllib.request
  6. import re
  7. # 目前仅支持开启了chat的回放,看不懂chat-downloader的源代码,甚至不想把直播安排进todo
  8. def sec2hms(sec):# 时间转换
  9. hms = str(int(sec//3600)).zfill(2)+':' + \
  10. str(int((sec % 3600)//60)).zfill(2)+':'+str(round(sec % 60, 2))
  11. return hms
  12. url = 'youtu.be/'+sys.argv[1]
  13. html = urllib.request.urlopen("https://www.youtube.com/watch?v="+sys.argv[1]).read().decode('utf-8')
  14. name = [] #预留加人用
  15. title = re.findall("<title>(.+?)</title>",html)[0].replace(' - YouTube','')
  16. name += re.findall('itemprop="name" content="(.+?)">',html)
  17. chat = ChatDownloader().get_chat(url,message_groups=['messages','superchat']) #默认普通评论和sc
  18. limitLineAmount = 12 # 屏上弹幕行数限制
  19. danmakuPassageway = [] # 塞弹幕用,记录每行上一条弹幕的消失时间
  20. for i in range(limitLineAmount):
  21. danmakuPassageway.append(0)
  22. fontName = 'Source Han Sans JP' # 字体自己换
  23. videoWidth = 1280 # 视频宽度,按720P处理了后面的内容,不建议改
  24. videoHeight = 720 # 视频高度
  25. OfficeBgHeight = 72
  26. OfficeSize = 36
  27. fontSize = 58
  28. head = '[Script Info]\n\
  29. ; Script generated by Aegisub 3.2.2\n\
  30. ; http://www.aegisub.org/\n\
  31. ScriptType: v4.00+\n\
  32. PlayResX: 1280\n\
  33. PlayResY: 720\n\
  34. \n\
  35. [V4+ Styles]\n\
  36. 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\
  37. Style: Default,微软雅黑,54,&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,0,2,0,0,0,0\n\
  38. Style: Alternate,微软雅黑,36,&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,0,2,0,0,0,0\n\
  39. 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\
  40. 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\
  41. [Events]\n\
  42. Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n'
  43. f = open(sys.argv[1]+'.ass','w',encoding='utf-8-sig')
  44. f.write(head)
  45. for message in chat:
  46. if message['time_in_seconds'] > 0:
  47. vpos = message['time_in_seconds']
  48. vpos_end = vpos+8 # 普通弹幕的时长,默认8秒
  49. else:
  50. continue
  51. if 'name' not in message['author'].keys():
  52. continue
  53. if 'money' in message.keys():
  54. text = '('+str(message['money']['amount'])+message['money']['currency']+')' # 打钱的标上数额
  55. if 'message' in message.keys():
  56. if message['message']:
  57. text += message['message'] # 打钱有留言的加上
  58. vpos_end += 2 # 打钱的多给2秒
  59. else:
  60. text=message['author']['name']+': '+message['message'] if message['author']['name'] in name else message['message'] # 没打钱的直接记录弹幕,设置了的号加上账号名字
  61. if 'emotes' in message.keys():
  62. for i in message['emotes']:
  63. if i['is_custom_emoji']:
  64. text = text.replace(i['name'],'')
  65. else:
  66. text = text.replace(i['name'],i['id'])
  67. if len(text) == 0:
  68. continue
  69. if message['author']['name'] in name: # 特定账号的弹幕放上面并加上背景
  70. f.write('Dialogue: 4,'+sec2hms(vpos)+','+sec2hms(vpos_end)+',Office,,0,0,0,,{\\an5\\p1\\pos('+str(videoWidth/2)+','+str(math.floor(OfficeBgHeight/2))+')\\bord0\\1c&H000000&\\1a&H78&}'+'m 0 0 l '+str(videoWidth)+' 0 l '+str(videoWidth) + ' '+str(OfficeBgHeight)+' l 0 '+str(OfficeBgHeight)+'\n')
  71. f.write('Dialogue: 5,'+sec2hms(vpos)+','+sec2hms(vpos_end)+',Office,,0,0,0,,{\\an5\\pos('+str(videoWidth/2)+','+str(math.floor(OfficeBgHeight/2))+')\\bord0\\fsp0}'+text+'\n')
  72. else: # 其他人的弹幕放滚动
  73. vpos_next_min = float('inf')
  74. vpos_next = vpos+1280/(len(text)*60+1280) * 8
  75. for i in range(limitLineAmount):
  76. if vpos_next >= danmakuPassageway[i]:
  77. passageway_index = i
  78. danmakuPassageway[i] = vpos+8
  79. break
  80. elif danmakuPassageway[i] < vpos_next_min:
  81. vpos_next_min = danmakuPassageway[i]
  82. Passageway_min = i
  83. if i == limitLineAmount-1 and vpos_next < vpos_next_min:
  84. passageway_index = Passageway_min
  85. danmakuPassageway[Passageway_min] = vpos+8
  86. # 计算弹幕位置
  87. sx = videoWidth
  88. sy = fontSize*(passageway_index)
  89. ex = 0
  90. for i in text:
  91. if re.search("[A-Za-z 0-9',.]",i):
  92. ex = ex-30
  93. else:
  94. ex = ex-60
  95. ey = fontSize*(passageway_index)
  96. 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')
  97. f.close()
  98. print(title+'的弹幕已经存为'+sys.argv[1]+'.ass')