Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from moviepy.editor import VideoFileClip, AudioFileClip, CompositeVideoClip
|
3 |
+
|
4 |
+
def video_editor(video, audio, music, width, height, output_title):
|
5 |
+
# Load video and audio clips
|
6 |
+
video_clip = VideoFileClip(video.name)
|
7 |
+
audio_clip = AudioFileClip(audio.name)
|
8 |
+
|
9 |
+
# Resize video using width and height
|
10 |
+
if width and height:
|
11 |
+
video_clip = video_clip.resize(newsize=(width, height))
|
12 |
+
|
13 |
+
# Add background music if provided
|
14 |
+
if music:
|
15 |
+
music_clip = AudioFileClip(music.name)
|
16 |
+
final_audio = CompositeVideoClip([audio_clip, music_clip.set_duration(video_clip.duration)])
|
17 |
+
else:
|
18 |
+
final_audio = audio_clip
|
19 |
+
|
20 |
+
# Combine video with the final audio
|
21 |
+
final_clip = video_clip.set_audio(final_audio)
|
22 |
+
|
23 |
+
# Define output file path
|
24 |
+
output_file = f"{output_title}.mp4"
|
25 |
+
|
26 |
+
# Write the final video file
|
27 |
+
final_clip.write_videofile(output_file, codec="libx264")
|
28 |
+
|
29 |
+
return output_file
|
30 |
+
|
31 |
+
# Define the Gradio interface
|
32 |
+
inputs = [
|
33 |
+
gr.File(label="Video File"),
|
34 |
+
gr.File(label="Audio File"),
|
35 |
+
gr.File(label="Background Music", optional=True),
|
36 |
+
gr.Number(label="Screen Width", default=1280),
|
37 |
+
gr.Number(label="Screen Height", default=720),
|
38 |
+
gr.Textbox(label="Output File Title", default="edited_video")
|
39 |
+
]
|
40 |
+
|
41 |
+
outputs = gr.File(label="Edited Video File")
|
42 |
+
|
43 |
+
# Launch the Gradio interface
|
44 |
+
gr.Interface(fn=video_editor, inputs=inputs, outputs=outputs, title="Simple Video Editor").launch()
|