Merlintxu commited on
Commit
c4a2319
·
verified ·
1 Parent(s): 7f0478f

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +85 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from moviepy.editor import VideoFileClip
3
+ import os
4
+
5
+ # Función para obtener duración del vídeo
6
+ def get_video_duration(video_path):
7
+ clip = VideoFileClip(video_path)
8
+ duration = clip.duration
9
+ clip.close()
10
+ return duration
11
+
12
+ # Función para cortar y redimensionar el vídeo
13
+ def edit_video(video_file, start_time, end_time, resolution):
14
+ if video_file is None:
15
+ return "Sube un vídeo primero.", None
16
+
17
+ input_path = video_file.name
18
+ clip = VideoFileClip(input_path)
19
+
20
+ # Asegurar que los tiempos son válidos
21
+ if start_time < 0 or end_time > clip.duration or start_time >= end_time:
22
+ clip.close()
23
+ return "Tiempos inválidos. Asegúrate de elegir un rango correcto.", None
24
+
25
+ # Cortar el clip según tiempos proporcionados
26
+ edited_clip = clip.subclip(start_time, end_time)
27
+
28
+ # Definir resoluciones estándar
29
+ resolutions = {
30
+ "480p": (854, 480),
31
+ "720p": (1280, 720),
32
+ "1080p": (1920, 1080),
33
+ "1440p": (2560, 1440),
34
+ "4K": (3840, 2160)
35
+ }
36
+
37
+ # Redimensionar el vídeo manteniendo aspecto original
38
+ target_resolution = resolutions[resolution]
39
+ edited_clip = edited_clip.resize(height=target_resolution[1])
40
+
41
+ # Guardar resultado
42
+ output_filename = f"edited_{resolution}_{os.path.basename(input_path)}"
43
+ edited_clip.write_videofile(output_filename, codec="libx264", audio_codec="aac")
44
+
45
+ edited_clip.close()
46
+ clip.close()
47
+
48
+ return "Vídeo editado correctamente.", output_filename
49
+
50
+ # Interfaz de Gradio
51
+ with gr.Blocks() as demo:
52
+ gr.Markdown("# 🎬 Editor de Vídeos Interactivo")
53
+
54
+ video_input = gr.File(label="Sube tu vídeo aquí", file_types=["video"])
55
+ duration_display = gr.Textbox(label="Duración del vídeo (segundos)", interactive=False)
56
+
57
+ with gr.Row():
58
+ start_time = gr.Number(label="Tiempo inicial (segundos)", value=0)
59
+ end_time = gr.Number(label="Tiempo final (segundos)", value=10)
60
+
61
+ resolution = gr.Dropdown(label="Resolución de exportación",
62
+ choices=["480p", "720p", "1080p", "1440p", "4K"],
63
+ value="1080p")
64
+
65
+ edit_button = gr.Button("Editar Vídeo")
66
+ status_display = gr.Textbox(label="Estado", interactive=False)
67
+ video_output = gr.Video(label="Vídeo editado")
68
+
69
+ # Actualizar duración cuando se sube un vídeo
70
+ def update_duration(video):
71
+ if video is not None:
72
+ duration = get_video_duration(video.name)
73
+ return duration
74
+ return ""
75
+
76
+ video_input.change(fn=update_duration, inputs=video_input, outputs=duration_display)
77
+
78
+ # Editar vídeo al pulsar el botón
79
+ edit_button.click(
80
+ fn=edit_video,
81
+ inputs=[video_input, start_time, end_time, resolution],
82
+ outputs=[status_display, video_output]
83
+ )
84
+
85
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ moviepy