File size: 1,489 Bytes
4f1a3bc
 
 
 
 
 
 
 
 
 
 
 
 
7e3db37
4f1a3bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e3db37
076934c
 
 
4f1a3bc
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import gradio as gr
from moviepy.editor import VideoFileClip, AudioFileClip, CompositeVideoClip

def video_editor(video, audio, music, width, height, output_title):
    # Load video and audio clips
    video_clip = VideoFileClip(video.name)
    audio_clip = AudioFileClip(audio.name)
    
    # Resize video using width and height
    if width and height:
        video_clip = video_clip.resize(newsize=(width, height))
    
    # Add background music if provided
    if music is not None:
        music_clip = AudioFileClip(music.name)
        final_audio = CompositeVideoClip([audio_clip, music_clip.set_duration(video_clip.duration)])
    else:
        final_audio = audio_clip
    
    # Combine video with the final audio
    final_clip = video_clip.set_audio(final_audio)
    
    # Define output file path
    output_file = f"{output_title}.mp4"
    
    # Write the final video file
    final_clip.write_videofile(output_file, codec="libx264")
    
    return output_file

# Define the Gradio interface
inputs = [
    gr.File(label="Video File"),
    gr.File(label="Audio File"),
    gr.File(label="Background Music (Optional)"),  # No optional keyword, just handle it in the function
    gr.Number(label="Screen Width"),
    gr.Number(label="Screen Height"),
    gr.Textbox(label="Output File Title")
]

outputs = gr.File(label="Edited Video File")

# Launch the Gradio interface
gr.Interface(fn=video_editor, inputs=inputs, outputs=outputs, title="Simple Video Editor").launch()