Below is an example Python script that reads your subtitle file in the custom format and converts it into a valid SRT file. In the custom format each subtitle cue is enclosed in square brackets with start and end timecodes (using periods to separate milliseconds), followed by the text. The script uses regular expressions to extract each cue, converts the timecodes to SRT’s format (which uses commas), and writes the cues with sequential numbering.
Python Script (convert_subs.py)
import re
import sys
def convert_timecode(tc):
"""
Convert a timecode from the format "HH:MM:SS.mmm" to "HH:MM:SS,mmm"
"""
return tc.replace('.', ',')
def main():
if len(sys.argv) != 3:
print("Usage: python convert_subs.py input.txt output.srt")
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2]
# Read the entire file.
with open(input_file, "r", encoding="utf-8") as f:
content = f.read()
# Regular Expression Explanation:
# \[
# (\d{2}:\d{2}:\d{2}\.\d{3}) -> captures the start time (e.g., 00:00:00.000)
# \s*-->\s*
# (\d{2}:\d{2}:\d{2}\.\d{3}) -> captures the end time (e.g., 00:00:08.040)
# \]
# \s*
# ([^[]+?) -> lazily captures all characters until the next '[' (i.e. the text)
pattern = re.compile(
r'\[(\d{2}:\d{2}:\d{2}\.\d{3})\s*-->\s*(\d{2}:\d{2}:\d{2}\.\d{3})\]\s*([^[]+)',
re.DOTALL
)
matches = pattern.findall(content)
if not matches:
print("No cues were found. Please check the input file format.")
sys.exit(1)
srt_lines = []
for index, (start, end, text) in enumerate(matches, start=1):
start_converted = convert_timecode(start)
end_converted = convert_timecode(end)
text_cleaned = text.strip()
srt_lines.append(str(index))
srt_lines.append(f"{start_converted} --> {end_converted}")
srt_lines.append(text_cleaned)
srt_lines.append("") # Empty line between cues
with open(output_file, "w", encoding="utf-8") as f:
f.write("\n".join(srt_lines))
print(f"Conversion completed. SRT file saved as '{output_file}'.")
if __name__ == "__main__":
main()
How to Use the Script
-
Save the Script:
Copy the script into a file named convert_subs.py.
-
Prepare Your Input File:
For example, save your subtitle content (the custom format) in a file named input.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
-
Run the Script:
Open a terminal or command prompt in the folder containing and run: