20230812在WIN10下使用python3将SRT格式的字幕转换为SSA格式
 2023/8/12 20:58
 本文的SSA格式以【Batch Subtitles Converter(批量字幕转换) v1.23】的格式为准!
1、
 缘起:网上找到的各种各样的字幕转换软件/小工具都不是让自己完全满意!
 【都有各种各样的小瑕疵】
 因此决定用python3自己写一个练手!
 本文的python脚本只处理UTF8编码的SRT格式字幕,ANSI编码的SRT格式请自行参照本文修改了!
 !!!!Batch Subtitles Converter(批量字幕转换) v1.23 绿色英文版:不能修改/自定义所需要的SSA字幕的位置和颜色/字体/字号。
 subresync.exe:不支持UTF8编码的字幕!且不能修改/自定义所需要的SSA字幕的位置和颜色/字体/字号。
 vobsub.dll
技术点:
 Red.Eye.2005.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.5.1-FGT.cn9.srt
 将《迫在眉睫》的英文字母通过google翻译之后的中文SRT格式的字幕转换为SSA格式!
path_to_save_txt+utf8_file.cn.srt
1
 00:02:12,766 --> 00:02:16,099
 泰勒·鲍勃和玛丽安·泰勒和我在一起
2
 00:02:16,100 --> 00:02:16,900
 一秒
 path_to_save_txt+utf8_file.cn.ssa
[Script Info]
 ; This is a Sub Station Alpha v4 script.
 Title: path_to_save_txt+utf8_file.cn
 ScriptType: v4.00
 Collisions: Normal
 PlayDepth: 0
[V4 Styles]
 Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding
 Style: Default,Tahoma,25,&H80ff00,&H00ffff,&H000008,&H000008,-1,0,1,2,0,6,80,80,80,0,134
[Events]
 Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
 Dialogue: Marked=0,0:02:12.76,0:02:16.09,Default,NTP,0000,0000,0000,!Effect,泰勒·鲍勃和玛丽安·泰勒和我在一起
 Dialogue: Marked=0,0:02:16.10,0:02:16.90,Default,NTP,0000,0000,0000,!Effect,一秒
 重点:将SRT格式的时间轴修改为SSA格式的,需要使用python3的字符串处理/分割功能!
 PYTHON SRT转SSA
 python 时间轴 字符串拆分
 https://www.techiedelight.com/zh/split-string-with-delimiters-python/
 在 Python 中使用分隔符拆分字符串
在 Python 中使用分隔符拆分字符串
 Google Translate Icon
 这篇文章将讨论如何在 Python 中使用分隔符分割字符串。
1.使用 re.split() 功能
 通过模式的出现来拆分字符串的简单解决方案是使用内置函数 re.split().模式可以由一个或多个分隔符组成:
在单个分隔符上拆分
要使用单个分隔符拆分字符串,您只需将该分隔符传递给 re.split() 功能。
import re
 if __name__ == '__main__':
     s = 'Hello,World'
     res = re.split(',', s)                # split with comma
     print(res)                            # ['Hello', 'World']
 Microsoft Windows [版本 10.0.19045.2311]
 (c) Microsoft Corporation。保留所有权利。
C:\Users\Administrator>python
 Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] on win32
 Type "help", "copyright", "credits" or "license" for more information.
 >>>
 >>>
 KeyboardInterrupt
 >>> temp = '00:00:16,533 --> 00:00:18,366'
 >>> import re
 >>> temp
 '00:00:16,533 --> 00:00:18,366'
 >>> res = re.split(':', temp)
 >>> print(res)
 ['00', '00', '16,533 --> 00', '00', '18,366']
 >>> res[0]
 '00'
 >>> res[1]
 '00'
 >>> res[2]
 '16,533 --> 00'
 >>>
 >>> re.split('-->', res[2])
 ['16,533 ', ' 00']
 >>> res[3]
 '00'
 >>> res[4]
 '18,366'
 >>>

 srt2ssa.py
# coding=utf-8
import re
 #f2 = open(new_file, "w", encoding="UTF-8")
 f2 = open('test.ssa', "w", encoding="UTF-8")
f2.write('[Script Info]\n')
 f2.write('; This is a Sub Station Alpha v4 script.\n')
 f2.write('Title: 8月7日.cn\n')
 f2.write('ScriptType: v4.00\n')
 f2.write('Collisions: Normal\n')
 f2.write('PlayDepth: 0\n')
 f2.write('\n')
 f2.write('[V4 Styles]\n')
 f2.write('Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\n')
 f2.write('Style: Default,Tahoma,25,&H80ff00,&H00ffff,&H000008,&H000008,-1,0,1,2,0,6,80,80,80,0,134\n')
 f2.write('\n')
 f2.write('[Events]\n')
 f2.write('Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n')
 #f2.write('Dialogue: Marked=0,0:02:12.76,0:02:16.09,Default,NTP,0000,0000,0000,!Effect,泰勒·鲍勃和玛丽安·泰勒和我在一起\n')
 #with open(file, "r", encoding="UTF-8") as f:
 #f = open(file, "r", encoding="UTF-8")
 f = open('path_to_save_txt+utf8_file.cn.srt', "r", encoding="UTF-8")
 lines = f.readlines()
temp = 1
 xuhao = 1;
for line in lines:
     if temp == 1:
         #f2.write(str(xuhao))
         #f2.write(str('\n'))
         #temp=0
         temp=2
     else:
         #time8 = ''
         
         # 2023/8/11 9:09 发现SRT的空格
         if len(line) == 1:
             temp=1
             xuhao = xuhao+1
         #f2.write(line)
         # 2023/8/11 9:34 中英文字幕干掉空格行之后的时间轴
         elif temp == 2:
             temp = 3
             #res = re.split(':', temp)
             time1 = re.split(':', line)
             #re.split('-->', res[2])
             #time2 = re.split('-->', time1[2])
             time2 = re.split(' --> ', time1[2])
             #Dialogue: Marked=0,0:02:12.76,0:02:16.09,Default,NTP,0000,0000,0000,!Effect,泰勒·鲍勃和玛丽安·泰勒和我在一起
             #time8 = 'Dialogue: Marked=0,' + time1[0] + time1[1] + time2[0] + time2[1] + time1[3] + time1[4] + ',Default,NTP,0000,0000,0000,!Effect,'
             
             #time8 = 'Dialogue: Marked=0,' + time1[0] + ':' + time1[1] + ':' + time2[0] + ':' + time2[1] + ':' + time1[3] + ':' + time1[4] + ',Default,NTP,0000,0000,0000,!Effect,'
             #print(line.rstrip())
             #time8 = 'Dialogue: Marked=0,' + time1[0] + ':' + time1[1] + ':' + time2[0] + ':' + time2[1] + ':' + time1[3] + ':' + time1[4].rstrip() + ',Default,NTP,0000,0000,0000,!Effect,'
             #time8 = 'Dialogue: Marked=0,' + time1[0] + ':' + time1[1] + ':' + time2[0] + ',' + time2[1] + ':' + time1[3] + ':' + time1[4].rstrip() + ',Default,NTP,0000,0000,0000,!Effect,'
             
             #Dialogue: Marked=0,00:02:12,76, 00:02:16,099,Default,NTP,0000,0000,0000,!Effect,泰勒·鲍勃和玛丽安·泰勒和我在一起
             #time8 = 'Dialogue: Marked=0,' + time1[0] + ':' + time1[1] + ':' + time2[0][0:5] + ',' + time2[1] + ':' + time1[3] + ':' + time1[4].rstrip() + ',Default,NTP,0000,0000,0000,!Effect,'

             
             #Dialogue: Marked=0,00:02:12,766,00:02:16,099,Default,NTP,0000,0000,0000,!Effect,泰勒·鲍勃和玛丽安·泰勒和我在一起
             #time8 = 'Dialogue: Marked=0,' + time1[0] + ':' + time1[1] + ':' + time2[0] + ',' + time2[1] + ':' + time1[3] + ':' + time1[4].rstrip() + ',Default,NTP,0000,0000,0000,!Effect,'

             
             #Dialogue: Marked=0,0:02:12.76,0:02:16.09,Default,NTP,0000,0000,0000,!Effect,泰勒·鲍勃和玛丽安·泰勒和我在一起
             #Dialogue: Marked=0,00:02:12,76,00:02:16,09,Default,NTP,0000,0000,0000,!Effect,泰勒·鲍勃和玛丽安·泰勒和我在一起
             #time8 = 'Dialogue: Marked=0,' + time1[0] + ':' + time1[1] + ':' + time2[0][0:5] + ',' + time2[1] + ':' + time1[3] + ':' + time1[4][0:5] + ',Default,NTP,0000,0000,0000,!Effect,'
             
             #time2[0][2] = '.' 
             #time1[4][2] = '.'
             #time2 = re.split(' --> ', time1[2])
             time5 = re.split(',', time2[0][0:5])
             time6 = re.split(',', time1[4][0:5])
             #time8 = 'Dialogue: Marked=0,' + time1[0] + ':' + time1[1] + ':' + time2[0][0:5] + ',' + time2[1] + ':' + time1[3] + ':' + time1[4][0:5] + ',Default,NTP,0000,0000,0000,!Effect,'
             time8 = 'Dialogue: Marked=0,' + time1[0] + ':' + time1[1] + ':' + time5[0] + '.' + time5[1] + ',' + time2[1] + ':' + time1[3] + ':' + time6[0] + '.' + time6[1] + ',Default,NTP,0000,0000,0000,!Effect,'
         #else if temp == 2:
         elif temp == 3:
             #f3.write(line_en)
             #f4.write(line)
             #f2.write(line)
             f2.write(time8 + line)
             #f2.write('\n\n')

 
 
 参考资料:
 https://mathpretty.com/10393.html
 Python字幕转换工具–asstosrt
 https://fiime.cn/blog/281970
 如何实现ass/ssa批量转换srt的脚本
 https://www.techiedelight.com/zh/split-string-with-delimiters-python/
 在 Python 中使用分隔符拆分字符串
 https://zhuanlan.zhihu.com/p/460511660
 python如何切割字符串
 TypeError: 'str' object does not support item assignment
 https://pythonjishu.com/python-error-66/
 Python报”TypeError: ‘str’ object does not support item assignment “的原因以及解决办法
 https://blog.51cto.com/u_15849381/5801826
 python TypeError: ‘str‘ object does not support item assignment 报错解决方法
 https://blog.csdn.net/scp_6453/article/details/107866043
 解决python中“TypeError ‘str‘ object does not support item assignment”问题
 python字符串截取
 https://www.ycpai.cn/python/xBRw8f2K.html
 python怎么截取字符串
 print(string[2:8]) # 输出:llo, W
 #for line in lines: python 逐行读取
 https://blog.csdn.net/weixin_39662834/article/details/109925505
 python逐行获取_Python逐行读取文件内容的三种方法
 python if else if
 https://blog.csdn.net/MeiWenjilu/article/details/127546798
 python中的if和if_else以及if_elif_else
 https://blog.csdn.net/ldgk3ekkd/article/details/126383516
 python使用docx模块读写docx文件的方法与docx模块常用方法
 https://www.php.cn/faq/395631.html
 Python读写docx文件的方法
![2023年中国智慧公安行业发展现况及发展趋势分析:数据化建设的覆盖范围不断扩大[图]](https://img-blog.csdnimg.cn/img_convert/952a82a99c8e9866a06138f2228ad6af.png)

















![[NOIP2003 普及组] 栈](https://img-blog.csdnimg.cn/img_convert/b8f7b8da75c169f7ba188bbd473ccbee.png)
