File size: 1,080 Bytes
452fc48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from moviepy.editor import AudioFileClip, ImageClip

def convert_mp3_to_mp4(mp3_file, image_file):
    if mp3_file is None:
        return "Please upload an MP3 file."
    
    # Load the audio
    audio = AudioFileClip(mp3_file)

    # Use a default or uploaded image
    if image_file is None:
        image_file = "default.jpg"  # Provide a default image path

    # Create a video clip from the image and set duration to match audio
    image = ImageClip(image_file, duration=audio.duration)
    image = image.set_audio(audio)

    # Set the final output size
    image = image.resize(height=720)  # Resize for better compatibility

    # Output file
    output_path = "output.mp4"
    image.write_videofile(output_path, fps=1)

    return output_path

iface = gr.Interface(
    fn=convert_mp3_to_mp4,
    inputs=[gr.File(label="Upload MP3"), gr.File(label="Upload Image (optional)")],
    outputs=gr.File(label="Download MP4"),
    title="MP3 to MP4 Converter",
    description="Convert an MP3 file to an MP4 video with a static image."
)

iface.launch()