Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import subprocess
- import sys
- import json
- import argparse
- import openai
- from tqdm import tqdm
- client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
- def should_transcode(input_file):
- command = [
- "ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=bit_rate,height", "-of", "json", input_file
- ]
- try:
- result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, check=True)
- video_info = json.loads(result.stdout)
- video_stream = video_info['streams'][0]
- video_bitrate = int(video_stream.get('bit_rate', 0))
- video_height = int(video_stream.get('height', 0))
- command_audio = [
- "ffprobe", "-v", "error", "-select_streams", "a:0", "-show_entries", "stream=bit_rate", "-of", "json", input_file
- ]
- result_audio = subprocess.run(command_audio, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, check=True)
- audio_info = json.loads(result_audio.stdout)
- audio_stream = audio_info['streams'][0]
- audio_bitrate = int(audio_stream.get('bit_rate', 0))
- should_transcode = video_bitrate > 1000000 or video_height > 540 or audio_bitrate > 128000
- print(f"Debug: Video bitrate: {video_bitrate}, Video height: {video_height}, Audio bitrate: {audio_bitrate}, Should transcode: {should_transcode}", flush=True)
- return should_transcode
- except subprocess.CalledProcessError as e:
- print(f"Error al verificar el archivo {input_file}: {e.stderr}")
- return False
- def transcode_video(input_file, temp_file):
- print("Debug: Iniciando transcodificación...", flush=True)
- print(f"Iniciando transcodificación del archivo: {input_file}", flush=True)
- command = [
- "ffmpeg", "-i", input_file,
- "-vf", "scale=-1:540,fps=fps=25",
- "-c:v", "h264_qsv", "-preset", "veryfast", "-b:v", "1000k", "-minrate", "1000k", "-maxrate", "1000k", "-bufsize", "2800k", "-x264-params", "nal-hrd=cbr",
- "-c:a", "aac", "-b:a", "128k", "-ac", "2",
- "-map", "0",
- "-c:s", "copy",
- "-f", "matroska",
- temp_file
- ]
- try:
- print(f"Debug: Ejecutando comando: {' '.join(command)}", flush=True)
- process = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, check=True)
- print(f"FFmpeg stderr output: {process.stderr}", flush=True)
- except subprocess.CalledProcessError as e:
- print(f"Error al transcodificar el archivo {input_file}: {e.stderr}")
- return False
- return True
- def translate_text(text, target_language="es"):
- chat_completion = client.chat.completions.create(
- messages=[
- {
- "role": "system",
- "content": f"Translate the following text to {target_language}.",
- },
- {
- "role": "user",
- "content": text,
- },
- ],
- model="gpt-4o-mini",
- )
- translation = chat_completion.choices[0].message.content.strip()
- return translation
- def find_subtitle_file(input_file):
- base_name = os.path.splitext(input_file)[0]
- for ext in [".srt", ".en.srt", ".eng.srt", ".es.srt", ".spa.srt"]:
- potential_file = f"{base_name}{ext}"
- if os.path.exists(potential_file):
- return potential_file
- return None
- def has_embedded_subtitles(input_file, language_code="es"):
- command = [
- "ffprobe", "-v", "error", "-select_streams", "s", "-show_entries", "stream_tags=language", "-of", "json", input_file
- ]
- try:
- result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, check=True)
- subtitles_info = json.loads(result.stdout)
- for stream in subtitles_info.get('streams', []):
- if stream.get('tags', {}).get('language') == language_code:
- return True
- return False
- except subprocess.CalledProcessError as e:
- print(f"Error al verificar subtítulos incrustados en {input_file}: {e.stderr}")
- return False
- def extract_embedded_subtitles(input_file, output_file):
- command = [
- "ffmpeg", "-i", input_file, "-map", "0:s:0", output_file
- ]
- try:
- subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, check=True)
- print(f"Subtítulos extraídos a {output_file}")
- except subprocess.CalledProcessError as e:
- print(f"Error al extraer subtítulos incrustados del archivo {input_file}: {e.stderr}")
- def translate_srt(file_path, target_language="es"):
- if not os.path.exists(file_path):
- print(f"Error: No se encontraron subtítulos compatibles para el archivo {file_path}.", flush=True)
- return
- with open(file_path, 'r') as file:
- lines = file.readlines()
- output_file_path = file_path.replace('.en.srt', f'.{target_language}.srt').replace('.eng.srt', f'.{target_language}.srt').replace('_extracted.srt', f'.{target_language}.srt')
- with open(output_file_path, 'w') as output_file, tqdm(total=len(lines), desc="Translating", unit="subtitle") as pbar:
- text_to_translate = ""
- subtitle_block = []
- for line in lines:
- if line.strip().isdigit():
- if subtitle_block:
- translated_text = translate_text(text_to_translate, target_language)
- output_file.writelines(subtitle_block[:-1])
- output_file.write(translated_text + '\n')
- output_file.write('\n')
- output_file.flush()
- subtitle_block = [line]
- text_to_translate = ""
- elif '-->' in line:
- subtitle_block.append(line)
- elif line.strip():
- text_to_translate += line.strip() + " "
- else:
- subtitle_block.append(line)
- pbar.update(1)
- if subtitle_block:
- translated_text = translate_text(text_to_translate, target_language)
- output_file.writelines(subtitle_block[:-1])
- output_file.write(translated_text + '\n')
- output_file.write('\n')
- output_file.flush()
- pbar.update(1)
- print(f'Translation completed. Output file: {output_file_path}')
- def main():
- parser = argparse.ArgumentParser(description="Transcodificar videos y agregar subtítulos")
- parser.add_argument('input_file', type=str, help="Ruta al archivo de video a procesar")
- args = parser.parse_args()
- input_file = args.input_file
- temp_file = f"{os.path.splitext(input_file)[0]}_temp.mkv"
- print("Debug: Iniciando proceso principal...", flush=True)
- if should_transcode(input_file):
- if transcode_video(input_file, temp_file):
- # Replace the original file with the transcoded one
- os.replace(temp_file, input_file)
- if not has_embedded_subtitles(input_file, language_code="es"):
- subtitle_file = find_subtitle_file(input_file)
- if subtitle_file:
- if ".es." not in subtitle_file and ".spa." not in subtitle_file:
- translate_srt(subtitle_file, target_language="es")
- else:
- embedded_subtitle_file = f"{os.path.splitext(input_file)[0]}.es.srt"
- extract_embedded_subtitles(input_file, embedded_subtitle_file)
- translate_srt(embedded_subtitle_file, target_language="es")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement