Spaces:
Runtime error
Runtime error
import gradio as gr | |
import cv2 | |
import numpy as np | |
from pydub import AudioSegment | |
from moviepy.editor import VideoFileClip, AudioFileClip | |
import tempfile | |
import os | |
def image_to_video(image, audio): | |
""" | |
Converts an image and an audio file into a video. | |
Parameters: | |
- image: Uploaded image file. | |
- audio: Uploaded audio file. | |
""" | |
# Create temporary paths for the files | |
image_path = tempfile.mktemp(suffix=".png") | |
audio_path = tempfile.mktemp(suffix=".mp3") | |
video_path = tempfile.mktemp(suffix=".mp4") | |
# Save uploaded files to temporary paths | |
image.save(image_path) | |
audio.save(audio_path) | |
# Load the image | |
img = cv2.imread(image_path) | |
# Get image dimensions | |
height, width, layers = img.shape | |
# Define the codec and create VideoWriter object | |
fourcc = cv2.VideoWriter_fourcc(*'mp4v') | |
video = cv2.VideoWriter(video_path, fourcc, 30, (width, height)) | |
audio_segment = AudioSegment.from_file(audio_path) | |
duration_sec = len(audio_segment) / 1000.0 # Convert duration from ms to seconds | |
# Calculate the number of frames needed | |
num_frames = int(duration_sec * 30) # Assuming 30 fps | |
# Write the image to the video file for the required number of frames | |
for _ in range(num_frames): | |
video.write(img) | |
# Release the video writer | |
video.release() | |
# Add audio to the video | |
video_clip = VideoFileClip(video_path) | |
audio_clip = AudioFileClip(audio_path) | |
final_clip = video_clip.set_audio(audio_clip) | |
final_clip.write_videofile(video_path, codec="libx264", audio_codec="aac") | |
return video_path | |
# Define the Gradio interface | |
iface = gr.Interface( | |
fn=image_to_video, | |
inputs=[gr.inputs.Image(label="Upload Image"), gr.inputs.Audio(label="Upload Audio")], | |
outputs=gr.outputs.Video(label="Output Video"), | |
title="Image to Video Converter", | |
description="Converts an image and an audio file into a video." | |
) | |
# Launch the app | |
iface.launch() | |