Merlintxu commited on
Commit
d93b1a7
·
verified ·
1 Parent(s): a589ee0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -83
app.py CHANGED
@@ -1,85 +1,72 @@
1
- import gradio as gr
2
- from moviepy.video.io.VideoFileClip 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()
 
1
+ import streamlit as st
2
+ import moviepy.editor as mp
3
+ import tempfile
4
  import os
5
 
6
+ def main():
7
+ st.title("Simple Video Editor")
8
+
9
+ # File uploaders
10
+ video_file = st.file_uploader("Upload a video file (MP4)", type=["mp4"])
11
+ audio_file = st.file_uploader("Upload an audio file (MP3 or WAV)", type=["mp3", "wav"])
12
+
13
+ if video_file and audio_file:
14
+ # Save uploaded files to temporary locations
15
+ temp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
16
+ temp_video.write(video_file.read())
17
+ temp_video_path = temp_video.name
18
+
19
+ temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=f".{audio_file.name.split('.')[-1]}")
20
+ temp_audio.write(audio_file.read())
21
+ temp_audio_path = temp_audio.name
22
+
23
+ # Load video and audio
24
+ video = mp.VideoFileClip(temp_video_path)
25
+ audio = mp.AudioFileClip(temp_audio_path)
26
+
27
+ # Display video info
28
+ st.write(f"Video duration: {video.duration:.2f} seconds")
29
+ st.write(f"Audio duration: {audio.duration:.2f} seconds")
30
+
31
+ # Trim video
32
+ st.subheader("Trim Video")
33
+ video_start = st.slider("Video start time (seconds)", 0.0, video.duration, 0.0)
34
+ video_end = st.slider("Video end time (seconds)", 0.0, video.duration, video.duration)
35
+ trimmed_video = video.subclip(video_start, video_end)
36
+
37
+ # Trim audio
38
+ st.subheader("Trim Audio")
39
+ audio_start = st.slider("Audio start time (seconds)", 0.0, audio.duration, 0.0)
40
+ audio_end = st.slider("Audio end time (seconds)", 0.0, audio.duration, audio.duration)
41
+ trimmed_audio = audio.subclip(audio_start, audio_end)
42
+
43
+ # Combine video and audio
44
+ if st.button("Combine Video and Audio"):
45
+ final_clip = trimmed_video.set_audio(trimmed_audio)
46
+
47
+ # Save the final video
48
+ output_path = "output_video.mp4"
49
+ final_clip.write_videofile(output_path, codec="libx264", audio_codec="aac")
50
+
51
+ # Provide download link
52
+ st.success("Video processing complete!")
53
+ with open(output_path, "rb") as file:
54
+ btn = st.download_button(
55
+ label="Download processed video",
56
+ data=file,
57
+ file_name="processed_video.mp4",
58
+ mime="video/mp4"
59
+ )
60
+
61
+ # Display video player
62
+ st.subheader("Preview Processed Video")
63
+ st.video(output_path)
64
+
65
+ # Clean up temporary files
66
+ video.close()
67
+ audio.close()
68
+ os.unlink(temp_video_path)
69
+ os.unlink(temp_audio_path)
70
+
71
+ if __name__ == "__main__":
72
+ main()