Spaces:
Sleeping
Sleeping
Update video_gen.py
Browse files- video_gen.py +24 -19
video_gen.py
CHANGED
|
@@ -22,23 +22,26 @@ import seaborn as sns
|
|
| 22 |
logging.basicConfig(level=logging.INFO)
|
| 23 |
logger = logging.getLogger(__name__)
|
| 24 |
|
|
|
|
| 25 |
def create_silent_video(images, durations, output_path, logo_path="sozo_logo2.png", font_path="lazy_dog.ttf"):
|
| 26 |
try:
|
|
|
|
| 27 |
height, width = 720, 1280
|
| 28 |
fps = 24
|
| 29 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 30 |
video = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 31 |
|
| 32 |
if not video.isOpened():
|
| 33 |
-
print("Failed to create video file.")
|
| 34 |
return None
|
| 35 |
|
| 36 |
# Load font
|
| 37 |
try:
|
| 38 |
font_size = 45
|
| 39 |
font = ImageFont.truetype(font_path, font_size)
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
-
print(f"
|
| 42 |
font = None
|
| 43 |
|
| 44 |
# Load logo
|
|
@@ -47,32 +50,33 @@ def create_silent_video(images, durations, output_path, logo_path="sozo_logo2.pn
|
|
| 47 |
try:
|
| 48 |
logo = cv2.imread(logo_path)
|
| 49 |
if logo is not None:
|
| 50 |
-
logo = cv2.resize(logo, (width, height))
|
|
|
|
| 51 |
else:
|
| 52 |
-
print(f"Warning: Failed to load logo from {logo_path}.")
|
| 53 |
except Exception as e:
|
| 54 |
-
print(f"Error loading logo: {e}")
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
for img, duration in zip(images, durations):
|
| 57 |
try:
|
| 58 |
-
|
|
|
|
| 59 |
if img.mode != "RGB":
|
| 60 |
img = img.convert("RGB")
|
| 61 |
|
| 62 |
-
# Resize image
|
| 63 |
img_resized = img.resize((width, height))
|
| 64 |
-
|
| 65 |
-
# Convert to NumPy array
|
| 66 |
frame = np.array(img_resized)
|
| 67 |
|
| 68 |
-
# Convert to OpenCV format
|
| 69 |
frame_cv = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
| 70 |
except Exception as e:
|
| 71 |
-
print(f"Invalid image detected
|
| 72 |
if logo is not None:
|
| 73 |
frame_cv = logo
|
| 74 |
else:
|
| 75 |
-
frame_cv = np.zeros((height, width, 3), dtype=np.uint8)
|
| 76 |
|
| 77 |
# Convert frame to PIL for text overlay
|
| 78 |
pil_img = Image.fromarray(cv2.cvtColor(frame_cv, cv2.COLOR_BGR2RGB))
|
|
@@ -86,8 +90,8 @@ def create_silent_video(images, durations, output_path, logo_path="sozo_logo2.pn
|
|
| 86 |
bbox = draw.textbbox((0, 0), text1, font=font)
|
| 87 |
text1_height = bbox[3] - bbox[1]
|
| 88 |
|
| 89 |
-
text_position1 = (width - 270, height - 120)
|
| 90 |
-
text_position2 = (width - 330, height - 120 + text1_height + 5)
|
| 91 |
|
| 92 |
draw.text(text_position1, text1, font=font, fill=(81, 34, 97, 255))
|
| 93 |
draw.text(text_position2, text2, font=font, fill=(81, 34, 97, 255))
|
|
@@ -95,20 +99,21 @@ def create_silent_video(images, durations, output_path, logo_path="sozo_logo2.pn
|
|
| 95 |
# Convert back to OpenCV format
|
| 96 |
frame_cv = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
|
| 97 |
|
| 98 |
-
# Write frame multiple times
|
| 99 |
for _ in range(int(duration * fps)):
|
| 100 |
video.write(frame_cv)
|
| 101 |
|
| 102 |
-
# Add full-screen logo
|
| 103 |
if logo is not None:
|
| 104 |
-
for _ in range(int(3 * fps)):
|
| 105 |
video.write(logo)
|
| 106 |
|
| 107 |
video.release()
|
|
|
|
| 108 |
return output_path
|
| 109 |
|
| 110 |
except Exception as e:
|
| 111 |
-
print(f"
|
| 112 |
return None
|
| 113 |
|
| 114 |
|
|
|
|
| 22 |
logging.basicConfig(level=logging.INFO)
|
| 23 |
logger = logging.getLogger(__name__)
|
| 24 |
|
| 25 |
+
|
| 26 |
def create_silent_video(images, durations, output_path, logo_path="sozo_logo2.png", font_path="lazy_dog.ttf"):
|
| 27 |
try:
|
| 28 |
+
print("Initializing video creation...")
|
| 29 |
height, width = 720, 1280
|
| 30 |
fps = 24
|
| 31 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 32 |
video = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 33 |
|
| 34 |
if not video.isOpened():
|
| 35 |
+
print("❌ ERROR: Failed to create video file.")
|
| 36 |
return None
|
| 37 |
|
| 38 |
# Load font
|
| 39 |
try:
|
| 40 |
font_size = 45
|
| 41 |
font = ImageFont.truetype(font_path, font_size)
|
| 42 |
+
print("✅ Font loaded successfully.")
|
| 43 |
except Exception as e:
|
| 44 |
+
print(f"⚠️ Font load error: {e}")
|
| 45 |
font = None
|
| 46 |
|
| 47 |
# Load logo
|
|
|
|
| 50 |
try:
|
| 51 |
logo = cv2.imread(logo_path)
|
| 52 |
if logo is not None:
|
| 53 |
+
logo = cv2.resize(logo, (width, height))
|
| 54 |
+
print("✅ Logo loaded successfully.")
|
| 55 |
else:
|
| 56 |
+
print(f"⚠️ Warning: Failed to load logo from {logo_path}.")
|
| 57 |
except Exception as e:
|
| 58 |
+
print(f"⚠️ Error loading logo: {e}")
|
| 59 |
+
|
| 60 |
+
print(f"Processing {len(images)} images...")
|
| 61 |
|
| 62 |
+
for idx, (img, duration) in enumerate(zip(images, durations)):
|
| 63 |
try:
|
| 64 |
+
print(f"➡️ Processing image {idx + 1}/{len(images)}...")
|
| 65 |
+
|
| 66 |
if img.mode != "RGB":
|
| 67 |
img = img.convert("RGB")
|
| 68 |
|
|
|
|
| 69 |
img_resized = img.resize((width, height))
|
|
|
|
|
|
|
| 70 |
frame = np.array(img_resized)
|
| 71 |
|
| 72 |
+
# Convert to OpenCV format
|
| 73 |
frame_cv = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
| 74 |
except Exception as e:
|
| 75 |
+
print(f"❌ ERROR: Invalid image detected: {e}")
|
| 76 |
if logo is not None:
|
| 77 |
frame_cv = logo
|
| 78 |
else:
|
| 79 |
+
frame_cv = np.zeros((height, width, 3), dtype=np.uint8)
|
| 80 |
|
| 81 |
# Convert frame to PIL for text overlay
|
| 82 |
pil_img = Image.fromarray(cv2.cvtColor(frame_cv, cv2.COLOR_BGR2RGB))
|
|
|
|
| 90 |
bbox = draw.textbbox((0, 0), text1, font=font)
|
| 91 |
text1_height = bbox[3] - bbox[1]
|
| 92 |
|
| 93 |
+
text_position1 = (width - 270, height - 120)
|
| 94 |
+
text_position2 = (width - 330, height - 120 + text1_height + 5)
|
| 95 |
|
| 96 |
draw.text(text_position1, text1, font=font, fill=(81, 34, 97, 255))
|
| 97 |
draw.text(text_position2, text2, font=font, fill=(81, 34, 97, 255))
|
|
|
|
| 99 |
# Convert back to OpenCV format
|
| 100 |
frame_cv = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
|
| 101 |
|
| 102 |
+
# Write frame multiple times
|
| 103 |
for _ in range(int(duration * fps)):
|
| 104 |
video.write(frame_cv)
|
| 105 |
|
| 106 |
+
# Add full-screen logo at the end
|
| 107 |
if logo is not None:
|
| 108 |
+
for _ in range(int(3 * fps)):
|
| 109 |
video.write(logo)
|
| 110 |
|
| 111 |
video.release()
|
| 112 |
+
print("✅ Video creation completed successfully!")
|
| 113 |
return output_path
|
| 114 |
|
| 115 |
except Exception as e:
|
| 116 |
+
print(f"❌ ERROR in video generation: {e}")
|
| 117 |
return None
|
| 118 |
|
| 119 |
|