Sure! If your subtitle file is in a custom or malformed format like this:
[00:00:00.000 --> 00:00:08.040] To be honest, I actually want to get you the
[00:00:08.040 --> 00:00:14.460] Because I know you have the most interesting
[00:00:14.460 --> 00:00:16.460] I've been trading all day. So
We can use a Python script to convert it into a valid .srt (SubRip) format.
✅ Python Script to Convert Subtitles to SRT
Save this as convert_to_srt.py:
import re
import sys
def convert_to_srt(input_path, output_path):
with open(input_path, 'r', encoding='utf-8') as infile:
content = infile.read()
# Split by lines but keep empty lines for grouping
lines = content.splitlines()
srt_lines = []
index = 1
i = 0
while i < len(lines):
line = lines[i].strip()
if not line:
i += 1
continue
# Match timestamp line
ts_match = re.match(r'$$([0-9:\.]+) --> ([0-9:\.]+)$$', line)
if ts_match:
start, end = ts_match.groups()
# Convert .xxx to ,xxx for SRT format and fix time format
start = start.replace('.', ',')
end = end.replace('.', ',')
# Handle cases where milliseconds may be missing
if ',' not in start:
start += ',000'
if ',' not in end:
end += ',000'
# Next line is the text
i += 1
if i < len(lines):
text = lines[i].strip()
else:
text = ''
# Add SRT block
srt_lines.append(str(index))
srt_lines.append(f"{start} --> {end}")
srt_lines.append(text)
srt_lines.append('') # Empty line between entries
index += 1
i += 1
with open(output_path, 'w', encoding='utf-8') as outfile:
outfile.write('\n'.join(srt_lines))
print(f"✅ Subtitle file saved as {output_path}")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python convert_to_srt.py input.txt output.srt")
else:
convert_to_srt(sys.argv[1], sys.argv[2])
📦 How to Use It
1. Save your input subtitles into a file, e.g., input_subtitles.txt.
Example content of input_subtitles.txt:
[00:00:00.000 --> 00:00:08.040] To be honest, I actually want to get you the
[00:00:08.040 --> 00:00:14.460] Because I know you have the most interesting
[00:00:14.460 --> 00:00:16.460] I've been trading all day. So
2. Run the script:
python convert_to_srt.py input_subtitles.txt subtitles.srt
This will generate a properly formatted file.