fffiloni commited on
Commit
399e705
·
1 Parent(s): 63d925a

limit video to 3 seconds

Browse files
Files changed (1) hide show
  1. app.py +13 -4
app.py CHANGED
@@ -14,19 +14,26 @@ pipe_xl.scheduler = DPMSolverMultistepScheduler.from_config(pipe_xl.scheduler.co
14
  pipe_xl.enable_model_cpu_offload()
15
  pipe_xl.to("cuda")
16
 
17
- def convert_mp4_to_frames(video_path):
18
  # Read the video file
19
  video = cv2.VideoCapture(video_path)
20
 
 
 
 
 
 
 
21
  frames = []
 
22
 
23
  # Iterate through each frame
24
  while True:
25
  # Read a frame
26
  ret, frame = video.read()
27
 
28
- # If the frame was not successfully read, then we have reached the end of the video
29
- if not ret:
30
  break
31
 
32
  # Convert BGR to RGB
@@ -35,6 +42,8 @@ def convert_mp4_to_frames(video_path):
35
  # Append the frame to the list of frames
36
  frames.append(frame)
37
 
 
 
38
  # Release the video object
39
  video.release()
40
 
@@ -45,7 +54,7 @@ def convert_mp4_to_frames(video_path):
45
 
46
  def infer(prompt, video_in):
47
 
48
- video = convert_mp4_to_frames(video_in)
49
  video_resized = [Image.fromarray(frame).resize((1024, 576)) for frame in video]
50
  video_frames = pipe_xl(prompt, video=video_resized, strength=0.6).frames
51
  video_path = export_to_video(video_frames, output_video_path="xl_result.mp4")
 
14
  pipe_xl.enable_model_cpu_offload()
15
  pipe_xl.to("cuda")
16
 
17
+ def convert_mp4_to_frames(video_path, duration=3):
18
  # Read the video file
19
  video = cv2.VideoCapture(video_path)
20
 
21
+ # Get the frames per second (fps) of the video
22
+ fps = video.get(cv2.CAP_PROP_FPS)
23
+
24
+ # Calculate the number of frames to extract
25
+ num_frames = int(fps * duration)
26
+
27
  frames = []
28
+ frame_count = 0
29
 
30
  # Iterate through each frame
31
  while True:
32
  # Read a frame
33
  ret, frame = video.read()
34
 
35
+ # If the frame was not successfully read or we have reached the desired duration, break the loop
36
+ if not ret or frame_count == num_frames:
37
  break
38
 
39
  # Convert BGR to RGB
 
42
  # Append the frame to the list of frames
43
  frames.append(frame)
44
 
45
+ frame_count += 1
46
+
47
  # Release the video object
48
  video.release()
49
 
 
54
 
55
  def infer(prompt, video_in):
56
 
57
+ video = convert_mp4_to_frames(video_in, duration=3)
58
  video_resized = [Image.fromarray(frame).resize((1024, 576)) for frame in video]
59
  video_frames = pipe_xl(prompt, video=video_resized, strength=0.6).frames
60
  video_path = export_to_video(video_frames, output_video_path="xl_result.mp4")