''' Run it in srt file folder. python 3.x ''' import glob import re SRTBLOCKREGEX =…
- User
''' Run it in srt file folder. python 3.x ''' import glob import re
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]*)')
def srt_block_to_irc(block): match = SRT_BLOCK_REGEX.search(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, te)
def srt_file_to_irc(fname): with open(fname, encoding='utf8') as file_in: str_in = file_in.read() blocks_in = str_in.replace('\r\n', '\n').split('\n\n') blocks_out = [srt_block_to_irc(block) 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 = 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, blocks_num, context in err_info: print('\tfile: %s, block num: %s, context: %s' % (file_name, blocks_num, context)) else: print('success')
this work flawlessly, but when it reaches 1 hour part it parse 02:44:40,960 --> 02:44:41,960 into [44:41.96] missing the hour part. this script was designed to handle timestamps in the format of mm:ss,SSS, but it doesn’t correctly handle the hour part (hh:mm:ss,SSS). Because of that, the script fails to parse and include the hour part correctly in the output
- Assistant
It appears that the issue lies in the way the timestamp is formatted from the matched groups in the
srt_block_to_ircfunction. The script is designed to handle timestamps with the HH:MM:SS,SSS format, but it currently removes the hour part.To fix this, we need to ensure that the entire timestamp, including the hour, is preserved when formatting. Let's modify the code accordingly: