-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.py
More file actions
82 lines (62 loc) · 2.66 KB
/
extract.py
File metadata and controls
82 lines (62 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
import json
import sys
import re
from get_route import obtener_nombre_usuario_os
def extract_subtitle_text(ruta_archivo_json):
"""
Para usarlo ejecutar este archivo con python extract.py y nombre del video de Capcut
"""
try:
with open(ruta_archivo_json, 'r', encoding='utf-8') as f:
datos_json = json.load(f)
except FileNotFoundError:
print(f"Error: No se encontró el archivo en la ruta: {ruta_archivo_json}")
return
except json.JSONDecodeError as e:
print(f"Error: El archivo no es un JSON válido. Error: {e}")
return
except Exception as e:
print(f"Error inesperado al leer el archivo: {e}")
return
subtitle_texts = []
materials = datos_json.get("materials")
if not materials:
print("Error: No se encontró la clave 'materials' en el JSON.")
return
texts_list = materials.get("texts")
if not texts_list:
print("Advertencia: No se encontraron elementos de texto en 'materials'.")
return
for text_item in texts_list:
content_str = text_item.get("content")
if content_str:
try:
content_obj = json.loads(content_str)
text_value = content_obj.get("text")
if text_value:
subtitle_texts.append(text_value)
except json.JSONDecodeError as e:
text_pattern = re.compile(r'"text":"([^"]*)"')
match = text_pattern.search(content_str)
if match:
text_value = match.group(1)
subtitle_texts.append(text_value)
else:
print(f"Error al decodificar JSON en el campo 'content': {e}. Valor: {content_str[:50]}...")
subtitles_final = "\n".join(list(dict.fromkeys(subtitle_texts)))
base_name = os.path.splitext(os.path.basename(ruta_archivo_json))[0]
nombre_script_final = ruta_archivo_json.split('\\')[9]
archivo_salida = f"{nombre_script_final}_transcript.txt"
if subtitles_final:
with open(archivo_salida, "w", encoding="utf-8") as f:
f.write(subtitles_final)
print(f"\n✅ Transcripción guardada ({len(subtitle_texts)} lineas):")
print(f"El texto ha sido guardado en: '{archivo_salida}'")
else:
print("\n❌ No se pudo extraer ningún subtítulo. Archivo de salida no creado.")
if __name__ == "__main__":
if len(sys.argv) < 2:
sys.exit(1)
ruta_del_json = obtener_nombre_usuario_os(sys.argv[1])
extract_subtitle_text(ruta_del_json)