Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,11 +2,14 @@ import gradio as gr
|
|
2 |
import moviepy.editor as mp
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
|
|
|
|
5 |
|
6 |
-
# Resize image
|
7 |
def resize_image_with_aspect_ratio(img, target_size=(1280, 720), padding_color=(0, 0, 0)):
|
8 |
width, height = img.size
|
9 |
target_width, target_height = target_size
|
|
|
10 |
aspect_ratio = width / height
|
11 |
target_aspect_ratio = target_width / target_height
|
12 |
|
@@ -27,34 +30,55 @@ def resize_image_with_aspect_ratio(img, target_size=(1280, 720), padding_color=(
|
|
27 |
|
28 |
# Video generation function with transition
|
29 |
def process_and_generate_video(audio_file, images):
|
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 |
# Gradio interface setup
|
60 |
def gradio_interface():
|
@@ -62,14 +86,14 @@ def gradio_interface():
|
|
62 |
with gr.Row():
|
63 |
with gr.Column():
|
64 |
mp3_input = gr.Audio(type="filepath", label="Upload MP3") # MP3 input
|
65 |
-
image_input = gr.File(type="
|
66 |
generate_button = gr.Button("Generate Video") # Button to generate video
|
67 |
-
|
68 |
output_video = gr.Video(label="Generated Video") # Video output display
|
69 |
|
70 |
generate_button.click(fn=process_and_generate_video, inputs=[mp3_input, image_input], outputs=output_video)
|
71 |
|
72 |
demo.launch() # Launch the Gradio interface
|
73 |
|
74 |
-
# Run the
|
75 |
gradio_interface()
|
|
|
2 |
import moviepy.editor as mp
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
5 |
+
import tempfile
|
6 |
+
import shutil
|
7 |
|
8 |
+
# Resize image while maintaining aspect ratio
|
9 |
def resize_image_with_aspect_ratio(img, target_size=(1280, 720), padding_color=(0, 0, 0)):
|
10 |
width, height = img.size
|
11 |
target_width, target_height = target_size
|
12 |
+
|
13 |
aspect_ratio = width / height
|
14 |
target_aspect_ratio = target_width / target_height
|
15 |
|
|
|
30 |
|
31 |
# Video generation function with transition
|
32 |
def process_and_generate_video(audio_file, images):
|
33 |
+
try:
|
34 |
+
# Create temporary folder to store image paths
|
35 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
36 |
+
# Save images to temporary directory
|
37 |
+
image_paths = []
|
38 |
+
for img in images:
|
39 |
+
temp_image_path = os.path.join(temp_dir, os.path.basename(img.name))
|
40 |
+
with open(temp_image_path, 'wb') as f:
|
41 |
+
f.write(img.read())
|
42 |
+
image_paths.append(temp_image_path)
|
43 |
+
|
44 |
+
# Process audio file
|
45 |
+
audio = mp.AudioFileClip(audio_file.name) # Use .name for filepath
|
46 |
+
audio_duration = audio.duration
|
47 |
+
image_clips = []
|
48 |
+
image_count = len(image_paths)
|
49 |
+
image_duration = audio_duration / image_count
|
50 |
+
|
51 |
+
print(f"Audio duration: {audio_duration} seconds, Image count: {image_count}")
|
52 |
+
|
53 |
+
# Process each image and create video clip
|
54 |
+
for img_path in image_paths:
|
55 |
+
img = Image.open(img_path)
|
56 |
+
img = resize_image_with_aspect_ratio(img, target_size=(1280, 720))
|
57 |
+
|
58 |
+
# Create image clip with a crossfade transition effect
|
59 |
+
img_clip = mp.ImageClip(np.array(img)).set_duration(image_duration).set_fps(24)
|
60 |
+
|
61 |
+
# Add transition effect - Crossfade In
|
62 |
+
if len(image_clips) > 0: # Apply transition only after the first image
|
63 |
+
img_clip = img_clip.crossfadein(1) # 1-second fade-in transition
|
64 |
+
|
65 |
+
image_clips.append(img_clip)
|
66 |
+
|
67 |
+
# Concatenate image clips with transitions
|
68 |
+
video = mp.concatenate_videoclips(image_clips, method="compose")
|
69 |
+
video = video.set_audio(audio)
|
70 |
+
|
71 |
+
# Set output file path
|
72 |
+
output_path = '/tmp/generated_video.mp4' # Temporary path for output
|
73 |
+
|
74 |
+
# Write video to file
|
75 |
+
video.write_videofile(output_path, codec='libx264', audio_codec='aac')
|
76 |
+
|
77 |
+
return output_path # Return the file path for Gradio output
|
78 |
+
|
79 |
+
except Exception as e:
|
80 |
+
print(f"Error during video generation: {str(e)}")
|
81 |
+
return f"Error generating video: {str(e)}"
|
82 |
|
83 |
# Gradio interface setup
|
84 |
def gradio_interface():
|
|
|
86 |
with gr.Row():
|
87 |
with gr.Column():
|
88 |
mp3_input = gr.Audio(type="filepath", label="Upload MP3") # MP3 input
|
89 |
+
image_input = gr.File(type="file", file_types=[".jpg", ".png"], label="Upload Images", file_count="multiple") # Images input
|
90 |
generate_button = gr.Button("Generate Video") # Button to generate video
|
91 |
+
|
92 |
output_video = gr.Video(label="Generated Video") # Video output display
|
93 |
|
94 |
generate_button.click(fn=process_and_generate_video, inputs=[mp3_input, image_input], outputs=output_video)
|
95 |
|
96 |
demo.launch() # Launch the Gradio interface
|
97 |
|
98 |
+
# Run the interface
|
99 |
gradio_interface()
|