|
import gradio as gr |
|
from moviepy.editor import VideoFileClip |
|
import os |
|
|
|
|
|
def get_video_duration(video_path): |
|
clip = VideoFileClip(video_path) |
|
duration = clip.duration |
|
clip.close() |
|
return duration |
|
|
|
|
|
def edit_video(video_file, start_time, end_time, resolution): |
|
if video_file is None: |
|
return "Sube un vídeo primero.", None |
|
|
|
input_path = video_file.name |
|
clip = VideoFileClip(input_path) |
|
|
|
|
|
if start_time < 0 or end_time > clip.duration or start_time >= end_time: |
|
clip.close() |
|
return "Tiempos inválidos. Asegúrate de elegir un rango correcto.", None |
|
|
|
|
|
edited_clip = clip.subclip(start_time, end_time) |
|
|
|
|
|
resolutions = { |
|
"480p": (854, 480), |
|
"720p": (1280, 720), |
|
"1080p": (1920, 1080), |
|
"1440p": (2560, 1440), |
|
"4K": (3840, 2160) |
|
} |
|
|
|
|
|
target_resolution = resolutions[resolution] |
|
edited_clip = edited_clip.resize(height=target_resolution[1]) |
|
|
|
|
|
output_filename = f"edited_{resolution}_{os.path.basename(input_path)}" |
|
edited_clip.write_videofile(output_filename, codec="libx264", audio_codec="aac") |
|
|
|
edited_clip.close() |
|
clip.close() |
|
|
|
return "Vídeo editado correctamente.", output_filename |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# 🎬 Editor de Vídeos Interactivo") |
|
|
|
video_input = gr.File(label="Sube tu vídeo aquí", file_types=["video"]) |
|
duration_display = gr.Textbox(label="Duración del vídeo (segundos)", interactive=False) |
|
|
|
with gr.Row(): |
|
start_time = gr.Number(label="Tiempo inicial (segundos)", value=0) |
|
end_time = gr.Number(label="Tiempo final (segundos)", value=10) |
|
|
|
resolution = gr.Dropdown(label="Resolución de exportación", |
|
choices=["480p", "720p", "1080p", "1440p", "4K"], |
|
value="1080p") |
|
|
|
edit_button = gr.Button("Editar Vídeo") |
|
status_display = gr.Textbox(label="Estado", interactive=False) |
|
video_output = gr.Video(label="Vídeo editado") |
|
|
|
|
|
def update_duration(video): |
|
if video is not None: |
|
duration = get_video_duration(video.name) |
|
return duration |
|
return "" |
|
|
|
video_input.change(fn=update_duration, inputs=video_input, outputs=duration_display) |
|
|
|
|
|
edit_button.click( |
|
fn=edit_video, |
|
inputs=[video_input, start_time, end_time, resolution], |
|
outputs=[status_display, video_output] |
|
) |
|
|
|
demo.launch() |
|
|