To debug why some blocks in your SRT files fail to convert, we need to dive into different potential issues. However, first, let's address one key potential problem: the regex you use to parse the SRT file.
The existing regular expression (SRT_BLOCK_REGEX) used in your code is:
SRT_BLOCK_REGEX = re.compile(
r'(\d+)[^\S\r\n]*[\r\n]+'
r'(\d{2}:\d{2}:\d{2},\d{3,4})[^\S\r\n]*-->[^\S\r\n]*(\d{2}:\d{2}:\d{2},\d{3,4})[^\S\r\n]*[\r\n]+'
r'([\s\S]*)')
This regex assumes that the SRT format strictly follows certain patterns, and could fail if any slight formatting deviations occur in the SRT file.
Potential Problems:
- Timestamp Format: Your regex allows 3 to 4 digits for milliseconds but standard SRT typically uses 3 digits.
- Line Endings and White Spaces: Variations in white spaces or line endings might break parsing.
- Text Content: If the subtitle block text contains multiple blank lines or special sequences, the regex might fail.
Debugging Steps:
-
Inspecting Specific Failing Blocks: When a block conversion fails, print the context to see what differs from successfully converted blocks.
-
Update Regex: Ensure the regex is flexible enough to handle slight variations in formatting.
Improved Regex and Code:
Consider updating your regex to handle more flexible formatting:
SRT_BLOCK_REGEX = re.compile(
r'(\d+)\s*\n+'
r'(\d{2}:\d{2}:\d{2},\d{3})\s*-->\s*(\d{2}:\d{2}:\d{2},\d{3})\s*\n+'
r'([\s\S]*?)(?=\n+\d+\s*\n|\Z)')
With the ? and (?=\n+\d+\s*\n|\Z) lookahead to non-greedily match up to the next block or end of the string.
Updated Code:
Here’s the updated code integrating the new regex and better handling:
import glob
import re
SRT_BLOCK_REGEX = re.compile(
r'(\d+)\s*\n+' # Block number
r'(\d{2}:\d{2}:\d{2},\d{3})\s*-->\s*(\d{2}:\d{2}:\d{2},\d{3})\s*\n+' # Time frame
r'([\s\S]*?)(?=\n+\d+\s*\n|\Z)') # Subtitle text
def srt_block_to_irc(block):
match = SRT_BLOCK_REGEX.match(block)
if not match:
return None
num, ts, te, content = match.groups()
ts = ts[3:-1].replace(',', '.')
te = te[3:-1].replace(',', '.')
co = content.replace('\n', ' ')
return '[%s]%s\n[%s]\n' % (ts, co.strip(), te)
def srt_file_to_irc(fname):
with open(fname, encoding='utf8') as file_in:
str_in = file_in.read()
blocks_in = str_in.split('\n\n')
blocks_out = [srt_block_to_irc(block.strip()) for block in blocks_in]
if not all(blocks_out):
err_info.append((fname, blocks_out.index(None), blocks_in[blocks_out.index(None)]))
blocks_out = list(filter(None, blocks_out))
str_out = ''.join(blocks_out)
with open(fname.replace('.srt', '.lrc'), 'w', encoding='utf8') as file_out:
file_out.write(str_out)
if __name__ == '__main__':
err_info = []
for file_name in glob.glob('*.srt'):
srt_file_to_irc(file_name)
if err_info:
print('success, but some exceptions are ignored:')
for file_name, block_num, context in err_info:
print(f'file: {file_name}, block num: {block_num}, context: {context}')
else:
print('success')
This code improves error handling and regex flexibility, increasing the likelihood of successfully parsing varied SRT formats. If any blocks still fail, the debugging print statements should provide insight into what specific part of the block is causing issues.