|
import gradio as gr |
|
from moviepy.editor import VideoFileClip, concatenate_videoclips, AudioFileClip |
|
from moviepy.audio.fx.all import audio_fadeout |
|
import random |
|
import subprocess |
|
import os |
|
|
|
|
|
def convertir_mov_a_mp4(ruta_mov): |
|
ruta_mp4 = ruta_mov.replace('.MOV', '.mp4') |
|
subprocess.run(['ffmpeg', '-i', ruta_mov, '-vcodec', 'h264', '-acodec', 'aac', ruta_mp4], check=True) |
|
return ruta_mp4 |
|
|
|
|
|
def process_videos(videos, rotate_option, song): |
|
clips = [] |
|
errores = [] |
|
|
|
|
|
for video_path in videos: |
|
try: |
|
if video_path.endswith('.MOV'): |
|
|
|
video_path = convertir_mov_a_mp4(video_path) |
|
if video_path.endswith(('.mp4')): |
|
clip = VideoFileClip(video_path).without_audio() |
|
|
|
|
|
if clip.duration > 3: |
|
clip = clip.subclip(0, 3) |
|
|
|
|
|
if rotate_option == "90 grados": |
|
clip = clip.rotate(90) |
|
elif rotate_option == "180 grados": |
|
clip = clip.rotate(180) |
|
elif rotate_option == "270 grados": |
|
clip = clip.rotate(270) |
|
|
|
clips.append(clip) |
|
else: |
|
errores.append(f"Archivo no compatible: {video_path}") |
|
except Exception as e: |
|
errores.append(f"Error al procesar {video_path}: {str(e)}") |
|
|
|
|
|
if len(clips) == 0: |
|
return None, None |
|
|
|
|
|
random.shuffle(clips) |
|
|
|
|
|
try: |
|
final_clip = concatenate_videoclips(clips) |
|
|
|
|
|
if song: |
|
song_audio = AudioFileClip(song) |
|
song_audio = song_audio.subclip(0, final_clip.duration) |
|
song_audio = audio_fadeout(song_audio, 3) |
|
final_clip = final_clip.set_audio(song_audio) |
|
|
|
|
|
output_path = "output_video.mp4" |
|
final_clip.write_videofile(output_path, codec="libx264", audio_codec="aac") |
|
|
|
|
|
return output_path, output_path |
|
except Exception as e: |
|
return None, None |
|
|
|
|
|
demo = gr.Interface( |
|
fn=process_videos, |
|
inputs=[ |
|
gr.Files(label="Sube tus videos (MP4, MOV)", file_count="multiple", type="filepath"), |
|
gr.Radio(["Sin rotaci贸n", "90 grados", "180 grados", "270 grados"], label="Rotar video"), |
|
gr.Audio(label="Sube una canci贸n", type="filepath") |
|
], |
|
outputs=[ |
|
gr.Video(label="Previsualiza tu video procesado"), |
|
gr.File(label="Descargar video procesado") |
|
], |
|
title="Mezclar y Unir Videos con Rotaci贸n y M煤sica", |
|
description="Sube varios videos, rec贸rtalos, m茅zclalos, reemplaza el audio con una canci贸n y aplica una rotaci贸n.", |
|
) |
|
|
|
|
|
demo.launch(share=True) |
|
|