Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,12 +28,14 @@ def resize_image_with_aspect_ratio(img, target_size=(1280, 720), padding_color=(
|
|
| 28 |
|
| 29 |
return final_img
|
| 30 |
|
| 31 |
-
# Video generation function with transition
|
| 32 |
def process_and_generate_video(audio_file, images):
|
|
|
|
|
|
|
| 33 |
try:
|
| 34 |
# Log the files received
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
# Create temporary directory for images
|
| 39 |
with tempfile.TemporaryDirectory() as temp_dir:
|
|
@@ -46,7 +48,7 @@ def process_and_generate_video(audio_file, images):
|
|
| 46 |
image_paths.append(temp_image_path)
|
| 47 |
|
| 48 |
# Log image paths for debugging
|
| 49 |
-
|
| 50 |
|
| 51 |
# Process audio file
|
| 52 |
audio = mp.AudioFileClip(audio_file.name) # Use .name for filepath
|
|
@@ -55,11 +57,11 @@ def process_and_generate_video(audio_file, images):
|
|
| 55 |
image_count = len(image_paths)
|
| 56 |
image_duration = audio_duration / image_count
|
| 57 |
|
| 58 |
-
|
| 59 |
|
| 60 |
# Process each image and create video clip
|
| 61 |
for img_path in image_paths:
|
| 62 |
-
|
| 63 |
img = Image.open(img_path)
|
| 64 |
img = resize_image_with_aspect_ratio(img, target_size=(1280, 720))
|
| 65 |
|
|
@@ -72,8 +74,7 @@ def process_and_generate_video(audio_file, images):
|
|
| 72 |
|
| 73 |
image_clips.append(img_clip)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
print(f"Created {len(image_clips)} image clips.")
|
| 77 |
|
| 78 |
# Concatenate image clips with transitions
|
| 79 |
video = mp.concatenate_videoclips(image_clips, method="compose")
|
|
@@ -82,23 +83,22 @@ def process_and_generate_video(audio_file, images):
|
|
| 82 |
# Set output file path in a temporary location
|
| 83 |
output_path = '/tmp/generated_video.mp4' # Temporary path for output
|
| 84 |
|
| 85 |
-
|
| 86 |
-
print(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 |
-
|
| 94 |
-
return output_path # Return the
|
| 95 |
else:
|
| 96 |
-
|
| 97 |
-
return "Error: Video not generated."
|
| 98 |
|
| 99 |
except Exception as e:
|
| 100 |
-
|
| 101 |
-
return f"Error generating video: {str(e)}"
|
| 102 |
|
| 103 |
# Gradio interface setup
|
| 104 |
def gradio_interface():
|
|
@@ -110,10 +110,11 @@ def gradio_interface():
|
|
| 110 |
generate_button = gr.Button("Generate Video") # Button to generate video
|
| 111 |
|
| 112 |
output_video = gr.Video(label="Generated Video") # Video output display
|
|
|
|
| 113 |
|
| 114 |
-
generate_button.click(fn=process_and_generate_video, inputs=[mp3_input, image_input], outputs=output_video)
|
| 115 |
|
| 116 |
demo.launch() # Launch the Gradio interface
|
| 117 |
|
| 118 |
# Run the interface
|
| 119 |
-
gradio_interface()
|
|
|
|
| 28 |
|
| 29 |
return final_img
|
| 30 |
|
| 31 |
+
# Video generation function with transition and debug logging
|
| 32 |
def process_and_generate_video(audio_file, images):
|
| 33 |
+
debug_log = []
|
| 34 |
+
|
| 35 |
try:
|
| 36 |
# Log the files received
|
| 37 |
+
debug_log.append(f"Audio file received: {audio_file.name}")
|
| 38 |
+
debug_log.append(f"Images received: {[img.name for img in images]}")
|
| 39 |
|
| 40 |
# Create temporary directory for images
|
| 41 |
with tempfile.TemporaryDirectory() as temp_dir:
|
|
|
|
| 48 |
image_paths.append(temp_image_path)
|
| 49 |
|
| 50 |
# Log image paths for debugging
|
| 51 |
+
debug_log.append(f"Saved images in temporary directory: {image_paths}")
|
| 52 |
|
| 53 |
# Process audio file
|
| 54 |
audio = mp.AudioFileClip(audio_file.name) # Use .name for filepath
|
|
|
|
| 57 |
image_count = len(image_paths)
|
| 58 |
image_duration = audio_duration / image_count
|
| 59 |
|
| 60 |
+
debug_log.append(f"Audio duration: {audio_duration} seconds, Image count: {image_count}")
|
| 61 |
|
| 62 |
# Process each image and create video clip
|
| 63 |
for img_path in image_paths:
|
| 64 |
+
debug_log.append(f"Processing image: {img_path}") # Debug print
|
| 65 |
img = Image.open(img_path)
|
| 66 |
img = resize_image_with_aspect_ratio(img, target_size=(1280, 720))
|
| 67 |
|
|
|
|
| 74 |
|
| 75 |
image_clips.append(img_clip)
|
| 76 |
|
| 77 |
+
debug_log.append(f"Created {len(image_clips)} image clips.")
|
|
|
|
| 78 |
|
| 79 |
# Concatenate image clips with transitions
|
| 80 |
video = mp.concatenate_videoclips(image_clips, method="compose")
|
|
|
|
| 83 |
# Set output file path in a temporary location
|
| 84 |
output_path = '/tmp/generated_video.mp4' # Temporary path for output
|
| 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)}")
|
| 101 |
+
return f"Error generating video: {str(e)}", "\n".join(debug_log)
|
| 102 |
|
| 103 |
# Gradio interface setup
|
| 104 |
def gradio_interface():
|
|
|
|
| 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 |
|
| 115 |
+
generate_button.click(fn=process_and_generate_video, inputs=[mp3_input, image_input], outputs=[output_video, output_logs])
|
| 116 |
|
| 117 |
demo.launch() # Launch the Gradio interface
|
| 118 |
|
| 119 |
# Run the interface
|
| 120 |
+
gradio_interface()
|