File size: 12,137 Bytes
4a04dae 5d96110 4a04dae 5d96110 4a04dae 5d96110 8eb6c1b 4a04dae 5d96110 fc4d759 4a04dae 5da1288 8eb6c1b 4a04dae 5d96110 5da1288 5d96110 f46873e 8eb6c1b 4a04dae 8eb6c1b 4a04dae eb21acb 8eb6c1b eb21acb 8eb6c1b eb21acb 4a04dae 2b31c2c 1107c5d 2b31c2c dc23696 2b31c2c 1107c5d fc4d759 d97f0d8 8eb6c1b fc4d759 8eb6c1b 5da1288 504becd 8eb6c1b 5d96110 8eb6c1b 5d96110 8eb6c1b 5d96110 29652ae 5d96110 29652ae 6fdb9ae 5d96110 6fdb9ae f46873e 6fdb9ae 5d96110 6fdb9ae dc23696 5d96110 dc23696 6fdb9ae dc23696 27a4f8e 6fdb9ae |
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
import tempfile
import logging
import os
import asyncio
from moviepy.editor import *
import edge_tts
import gradio as gr
from pydub import AudioSegment
# Configuraci贸n de Logs
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
# CONSTANTES DE ARCHIVOS
INTRO_VIDEO = "introvideo.mp4"
OUTRO_VIDEO = "outrovideo.mp4"
MUSIC_BG = "musicafondo.mp3"
EJEMPLO_VIDEO = "ejemplo.mp4"
# Validar existencia de archivos
for file in [INTRO_VIDEO, OUTRO_VIDEO, MUSIC_BG, EJEMPLO_VIDEO]:
if not os.path.exists(file):
logging.error(f"Falta archivo necesario: {file}")
raise FileNotFoundError(f"Falta: {file}")
# Configuraci贸n de chunks
SEGMENT_DURATION = 30 # Duraci贸n exacta entre transiciones (sin overlap)
TRANSITION_DURATION = 1.5 # Duraci贸n del efecto slide
def eliminar_archivo_tiempo(ruta, delay=1800):
def eliminar():
try:
if os.path.exists(ruta):
os.remove(ruta)
logging.info(f"Archivo eliminado: {ruta}")
except Exception as e:
logging.error(f"Error al eliminar {ruta}: {e}")
from threading import Timer
Timer(delay, eliminar).start()
def validar_video(video_path):
try:
clip = VideoFileClip(video_path)
clip.close()
return True
except Exception as e:
logging.error(f"El video no es v谩lido: {e}")
return False
def convertir_video(video_path):
try:
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_converted:
output_path = tmp_converted.name
os.system(f'ffmpeg -i "{video_path}" -vcodec libx264 -acodec aac "{output_path}" -y')
return output_path
except Exception as e:
logging.error(f"Error al convertir el video: {e}")
raise
def ajustar_resolucion(video_path):
try:
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_resized:
output_path = tmp_resized.name
os.system(f'ffmpeg -i "{video_path}" -vf "scale=1280:720" -vcodec libx264 -acodec aac "{output_path}" -y')
return output_path
except Exception as e:
logging.error(f"Error al ajustar la resoluci贸n del video: {e}")
raise
async def generar_tts(texto, voz, duracion_total):
try:
if not texto.strip():
raise ValueError("El texto para TTS no puede estar vac铆o.")
if len(texto) > 1000:
texto = texto[:1000]
# Eliminado la validaci贸n restrictiva de voces para permitir todas las que est谩n en el dropdown
logging.info(f"Generando TTS con voz: {voz}")
communicate = edge_tts.Communicate(texto, voz)
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_tts:
await communicate.save(tmp_tts.name)
tts_audio = AudioFileClip(tmp_tts.name)
if tts_audio.duration > duracion_total:
tts_audio = tts_audio.subclip(0, duracion_total)
return tts_audio, tmp_tts.name
except Exception as e:
logging.error(f"Fallo en TTS: {str(e)}")
raise
def crear_musica_fondo(duracion_total):
bg_music = AudioSegment.from_mp3(MUSIC_BG)
needed_ms = int(duracion_total * 1000)
repeticiones = needed_ms // len(bg_music) + 1
bg_music = bg_music * repeticiones
bg_music = bg_music[:needed_ms].fade_out(1000)
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_bg:
bg_music.export(tmp_bg.name, format="mp3")
return AudioFileClip(tmp_bg.name).volumex(0.15), tmp_bg.name
def create_slide_transition(clip1, clip2, duration=TRANSITION_DURATION):
part1 = clip1.subclip(clip1.duration - duration)
part2 = clip2.subclip(0, duration)
transition = CompositeVideoClip([
part1.fx(vfx.fadeout, duration),
part2.fx(vfx.fadein, duration).set_position(
lambda t: ('center', 720 - (720 * (t/duration)))
)
], size=(1280, 720)).set_duration(duration)
return transition
async def procesar_video(video_input, texto_tts, voz_seleccionada):
temp_files = []
intro, outro, video_original = None, None, None
try:
logging.info("Iniciando procesamiento")
if not validar_video(video_input):
video_input = convertir_video(video_input)
temp_files.append(video_input)
video_original = VideoFileClip(video_input, target_resolution=(720, 1280))
duracion_video = video_original.duration
if duracion_video <= 0:
raise ValueError("El video debe tener una duraci贸n mayor que cero.")
tts_audio, tts_path = await generar_tts(texto_tts, voz_seleccionada, duracion_video)
bg_audio, bg_path = crear_musica_fondo(duracion_video)
temp_files.extend([tts_path, bg_path])
audio_original = video_original.audio.volumex(0.7) if video_original.audio else None
audios = [bg_audio.set_duration(duracion_video)]
if audio_original:
audios.append(audio_original)
audios.append(tts_audio.set_start(0).volumex(0.85))
audio_final = CompositeAudioClip(audios).set_duration(duracion_video)
video_final = video_original.copy()
if duracion_video > SEGMENT_DURATION:
clips = []
num_segments = int(duracion_video // SEGMENT_DURATION) + (1 if duracion_video % SEGMENT_DURATION > 0 else 0)
for i in range(num_segments):
start_time = i * SEGMENT_DURATION
end_time = min(start_time + SEGMENT_DURATION, duracion_video)
segment = video_original.subclip(start_time, end_time)
if i == 0:
clips.append(segment)
else:
prev_segment = clips[-1]
transition = create_slide_transition(prev_segment, segment)
prev_end = prev_segment.duration - TRANSITION_DURATION
if prev_end > 0:
clips[-1] = prev_segment.subclip(0, prev_end)
clips.append(transition)
clips.append(segment)
video_final = concatenate_videoclips(clips, method="compose")
video_final = video_final.set_audio(audio_final)
intro = VideoFileClip(INTRO_VIDEO, target_resolution=(720, 1280))
outro = VideoFileClip(OUTRO_VIDEO, target_resolution=(720, 1280))
video_final = concatenate_videoclips([intro, video_final, outro], method="compose")
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp:
video_final.write_videofile(
tmp.name,
codec="libx264",
audio_codec="aac",
fps=24,
threads=2,
bitrate="3M",
ffmpeg_params=[
"-preset", "ultrafast",
"-crf", "28",
"-movflags", "+faststart",
"-vf", "scale=1280:720"
],
verbose=False
)
eliminar_archivo_tiempo(tmp.name, 1800)
logging.info(f"Video final guardado: {tmp.name}")
return tmp.name
except Exception as e:
logging.error(f"Fallo general: {str(e)}")
raise
finally:
try:
if video_original:
video_original.close()
if intro:
intro.close()
if outro: # Corregido: outro en lugar de otro
outro.close()
for file in temp_files:
try:
os.remove(file)
except Exception as e:
logging.warning(f"Error limpiando {file}: {e}")
except Exception as e:
logging.warning(f"Error al cerrar recursos: {str(e)}")
# Interfaz Gradio
with gr.Blocks() as demo:
gr.Markdown("# Editor de Video con IA")
with gr.Tab("Principal"):
video_input = gr.Video(label="Subir video")
texto_tts = gr.Textbox(
label="Texto para TTS",
lines=3,
placeholder="Escribe aqu铆 tu texto..."
)
voz_seleccionada = gr.Dropdown(
label="Voz",
choices=[
"es-ES-AlvaroNeural", "es-MX-BeatrizNeural",
"es-ES-ElviraNeural", "es-MX-JavierNeural",
"es-AR-ElenaNeural", "es-AR-TomasNeural",
"es-CL-CatalinaNeural", "es-CL-LorenzoNeural",
"es-CO-SofiaNeural", "es-CO-GonzaloNeural",
"es-PE-CamilaNeural", "es-PE-AlexNeural",
"es-VE-MariaNeural", "es-VE-ManuelNeural",
"es-US-AlonsoNeural", "es-US-PalomaNeural",
"es-ES-AbrilNeural", "es-ES-DarioNeural",
"es-ES-HelenaRUS", "es-ES-LauraNeural",
"es-ES-PabloNeural", "es-ES-TriniNeural",
"en-US-AriaNeural", "en-US-GuyNeural",
"en-US-JennyNeural", "en-US-AmberNeural",
"en-US-AnaNeural", "en-US-AshleyNeural",
"en-US-BrandonNeural", "en-US-ChristopherNeural",
"en-US-CoraNeural", "en-US-DavisNeural",
"en-US-ElizabethNeural", "en-US-EricNeural",
"en-US-GinaNeural", "en-US-JacobNeural",
"en-US-JaneNeural", "en-US-JasonNeural",
"en-US-MichelleNeural", "en-US-MonicaNeural",
"en-US-SaraNeural", "en-US-SteffanNeural",
"en-US-TonyNeural", "en-US-YaraNeural",
"fr-FR-AlainNeural", "fr-FR-BrigitteNeural",
"fr-FR-CelesteNeural", "fr-FR-ClaudeNeural",
"fr-FR-CoralieNeural", "fr-FR-DeniseNeural",
"fr-FR-EloiseNeural", "fr-FR-HenriNeural",
"fr-FR-JacquelineNeural", "fr-FR-JeromeNeural",
"fr-FR-JosephineNeural", "fr-FR-MauriceNeural",
"fr-FR-YvesNeural", "fr-FR-YvetteNeural",
"de-DE-AmalaNeural", "de-DE-BerndNeural",
"de-DE-ChristophNeural", "de-DE-ConradNeural",
"de-DE-ElkeNeural", "de-DE-GiselaNeural",
"de-DE-KasperNeural", "de-DE-KatjaNeural",
"de-DE-KillianNeural", "de-DE-KlarissaNeural",
"de-DE-KlausNeural", "de-DE-LouisaNeural",
"de-DE-MajaNeural", "de-DE-RalfNeural",
"de-DE-TanjaNeural", "de-DE-ViktoriaNeural",
"it-IT-BenignoNeural", "it-IT-CalimeroNeural",
"it-IT-CataldoNeural", "it-IT-DiegoNeural",
"it-IT-ElsaNeural", "it-IT-FabiolaNeural",
"it-IT-GianniNeural", "it-IT-ImeldaNeural",
"it-IT-IrmaNeural", "it-IT-IsabellaNeural",
"it-IT-LisandroNeural", "it-IT-PalmiraNeural",
"it-IT-PierinaNeural", "it-IT-RinaldoNeural",
"ja-JP-AoiNeural", "ja-JP-DaichiNeural",
"ja-JP-HarukaNeural", "ja-JP-KeitaNeural",
"ja-JP-MayuNeural", "ja-JP-NanamiNeural",
"ja-JP-NaokiNeural", "ja-JP-ShioriNeural"
],
value="es-ES-AlvaroNeural"
)
procesar_btn = gr.Button("Generar Video")
video_output = gr.Video(label="Video Procesado")
with gr.Accordion("Ejemplos de Uso", open=False):
gr.Examples(
examples=[[EJEMPLO_VIDEO, "隆Hola! Esto es una prueba. Suscr铆bete al canal."]],
inputs=[video_input, texto_tts],
label="Ejemplos"
)
procesar_btn.click(
procesar_video,
inputs=[video_input, texto_tts, voz_seleccionada],
outputs=video_output
)
gr.Markdown("""
### 鈩癸笍 Notas importantes:
- Las transiciones ocurren solamente cada 30 segundos
- El video contiene intro y outro predefinidos
- El archivo generado se elimina despu茅s de 30 minutos
- Para mejores resultados, usa videos de dimensiones 720p o 1080p
""")
if __name__ == "__main__":
demo.queue().launch() |