ytbchat2ass.py 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding:utf-8 -*-
  2. from chat_downloader import ChatDownloader
  3. import sys
  4. import math
  5. # 目前仅支持开启了chat的回放,看不懂chat-downloader的源代码,甚至不想把直播安排进todo
  6. def sec2hms(sec):# 时间转换
  7. hms = str(int(sec//3600)).zfill(2)+':' + \
  8. str(int((sec % 3600)//60)).zfill(2)+':'+str(round(sec % 60, 2))
  9. return hms
  10. url = 'youtu.be/'+sys.argv[1]
  11. chat = ChatDownloader().get_chat(url,message_groups=['messages','superchat']) #默认普通评论和sc
  12. name = 'DIALOGUE Official Channel' #挑出来摆在上面的人,默认d+官号,自己改
  13. limitLineAmount = 12 # 屏上弹幕行数限制
  14. danmakuPassageway = [] # 塞弹幕用,记录每行上一条弹幕的消失时间
  15. for i in range(limitLineAmount):
  16. danmakuPassageway.append(0)
  17. fontName = 'Source Han Sans JP' # 字体自己换
  18. videoWidth = 1280 # 视频宽度,按720P处理了后面的内容,不建议改
  19. videoHeight = 720 # 视频高度
  20. OfficeBgHeight = 72
  21. OfficeSize = 36
  22. fontSize = 58
  23. head = '[Script Info]\n\
  24. ; Script generated by Aegisub 3.2.2\n\
  25. ; http://www.aegisub.org/\n\
  26. ScriptType: v4.00+\n\
  27. PlayResX: 1280\n\
  28. PlayResY: 720\n\
  29. \n\
  30. [V4+ Styles]\n\
  31. 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\
  32. Style: Default,微软雅黑,54,&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,0,2,0,0,0,0\n\
  33. Style: Alternate,微软雅黑,36,&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,0,2,0,0,0,0\n\
  34. Style: Office,'+fontName+','+str(OfficeSize)+',&H0FFFFFF,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,2,0,1,1.5,0,2,0,0,10,0\n\
  35. 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\
  36. [Events]\n\
  37. Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n'
  38. f = open(sys.argv[1]+'.ass','w',encoding='utf-8-sig')
  39. f.write(head)
  40. for message in chat:
  41. if message['time_in_seconds'] > 0:
  42. vpos = message['time_in_seconds']
  43. vpos_end = vpos+8 # 普通弹幕的时长,默认8秒
  44. else:
  45. #vpos = 0
  46. continue
  47. if 'money' in message.keys():
  48. text = '('+str(message['money']['amount'])+message['money']['currency']+')' # 打钱的标上数额
  49. if 'message' in message.keys():
  50. text += message['message'] # 打钱有留言的加上
  51. vpos_end += 2 # 打钱的多给2秒
  52. else:
  53. text=message['author']['name']+': '+message['message'] if message['author']['name'] == name else message['message'] # 没打钱的直接记录弹幕,设置了的号加上账号名字
  54. if message['author']['name'] == name: # 特定账号的弹幕放上面并加上背景
  55. 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')
  56. 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')
  57. else: # 其他人的弹幕放滚动
  58. vpos_next_min = float('inf')
  59. vpos_next = vpos+1280/(len(text)*60+1280) * 8
  60. for i in range(limitLineAmount):
  61. if vpos_next >= danmakuPassageway[i]:
  62. passageway_index = i
  63. danmakuPassageway[i] = vpos+8
  64. break
  65. elif danmakuPassageway[i] < vpos_next_min:
  66. vpos_next_min = danmakuPassageway[i]
  67. Passageway_min = i
  68. if i == limitLineAmount-1 and vpos_next < vpos_next_min:
  69. passageway_index = Passageway_min
  70. danmakuPassageway[Passageway_min] = vpos+8
  71. # 计算弹幕位置
  72. sx = videoWidth
  73. sy = fontSize*(passageway_index)
  74. ex = 0-len(text)*60
  75. ey = fontSize*(passageway_index)
  76. 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')
  77. f.close()