KingNish commited on
Commit
f489c9c
1 Parent(s): e8cb0da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -20
app.py CHANGED
@@ -51,26 +51,15 @@ def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=
51
  background_video = mp.VideoFileClip(bg_video)
52
  if background_video.duration < video.duration:
53
  if video_handling == "slow_down":
54
- speed_factor = video.duration / background_video.duration
55
- background_video = background_video.fx(mp.vfx.speedx, factor=speed_factor)
56
- background_frames = background_video.iter_frames(fps=fps) # Extract frames at desired FPS
57
  else: # video_handling == "loop"
58
  background_video = mp.concatenate_videoclips([background_video] * int(video.duration / background_video.duration + 1))
59
- background_frames = background_video.iter_frames(fps=fps) # Extract frames at desired FPS
60
- elif background_video.duration > video.duration:
61
- if video_handling == "slow_down":
62
- # Handle longer background video (e.g., trim or adjust logic)
63
- background_video = background_video.subclip(0, video.duration)
64
- background_frames = background_video.iter_frames(fps=fps)
65
- else: # video_handling == "loop"
66
- background_video = mp.concatenate_videoclips([background_video] * int(video.duration / background_video.duration + 1))
67
- background_frames = background_video.iter_frames(fps=fps) # Extract frames at desired FPS
68
-
69
- else:
70
- background_frames = background_video.iter_frames(fps=fps) # Extract frames at desired FPS
71
  else:
72
  background_frames = None
73
 
 
 
74
  for i, frame in enumerate(frames):
75
  pil_image = Image.fromarray(frame)
76
  if bg_type == "Color":
@@ -78,13 +67,19 @@ def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=
78
  elif bg_type == "Image":
79
  processed_image = process(pil_image, bg_image)
80
  elif bg_type == "Video":
81
- try:
82
- background_frame = next(background_frames)
 
83
  background_image = Image.fromarray(background_frame)
84
  processed_image = process(pil_image, background_image)
85
- except StopIteration:
86
- # Handle case where background video is shorter than input video
87
- processed_image = process(pil_image, "#000000") # Default to black background
 
 
 
 
 
88
  else:
89
  processed_image = pil_image # Default to original image if no background is selected
90
 
 
51
  background_video = mp.VideoFileClip(bg_video)
52
  if background_video.duration < video.duration:
53
  if video_handling == "slow_down":
54
+ background_video = background_video.fx(mp.vfx.speedx, factor=video.duration / background_video.duration)
 
 
55
  else: # video_handling == "loop"
56
  background_video = mp.concatenate_videoclips([background_video] * int(video.duration / background_video.duration + 1))
57
+ background_frames = list(background_video.iter_frames(fps=fps)) # Convert to list
 
 
 
 
 
 
 
 
 
 
 
58
  else:
59
  background_frames = None
60
 
61
+ bg_frame_index = 0 # Initialize background frame index
62
+
63
  for i, frame in enumerate(frames):
64
  pil_image = Image.fromarray(frame)
65
  if bg_type == "Color":
 
67
  elif bg_type == "Image":
68
  processed_image = process(pil_image, bg_image)
69
  elif bg_type == "Video":
70
+ if video_handling == "slow_down":
71
+ background_frame = background_frames[bg_frame_index % len(background_frames)]
72
+ bg_frame_index += 1
73
  background_image = Image.fromarray(background_frame)
74
  processed_image = process(pil_image, background_image)
75
+ else: # video_handling == "loop"
76
+ try:
77
+ background_frame = next(background_frames)
78
+ background_image = Image.fromarray(background_frame)
79
+ processed_image = process(pil_image, background_image)
80
+ except StopIteration:
81
+ # Handle case where background video is shorter than input video
82
+ processed_image = process(pil_image, "#000000") # Default to black background
83
  else:
84
  processed_image = pil_image # Default to original image if no background is selected
85