Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,12 @@ import numpy as np
|
|
| 4 |
from PIL import Image
|
| 5 |
import os
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Resize image while maintaining aspect ratio
|
| 8 |
def resize_and_fit_image(img, target_size=(1280, 720), padding_color=(0, 0, 0)):
|
| 9 |
width, height = img.size
|
|
@@ -37,8 +43,11 @@ def apply_zoom_effect(image_clip):
|
|
| 37 |
|
| 38 |
def process_and_generate_video(audio_file, images):
|
| 39 |
try:
|
|
|
|
|
|
|
| 40 |
# Check if the file paths are strings, and convert to actual file-like objects
|
| 41 |
if isinstance(audio_file, str):
|
|
|
|
| 42 |
audio = mp.AudioFileClip(audio_file)
|
| 43 |
else:
|
| 44 |
raise ValueError("Expected a valid file path for audio.")
|
|
@@ -48,11 +57,12 @@ def process_and_generate_video(audio_file, images):
|
|
| 48 |
image_count = len(images)
|
| 49 |
image_duration = audio_duration / image_count
|
| 50 |
|
| 51 |
-
|
| 52 |
|
| 53 |
# Iterate over images, resize them, and create video clips
|
| 54 |
for img_path in images:
|
| 55 |
if isinstance(img_path, str): # Ensure img_path is a string (file path)
|
|
|
|
| 56 |
img = Image.open(img_path)
|
| 57 |
img = resize_and_fit_image(img, target_size=(1280, 720))
|
| 58 |
|
|
@@ -66,21 +76,23 @@ def process_and_generate_video(audio_file, images):
|
|
| 66 |
|
| 67 |
image_clips.append(img_clip)
|
| 68 |
|
| 69 |
-
|
| 70 |
|
| 71 |
# Concatenate image clips with audio
|
| 72 |
video = mp.concatenate_videoclips(image_clips, method="compose")
|
| 73 |
video = video.set_audio(audio)
|
| 74 |
|
| 75 |
# Set output path
|
| 76 |
-
output_path = '/
|
|
|
|
| 77 |
video.write_videofile(output_path, codec='libx264', audio_codec='aac', threads=4, fps=30, preset='ultrafast')
|
| 78 |
|
| 79 |
return output_path # Return the file path for Gradio output
|
| 80 |
|
| 81 |
except Exception as e:
|
| 82 |
-
|
| 83 |
-
|
|
|
|
| 84 |
|
| 85 |
# Gradio interface setup
|
| 86 |
def gradio_interface():
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# Utility function to log messages
|
| 8 |
+
def log_message(message):
|
| 9 |
+
with open("/tmp/video_generation_log.txt", "a") as log_file:
|
| 10 |
+
log_file.write(message + "\n")
|
| 11 |
+
print(message)
|
| 12 |
+
|
| 13 |
# Resize image while maintaining aspect ratio
|
| 14 |
def resize_and_fit_image(img, target_size=(1280, 720), padding_color=(0, 0, 0)):
|
| 15 |
width, height = img.size
|
|
|
|
| 43 |
|
| 44 |
def process_and_generate_video(audio_file, images):
|
| 45 |
try:
|
| 46 |
+
log_message("Starting video generation...")
|
| 47 |
+
|
| 48 |
# Check if the file paths are strings, and convert to actual file-like objects
|
| 49 |
if isinstance(audio_file, str):
|
| 50 |
+
log_message(f"Audio file path: {audio_file}")
|
| 51 |
audio = mp.AudioFileClip(audio_file)
|
| 52 |
else:
|
| 53 |
raise ValueError("Expected a valid file path for audio.")
|
|
|
|
| 57 |
image_count = len(images)
|
| 58 |
image_duration = audio_duration / image_count
|
| 59 |
|
| 60 |
+
log_message(f"Audio duration: {audio_duration} seconds, Image count: {image_count}")
|
| 61 |
|
| 62 |
# Iterate over images, resize them, and create video clips
|
| 63 |
for img_path in images:
|
| 64 |
if isinstance(img_path, str): # Ensure img_path is a string (file path)
|
| 65 |
+
log_message(f"Processing image: {img_path}")
|
| 66 |
img = Image.open(img_path)
|
| 67 |
img = resize_and_fit_image(img, target_size=(1280, 720))
|
| 68 |
|
|
|
|
| 76 |
|
| 77 |
image_clips.append(img_clip)
|
| 78 |
|
| 79 |
+
log_message(f"Image clips: {len(image_clips)} clips created.")
|
| 80 |
|
| 81 |
# Concatenate image clips with audio
|
| 82 |
video = mp.concatenate_videoclips(image_clips, method="compose")
|
| 83 |
video = video.set_audio(audio)
|
| 84 |
|
| 85 |
# Set output path
|
| 86 |
+
output_path = '/tmp/generated_video.mp4'
|
| 87 |
+
log_message(f"Saving video to {output_path}")
|
| 88 |
video.write_videofile(output_path, codec='libx264', audio_codec='aac', threads=4, fps=30, preset='ultrafast')
|
| 89 |
|
| 90 |
return output_path # Return the file path for Gradio output
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
+
error_message = f"Error during video generation: {str(e)}"
|
| 94 |
+
log_message(error_message)
|
| 95 |
+
return error_message
|
| 96 |
|
| 97 |
# Gradio interface setup
|
| 98 |
def gradio_interface():
|