Spaces:
Runtime error
Runtime error
product2204
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,66 @@
|
|
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
|
|
|
8 |
|
9 |
-
def image_to_video(image, audio):
|
10 |
"""
|
11 |
-
Converts an image and
|
|
|
12 |
|
13 |
Parameters:
|
14 |
-
- image:
|
15 |
-
- audio:
|
|
|
|
|
|
|
|
|
16 |
"""
|
17 |
-
# Create temporary
|
18 |
-
|
19 |
-
audio_path = tempfile.mktemp(suffix=".mp3")
|
20 |
-
video_path = tempfile.mktemp(suffix=".mp4")
|
21 |
|
22 |
-
# Save
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
#
|
27 |
-
|
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,
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
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(
|
45 |
-
|
46 |
-
# Release the video writer
|
47 |
video.release()
|
48 |
-
|
49 |
-
#
|
|
|
|
|
|
|
|
|
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 |
-
#
|
58 |
iface = gr.Interface(
|
59 |
fn=image_to_video,
|
60 |
-
inputs=[
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
64 |
)
|
65 |
|
66 |
-
|
67 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import cv2
|
3 |
import numpy as np
|
|
|
|
|
4 |
import tempfile
|
5 |
+
import soundfile as sf
|
6 |
+
from moviepy.editor import VideoFileClip, concatenate_videoclips,AudioFileClip
|
7 |
|
8 |
+
def image_to_video(image, audio, fps=30):
|
9 |
"""
|
10 |
+
Converts an image (NumPy array) and audio (tuple of sample rate and audio data)
|
11 |
+
into a video file.
|
12 |
|
13 |
Parameters:
|
14 |
+
- image: Input image as a NumPy array.
|
15 |
+
- audio: Input audio as a tuple of (sample rate, audio data as a NumPy array).
|
16 |
+
- fps: Frames per second of the output video.
|
17 |
+
|
18 |
+
Returns:
|
19 |
+
- Path to the generated video file.
|
20 |
"""
|
21 |
+
# Create a temporary video file
|
22 |
+
video_path = tempfile.mktemp('.mp4')
|
|
|
|
|
23 |
|
24 |
+
# Save the image to a temporary file
|
25 |
+
image_path = tempfile.mktemp('.png')
|
26 |
+
cv2.imwrite(image_path, image)
|
27 |
|
28 |
+
# Create VideoWriter to write frames
|
29 |
+
height, width, _ = image.shape
|
|
|
|
|
|
|
|
|
|
|
30 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
31 |
+
video = cv2.VideoWriter(video_path, fourcc, fps, (width, height))
|
32 |
+
|
33 |
+
# Determine the duration from the audio sample count and sample rate
|
34 |
+
sample_rate, audio_data = audio
|
35 |
+
duration_sec = len(audio_data) / sample_rate
|
36 |
+
|
|
|
|
|
37 |
# Write the image to the video file for the required number of frames
|
38 |
+
num_frames = int(duration_sec * fps)
|
39 |
for _ in range(num_frames):
|
40 |
+
video.write(cv2.imread(image_path))
|
|
|
|
|
41 |
video.release()
|
42 |
+
|
43 |
+
# Save the audio to a temporary file
|
44 |
+
audio_path = tempfile.mktemp('.wav')
|
45 |
+
sf.write(audio_path, audio_data.T, sample_rate) # Transpose if necessary for multi-channel audio
|
46 |
+
|
47 |
+
# Combine the video and audio
|
48 |
video_clip = VideoFileClip(video_path)
|
49 |
audio_clip = AudioFileClip(audio_path)
|
50 |
final_clip = video_clip.set_audio(audio_clip)
|
51 |
+
final_clip.write_videofile(video_path, codec="libx264", audio_codec="aac")
|
|
|
52 |
return video_path
|
53 |
|
54 |
+
# Gradio interface
|
55 |
iface = gr.Interface(
|
56 |
fn=image_to_video,
|
57 |
+
inputs=[
|
58 |
+
gr.Image(type="numpy"),
|
59 |
+
gr.Audio(type="numpy", label="Audio (Sample Rate, Audio Data)")
|
60 |
+
],
|
61 |
+
outputs=gr.Video("video"),
|
62 |
+
title="Image and Audio to Video Converter",
|
63 |
+
description="Upload an image and audio to convert them into a video."
|
64 |
)
|
65 |
|
66 |
+
iface.launch(debug=True,share=True)
|
|