import argparse def convert_to_srt(input_file, output_file): with open(input_file, 'r', encoding='utf-8') as file: lines = file.readlines() srt_lines = [] subtitle_count = 1 for line in lines: if line.strip() == '': continue start_time, end_time, text = parse_line(line) srt_line = f"{subtitle_count}\n{format_time(start_time)} --> {format_time(end_time)}\n{text}\n\n" srt_lines.append(srt_line) subtitle_count += 1 with open(output_file, 'w', encoding='utf-8') as file: file.writelines(srt_lines) def parse_line(line): time_range, text = line.strip().split('] ', maxsplit=1) start_time, end_time = time_range.strip('[]').split(' -> ') return start_time, end_time, text def format_time(time): minutes, seconds = time.split(':') seconds, milliseconds = seconds.split('.') return f"00:{minutes}:{seconds},{milliseconds}" # Parse command-line arguments parser = argparse.ArgumentParser(description='Convert text file to SRT format.') parser.add_argument('input_file', help='Path to the input text file') parser.add_argument('output_file', help='Path to the output SRT file') args = parser.parse_args() # Convert the input file to SRT format convert_to_srt(args.input_file, args.output_file) print(f"Conversion completed. SRT file saved as {args.output_file}")