Update app.py
Browse files
app.py
CHANGED
|
@@ -344,61 +344,40 @@ def edit_video_raw(video_path: str) -> str:
|
|
| 344 |
import os, uuid, random
|
| 345 |
import numpy as np
|
| 346 |
from moviepy.editor import (
|
| 347 |
-
VideoFileClip, ImageClip, ColorClip,
|
| 348 |
-
CompositeVideoClip
|
| 349 |
)
|
| 350 |
|
| 351 |
-
# Ensure
|
| 352 |
if not CAPTIONS:
|
| 353 |
raise ValueError("CAPTIONS list is empty!")
|
| 354 |
|
| 355 |
# Load main video
|
| 356 |
clip = VideoFileClip(video_path)
|
|
|
|
| 357 |
|
| 358 |
-
|
| 359 |
-
CAPTION_H =
|
| 360 |
-
LAUGH_H =
|
| 361 |
MID_H = 120
|
| 362 |
-
MAIN_H = vh - (CAPTION_H + LAUGH_H + MID_H)
|
| 363 |
|
| 364 |
if clip.duration < 6:
|
| 365 |
raise ValueError("Main video must be at least 6 seconds.")
|
| 366 |
if clip.duration > 180:
|
| 367 |
clip = clip.subclip(0, 180)
|
| 368 |
|
| 369 |
-
#
|
| 370 |
-
|
| 371 |
caption_img = create_text_image_2(
|
| 372 |
-
|
| 373 |
font_size=72, align="center",
|
| 374 |
-
bg_color=(255, 255, 255), text_color=(0,0,0)
|
| 375 |
)
|
| 376 |
caption_clip = ImageClip(np.array(caption_img)) \
|
| 377 |
.set_duration(clip.duration) \
|
| 378 |
.set_position((0, 0))
|
| 379 |
|
| 380 |
-
#
|
| 381 |
-
laugh_files = ["laugh/laugh_one.mp4", "laugh/laugh_two.mp4"]
|
| 382 |
-
laugh_path = random.choice(laugh_files)
|
| 383 |
-
if not os.path.exists(laugh_path):
|
| 384 |
-
raise FileNotFoundError(f"β Laugh meme not found: {laugh_path}")
|
| 385 |
-
|
| 386 |
-
laugh_raw = VideoFileClip(laugh_path).resize(width=vw)
|
| 387 |
-
|
| 388 |
-
# Ensure the laugh meme is exactly 4 seconds long
|
| 389 |
-
if laugh_raw.duration != 4:
|
| 390 |
-
laugh_raw = laugh_raw.subclip(0, 4)
|
| 391 |
-
|
| 392 |
-
# Crop laugh meme equally from top and bottom to fit within LAUGH_H
|
| 393 |
-
laugh_crop = laugh_raw.crop(
|
| 394 |
-
y1=(laugh_raw.h - LAUGH_H) // 2,
|
| 395 |
-
y2=(laugh_raw.h + LAUGH_H) // 2
|
| 396 |
-
).set_start(0).set_position((0, CAPTION_H))
|
| 397 |
-
|
| 398 |
-
# Freeze frame for the first 2 seconds (without audio)
|
| 399 |
-
freeze_frame = laugh_crop.subclip(0, 2).without_audio()
|
| 400 |
-
|
| 401 |
-
# Mid caption
|
| 402 |
mid_text = random.choice([
|
| 403 |
"Pura 1 din laga tab ye reel mili π€£",
|
| 404 |
"Ye miss mat kr dena π",
|
|
@@ -412,35 +391,47 @@ def edit_video_raw(video_path: str) -> str:
|
|
| 412 |
)
|
| 413 |
mid_caption_clip = ImageClip(np.array(mid_img)) \
|
| 414 |
.set_duration(clip.duration) \
|
| 415 |
-
.set_start(0) \
|
| 416 |
.set_position((0, CAPTION_H + LAUGH_H))
|
| 417 |
|
| 418 |
-
#
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
|
| 425 |
-
#
|
| 426 |
overlay = ColorClip(size=(vw, MAIN_H), color=(255, 255, 255), duration=clip.duration) \
|
| 427 |
-
.set_opacity(0.
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
final = CompositeVideoClip([
|
| 436 |
-
caption_clip,
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
mid_caption_clip,
|
| 441 |
-
main_video,
|
| 442 |
-
overlay
|
| 443 |
-
], size=(vw, vh))
|
| 444 |
|
| 445 |
# Export
|
| 446 |
os.makedirs("edited", exist_ok=True)
|
|
@@ -450,15 +441,13 @@ def edit_video_raw(video_path: str) -> str:
|
|
| 450 |
codec="libx264",
|
| 451 |
audio_codec="aac",
|
| 452 |
preset="veryslow",
|
| 453 |
-
logger=None,
|
| 454 |
bitrate="15000k",
|
| 455 |
threads=4,
|
| 456 |
fps=clip.fps
|
| 457 |
)
|
| 458 |
|
| 459 |
-
clip.close(),
|
| 460 |
return out_path
|
| 461 |
-
|
| 462 |
# βββββββββββββββββββββββββββββ LOGGING
|
| 463 |
logging.basicConfig(
|
| 464 |
level=logging.INFO,
|
|
@@ -812,4 +801,4 @@ if __name__ == "__main__":
|
|
| 812 |
Thread(target=lambda: app.run(host="0.0.0.0", port=7860, debug=False, use_reloader=False)).start()
|
| 813 |
|
| 814 |
# β
Run uploader loop in background
|
| 815 |
-
Thread(target=auto_loop, daemon=True).start()
|
|
|
|
| 344 |
import os, uuid, random
|
| 345 |
import numpy as np
|
| 346 |
from moviepy.editor import (
|
| 347 |
+
VideoFileClip, ImageClip, ColorClip, CompositeVideoClip
|
|
|
|
| 348 |
)
|
| 349 |
|
| 350 |
+
# Ensure CAPTIONS exist
|
| 351 |
if not CAPTIONS:
|
| 352 |
raise ValueError("CAPTIONS list is empty!")
|
| 353 |
|
| 354 |
# Load main video
|
| 355 |
clip = VideoFileClip(video_path)
|
| 356 |
+
vw, vh = 1080, 2100
|
| 357 |
|
| 358 |
+
# Heights
|
| 359 |
+
CAPTION_H = 170
|
| 360 |
+
LAUGH_H = 500
|
| 361 |
MID_H = 120
|
| 362 |
+
MAIN_H = vh - (CAPTION_H + LAUGH_H + MID_H)
|
| 363 |
|
| 364 |
if clip.duration < 6:
|
| 365 |
raise ValueError("Main video must be at least 6 seconds.")
|
| 366 |
if clip.duration > 180:
|
| 367 |
clip = clip.subclip(0, 180)
|
| 368 |
|
| 369 |
+
# === CAPTION ===
|
| 370 |
+
caption_text = random.choice(CAPTIONS)
|
| 371 |
caption_img = create_text_image_2(
|
| 372 |
+
caption_text, vw, CAPTION_H,
|
| 373 |
font_size=72, align="center",
|
| 374 |
+
bg_color=(255, 255, 255), text_color=(0, 0, 0)
|
| 375 |
)
|
| 376 |
caption_clip = ImageClip(np.array(caption_img)) \
|
| 377 |
.set_duration(clip.duration) \
|
| 378 |
.set_position((0, 0))
|
| 379 |
|
| 380 |
+
# === MID CAPTION ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
mid_text = random.choice([
|
| 382 |
"Pura 1 din laga tab ye reel mili π€£",
|
| 383 |
"Ye miss mat kr dena π",
|
|
|
|
| 391 |
)
|
| 392 |
mid_caption_clip = ImageClip(np.array(mid_img)) \
|
| 393 |
.set_duration(clip.duration) \
|
|
|
|
| 394 |
.set_position((0, CAPTION_H + LAUGH_H))
|
| 395 |
|
| 396 |
+
# === MAIN VIDEO ===
|
| 397 |
+
clip = clip.resize(width=vw)
|
| 398 |
+
crop_y1 = 200
|
| 399 |
+
crop_y2 = crop_y1 + MAIN_H
|
| 400 |
+
main_video = clip.crop(y1=crop_y1, y2=crop_y2) \
|
| 401 |
+
.set_position((0, CAPTION_H + LAUGH_H + MID_H))
|
| 402 |
|
| 403 |
+
# Overlay (optional soft filter)
|
| 404 |
overlay = ColorClip(size=(vw, MAIN_H), color=(255, 255, 255), duration=clip.duration) \
|
| 405 |
+
.set_opacity(0.1).set_position((0, CAPTION_H + LAUGH_H + MID_H))
|
| 406 |
+
|
| 407 |
+
# === LAUGH MEME ===
|
| 408 |
+
laugh_files = ["laugh/laugh_one.mp4", "laugh/laugh_two.mp4"]
|
| 409 |
+
laugh_path = random.choice(laugh_files)
|
| 410 |
+
if not os.path.exists(laugh_path):
|
| 411 |
+
raise FileNotFoundError(f"β Laugh meme not found: {laugh_path}")
|
| 412 |
+
|
| 413 |
+
laugh_clip = VideoFileClip(laugh_path).resize(width=vw).subclip(0, 4)
|
| 414 |
+
y1 = (laugh_clip.h - LAUGH_H) // 2
|
| 415 |
+
y2 = y1 + LAUGH_H
|
| 416 |
+
laugh_crop = laugh_clip.crop(y1=y1, y2=y2).set_position((0, CAPTION_H))
|
| 417 |
+
|
| 418 |
+
# Parts:
|
| 419 |
+
laugh_part1 = laugh_crop.subclip(0, 2).set_start(0)
|
| 420 |
+
laugh_frozen = ImageClip(laugh_crop.get_frame(2)) \
|
| 421 |
+
.set_duration(clip.duration - 4).set_start(2) \
|
| 422 |
+
.set_position((0, CAPTION_H))
|
| 423 |
+
laugh_part3 = laugh_crop.subclip(2, 4).set_start(clip.duration - 2)
|
| 424 |
+
|
| 425 |
+
# === FINAL COMPOSITION ===
|
| 426 |
final = CompositeVideoClip([
|
| 427 |
+
caption_clip, # π Caption
|
| 428 |
+
laugh_part1, # π Laugh 0β2s (video)
|
| 429 |
+
laugh_frozen, # π§ Freeze mid
|
| 430 |
+
laugh_part3, # π Laugh end 2s (video)
|
| 431 |
+
mid_caption_clip, # π¬ Mid caption
|
| 432 |
+
main_video, # π₯ Main video
|
| 433 |
+
overlay # β¨ Optional light filter
|
| 434 |
+
], size=(vw, vh)).set_duration(clip.duration)
|
| 435 |
|
| 436 |
# Export
|
| 437 |
os.makedirs("edited", exist_ok=True)
|
|
|
|
| 441 |
codec="libx264",
|
| 442 |
audio_codec="aac",
|
| 443 |
preset="veryslow",
|
|
|
|
| 444 |
bitrate="15000k",
|
| 445 |
threads=4,
|
| 446 |
fps=clip.fps
|
| 447 |
)
|
| 448 |
|
| 449 |
+
clip.close(), laugh_clip.close(), final.close()
|
| 450 |
return out_path
|
|
|
|
| 451 |
# βββββββββββββββββββββββββββββ LOGGING
|
| 452 |
logging.basicConfig(
|
| 453 |
level=logging.INFO,
|
|
|
|
| 801 |
Thread(target=lambda: app.run(host="0.0.0.0", port=7860, debug=False, use_reloader=False)).start()
|
| 802 |
|
| 803 |
# β
Run uploader loop in background
|
| 804 |
+
Thread(target=auto_loop, daemon=True).start()
|