Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from moviepy.editor import AudioFileClip, ImageClip
|
| 3 |
+
|
| 4 |
+
def convert_mp3_to_mp4(mp3_file, image_file):
|
| 5 |
+
if mp3_file is None:
|
| 6 |
+
return "Please upload an MP3 file."
|
| 7 |
+
|
| 8 |
+
# Load the audio
|
| 9 |
+
audio = AudioFileClip(mp3_file)
|
| 10 |
+
|
| 11 |
+
# Use a default or uploaded image
|
| 12 |
+
if image_file is None:
|
| 13 |
+
image_file = "default.jpg" # Provide a default image path
|
| 14 |
+
|
| 15 |
+
# Create a video clip from the image and set duration to match audio
|
| 16 |
+
image = ImageClip(image_file, duration=audio.duration)
|
| 17 |
+
image = image.set_audio(audio)
|
| 18 |
+
|
| 19 |
+
# Set the final output size
|
| 20 |
+
image = image.resize(height=720) # Resize for better compatibility
|
| 21 |
+
|
| 22 |
+
# Output file
|
| 23 |
+
output_path = "output.mp4"
|
| 24 |
+
image.write_videofile(output_path, fps=1)
|
| 25 |
+
|
| 26 |
+
return output_path
|
| 27 |
+
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=convert_mp3_to_mp4,
|
| 30 |
+
inputs=[gr.File(label="Upload MP3"), gr.File(label="Upload Image (optional)")],
|
| 31 |
+
outputs=gr.File(label="Download MP4"),
|
| 32 |
+
title="MP3 to MP4 Converter",
|
| 33 |
+
description="Convert an MP3 file to an MP4 video with a static image."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
iface.launch()
|