soxogvv commited on
Commit
5dad7ea
Β·
verified Β·
1 Parent(s): e47b121

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -61
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, TextClip,
348
- CompositeVideoClip
349
  )
350
 
351
- # Ensure the CAPTIONS list is defined
352
  if not CAPTIONS:
353
  raise ValueError("CAPTIONS list is empty!")
354
 
355
  # Load main video
356
  clip = VideoFileClip(video_path)
 
357
 
358
- vw, vh = 1080, 1920 # Standard resolution for vertical video
359
- CAPTION_H = 179
360
- LAUGH_H = 340
361
  MID_H = 120
362
- MAIN_H = vh - (CAPTION_H + LAUGH_H + MID_H) # Calculate remaining height for the main video
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
- # Top caption
370
- caption = random.choice(CAPTIONS)
371
  caption_img = create_text_image_2(
372
- caption, 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
- # Laugh meme (4 seconds)
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
- # Main video
419
- main_video_y = CAPTION_H + LAUGH_H + MID_H
420
- main_video = clip.resize(width=vw).crop(
421
- y1=(clip.h - MAIN_H) // 2, # Crop equally from top and bottom to fit the height
422
- y2=(clip.h + MAIN_H) // 2
423
- ).set_start(0).set_position((0, main_video_y))
424
 
425
- # Light filter over main video to reduce copyright detection
426
  overlay = ColorClip(size=(vw, MAIN_H), color=(255, 255, 255), duration=clip.duration) \
427
- .set_opacity(0.06) \
428
- .set_position((0, main_video_y))
429
-
430
- # Final composition:
431
- # - Start laugh meme at the same time as main video
432
- # - Freeze for first 2 seconds without audio
433
- # - Freeze from 2s to (end-2) (no audio)
434
- # - Last 2 seconds of meme with audio, starting at (end-2)
 
 
 
 
 
 
 
 
 
 
 
 
 
435
  final = CompositeVideoClip([
436
- caption_clip,
437
- freeze_frame, # Freeze first 2 seconds
438
- laugh_crop.subclip(2, clip.duration-2).without_audio(), # Freeze for the rest of the meme (no audio)
439
- laugh_crop.subclip(clip.duration-2, clip.duration).with_audio(), # Last 2 seconds with audio
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(), laugh_raw.close(), final.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()