File size: 1,995 Bytes
b357832
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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()