Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -34,67 +34,54 @@ def process_and_generate_video(audio_file, images):
|
|
34 |
|
35 |
try:
|
36 |
# Log the files received
|
37 |
-
debug_log.append(f"Audio file received: {audio_file
|
38 |
-
debug_log.append(f"Images received: {[img
|
39 |
-
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
debug_log.append(f"
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
debug_log.append(f"Writing video to {output_path}...")
|
87 |
-
|
88 |
-
# Write video to file
|
89 |
-
video.write_videofile(output_path, codec='libx264', audio_codec='aac')
|
90 |
-
|
91 |
-
# Check if the video file exists
|
92 |
-
if os.path.exists(output_path):
|
93 |
-
debug_log.append(f"Video generated successfully at {output_path}")
|
94 |
-
return output_path, "\n".join(debug_log) # Return the video path and debug log
|
95 |
-
else:
|
96 |
-
debug_log.append(f"Error: Video not generated at {output_path}")
|
97 |
-
return "Error: Video not generated.", "\n".join(debug_log)
|
98 |
|
99 |
except Exception as e:
|
100 |
debug_log.append(f"Error during video generation: {str(e)}")
|
@@ -108,7 +95,7 @@ def gradio_interface():
|
|
108 |
mp3_input = gr.Audio(type="filepath", label="Upload MP3") # MP3 input
|
109 |
image_input = gr.File(type="filepath", file_types=[".jpg", ".png"], label="Upload Images", file_count="multiple") # Images input
|
110 |
generate_button = gr.Button("Generate Video") # Button to generate video
|
111 |
-
|
112 |
output_video = gr.Video(label="Generated Video") # Video output display
|
113 |
output_logs = gr.Textbox(label="Debug Logs", interactive=False) # Display debug logs
|
114 |
|
|
|
34 |
|
35 |
try:
|
36 |
# Log the files received
|
37 |
+
debug_log.append(f"Audio file received: {audio_file}")
|
38 |
+
debug_log.append(f"Images received: {[img for img in images]}")
|
39 |
+
|
40 |
+
# Process audio file (use audio_file directly as it's a path)
|
41 |
+
audio = mp.AudioFileClip(audio_file) # Use file path directly
|
42 |
+
audio_duration = audio.duration
|
43 |
+
image_clips = []
|
44 |
+
image_count = len(images)
|
45 |
+
image_duration = audio_duration / image_count
|
46 |
+
|
47 |
+
debug_log.append(f"Audio duration: {audio_duration} seconds, Image count: {image_count}")
|
48 |
+
|
49 |
+
# Process each image and create video clip
|
50 |
+
for img in images:
|
51 |
+
debug_log.append(f"Processing image: {img}") # Debug print
|
52 |
+
img = Image.open(img) # Open image from file path
|
53 |
+
img = resize_image_with_aspect_ratio(img, target_size=(1280, 720))
|
54 |
+
|
55 |
+
# Create image clip with a crossfade transition effect
|
56 |
+
img_clip = mp.ImageClip(np.array(img)).set_duration(image_duration).set_fps(24)
|
57 |
+
|
58 |
+
# Add transition effect - Crossfade In
|
59 |
+
if len(image_clips) > 0: # Apply transition only after the first image
|
60 |
+
img_clip = img_clip.crossfadein(1) # 1-second fade-in transition
|
61 |
+
|
62 |
+
image_clips.append(img_clip)
|
63 |
+
|
64 |
+
debug_log.append(f"Created {len(image_clips)} image clips.")
|
65 |
+
|
66 |
+
# Concatenate image clips with transitions
|
67 |
+
video = mp.concatenate_videoclips(image_clips, method="compose")
|
68 |
+
video = video.set_audio(audio)
|
69 |
+
|
70 |
+
# Set output file path in a temporary location
|
71 |
+
output_path = '/tmp/generated_video.mp4' # Temporary path for output
|
72 |
+
|
73 |
+
debug_log.append(f"Writing video to {output_path}...")
|
74 |
+
|
75 |
+
# Write video to file
|
76 |
+
video.write_videofile(output_path, codec='libx264', audio_codec='aac')
|
77 |
+
|
78 |
+
# Check if the video file exists
|
79 |
+
if os.path.exists(output_path):
|
80 |
+
debug_log.append(f"Video generated successfully at {output_path}")
|
81 |
+
return output_path, "\n".join(debug_log) # Return the video path and debug log
|
82 |
+
else:
|
83 |
+
debug_log.append(f"Error: Video not generated at {output_path}")
|
84 |
+
return "Error: Video not generated.", "\n".join(debug_log)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
except Exception as e:
|
87 |
debug_log.append(f"Error during video generation: {str(e)}")
|
|
|
95 |
mp3_input = gr.Audio(type="filepath", label="Upload MP3") # MP3 input
|
96 |
image_input = gr.File(type="filepath", file_types=[".jpg", ".png"], label="Upload Images", file_count="multiple") # Images input
|
97 |
generate_button = gr.Button("Generate Video") # Button to generate video
|
98 |
+
|
99 |
output_video = gr.Video(label="Generated Video") # Video output display
|
100 |
output_logs = gr.Textbox(label="Debug Logs", interactive=False) # Display debug logs
|
101 |
|