20230812 Use python3 to convert SRT format subtitles to SSA format under WIN10

20230812Use python3 to convert SRT format subtitles to SSA format under WIN102023
/8/12 20:58


The SSA format of this article is based on the format of [Batch Subtitles Converter v1.23]!

1.
Origin: None of the various subtitle conversion software/gadgets found on the Internet fully satisfy me!
[There are all kinds of small flaws]
So I decided to write a practice by myself using python3!
The python script in this article only handles UTF8-encoded SRT format subtitles. For ANSI-encoded SRT format, please refer to this article for modification!
! ! ! ! Batch Subtitles Converter (batch subtitle conversion) v1.23 Green English version: The position and color/font/font size of the required SSA subtitles cannot be modified/customized.


subresync.exe: UTF8 encoded subtitles are not supported! And the position and color/font/font size of the required SSA subtitles cannot be modified/customized.
vobsub.dll

Technical points:
Red.Eye.2005.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.5.1-FGT.cn9.srt
converts the English letters of "Imminent" into Chinese SRT format subtitles after google translation into SSA format !

path_to_save_txt+utf8_file.cn.srt

1
00:02:12,766 --> 00:02:16,099
Tyler Bob and Marian Tyler are with me

2
00:02:16,100 --> 00:02:16,900
one second


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,Tyler Bob and Marian Taylor are with meDialogue
: Marked=0,0:02:16.10,0:02:16.90,Default,NTP,0000,0000,0000,!Effect,one Second


Important: To modify the timeline in SRT format to SSA format, you need to use the string processing/splitting function of python3!


PYTHON SRT to SSA
python timeline string splitting


https://www.techiedelight.com/zh/split-string-with-delimiters-python/Split
strings using delimiters in Python

Splitting Strings Using Delimiters in Python
Google Translate Icon
This article will discuss how to split strings using delimiters in Python.

1. Use the re.split() function
A simple solution to split a string by the occurrence of a pattern is to use the built-in function re.split(). A pattern can consist of one or more delimiters:

Split on a single delimiter

To split a string using a single delimiter, you simply pass that delimiter to the re.split() function.

import re
if __name__ == '__main__':
    s = 'Hello,World'
    res = re.split(',', s)                # split with comma
    print(res)                            # ['Hello', 'World']


Microsoft Windows [Version 10.0.19045.2311]
(c) Microsoft Corporation. all rights reserved.

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,Tyler Bob and Marianne Taylor are with me \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 Found the spaces in SRT
        if len(line) == 1:
            temp=1
            xuhao = xuhao+1
        #f2.write(line)
        # 2023/8/11 9:34 Chinese and English subtitles remove the space lines The subsequent timeline
        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,Tyler Bob and Marian Taylor are with me
            #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,Tyler Bob and Marian Taylor are with me
            #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,Tyler Bob and Marian Taylor are with me
            #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,Tyler Bob and Marian Taylor are with me #Dialogue: Marked
            = 0,00:02:12,76,00:02:16,09,Default,NTP,0000,0000,0000,!Effect,Tyler Bob and Marian Taylor are with me
            #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')

 

 


Reference:
https://mathpretty.com/10393.html
Python subtitle conversion tool–asstosrt


https://fiime.cn/blog/281970How
to implement ass/ssa batch conversion script to srt


https://www.techiedelight.com/zh/split-string-with-delimiters-python/Split
strings using delimiters in Python


https://zhuanlan.zhihu.com/p/460511660
How to cut a string in python


TypeError: 'str' object does not support item assignment
https://pythonjishu.com/python-error-66/
The reasons and solutions for Python reporting "TypeError: 'str' object does not support item assignment"


https://blog.51cto.com/u_15849381/5801826
python TypeError: 'str' object does not support item assignment error solution


https://blog.csdn.net/scp_6453/article/details/107866043
Solve the problem of "TypeError 'str' object does not support item assignment" in python


python string interception
https://www.ycpai.cn/python/xBRw8f2K.html
python how to intercept string
print(string[2:8]) # Output: llo, W


#for line in lines: python reads line by line
https://blog.csdn.net/weixin_39662834/article/details/109925505
python obtains line by line_Python three methods of reading file content line by line


python if else if
https://blog.csdn.net/MeiWenjilu/article/details/127546798
if and if_else and if_elif_else in python


https://blog.csdn.net/ldgk3ekkd/article/details/126383516
python method of using docx module to read and write docx files and common methods of docx module


https://www.php.cn/faq/395631.html
How to read and write docx files in Python

Guess you like

Origin blog.csdn.net/wb4916/article/details/132253914