Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from pydub import AudioSegment
|
| 5 |
+
from moviepy.editor import VideoFileClip, AudioFileClip
|
| 6 |
+
import tempfile
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
def image_to_video(image, audio):
|
| 10 |
+
"""
|
| 11 |
+
Converts an image and an audio file into a video.
|
| 12 |
+
|
| 13 |
+
Parameters:
|
| 14 |
+
- image: Uploaded image file.
|
| 15 |
+
- audio: Uploaded audio file.
|
| 16 |
+
"""
|
| 17 |
+
# Create temporary paths for the files
|
| 18 |
+
image_path = tempfile.mktemp(suffix=".png")
|
| 19 |
+
audio_path = tempfile.mktemp(suffix=".mp3")
|
| 20 |
+
video_path = tempfile.mktemp(suffix=".mp4")
|
| 21 |
+
|
| 22 |
+
# Save uploaded files to temporary paths
|
| 23 |
+
image.save(image_path)
|
| 24 |
+
audio.save(audio_path)
|
| 25 |
+
|
| 26 |
+
# Load the image
|
| 27 |
+
img = cv2.imread(image_path)
|
| 28 |
+
|
| 29 |
+
# Get image dimensions
|
| 30 |
+
height, width, layers = img.shape
|
| 31 |
+
|
| 32 |
+
# Define the codec and create VideoWriter object
|
| 33 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 34 |
+
video = cv2.VideoWriter(video_path, fourcc, 30, (width, height))
|
| 35 |
+
|
| 36 |
+
audio_segment = AudioSegment.from_file(audio_path)
|
| 37 |
+
duration_sec = len(audio_segment) / 1000.0 # Convert duration from ms to seconds
|
| 38 |
+
|
| 39 |
+
# Calculate the number of frames needed
|
| 40 |
+
num_frames = int(duration_sec * 30) # Assuming 30 fps
|
| 41 |
+
|
| 42 |
+
# Write the image to the video file for the required number of frames
|
| 43 |
+
for _ in range(num_frames):
|
| 44 |
+
video.write(img)
|
| 45 |
+
|
| 46 |
+
# Release the video writer
|
| 47 |
+
video.release()
|
| 48 |
+
|
| 49 |
+
# Add audio to the video
|
| 50 |
+
video_clip = VideoFileClip(video_path)
|
| 51 |
+
audio_clip = AudioFileClip(audio_path)
|
| 52 |
+
final_clip = video_clip.set_audio(audio_clip)
|
| 53 |
+
final_clip.write_videofile(video_path, codec="libx264", audio_codec="aac")
|
| 54 |
+
|
| 55 |
+
return video_path
|
| 56 |
+
|
| 57 |
+
# Define the Gradio interface
|
| 58 |
+
iface = gr.Interface(
|
| 59 |
+
fn=image_to_video,
|
| 60 |
+
inputs=[gr.inputs.Image(label="Upload Image"), gr.inputs.Audio(label="Upload Audio")],
|
| 61 |
+
outputs=gr.outputs.Video(label="Output Video"),
|
| 62 |
+
title="Image to Video Converter",
|
| 63 |
+
description="Converts an image and an audio file into a video."
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Launch the app
|
| 67 |
+
iface.launch()
|