Created
December 9, 2023 00:36
-
-
Save delineas/2af3270ac87b815f63f39f4d4b723360 to your computer and use it in GitHub Desktop.
Convert SRT to TXT batch mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import argparse | |
# USE: python script.py ./srt-folder-source ./txt-folder-destination | |
def convert_srt_to_txt(src_folder, dest_folder): | |
for filename in os.listdir(src_folder): | |
if filename.endswith('.srt'): | |
src_path = os.path.join(src_folder, filename) | |
dest_path = os.path.join(dest_folder, filename.replace('.srt', '.txt')) | |
with open(src_path, 'r', encoding='utf-8') as src_file: | |
lines = src_file.readlines() | |
with open(dest_path, 'w', encoding='utf-8') as dest_file: | |
for line in lines: | |
# Elimina marcas de tiempo y otros metadatos | |
if re.match(r'^\d+$', line.strip()) or re.match(r'^\d\d:\d\d:\d\d,\d\d\d --> \d\d:\d\d:\d\d,\d\d\d$', line.strip()): | |
continue | |
if line.strip(): | |
dest_file.write(line) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Convertir archivos SRT a TXT.') | |
parser.add_argument('src_folder', type=str, help='Ruta de la carpeta de origen con archivos SRT.') | |
parser.add_argument('dest_folder', type=str, help='Ruta de la carpeta de destino para los archivos TXT.') | |
args = parser.parse_args() | |
convert_srt_to_txt(args.src_folder, args.dest_folder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment