Jonny001 commited on
Commit
0ce7f46
·
verified ·
1 Parent(s): 08c51e0

Update roop/capturer.py

Browse files
Files changed (1) hide show
  1. roop/capturer.py +36 -5
roop/capturer.py CHANGED
@@ -1,20 +1,51 @@
1
- from typing import Any
2
  import cv2
3
 
 
 
4
 
5
- def get_video_frame(video_path: str, frame_number: int = 0) -> Any:
 
 
 
 
 
 
6
  capture = cv2.VideoCapture(video_path)
 
 
 
 
7
  frame_total = capture.get(cv2.CAP_PROP_FRAME_COUNT)
8
- capture.set(cv2.CAP_PROP_POS_FRAMES, min(frame_total, frame_number - 1))
 
 
 
 
 
9
  has_frame, frame = capture.read()
10
  capture.release()
 
11
  if has_frame:
12
  return frame
13
- return None
14
-
 
15
 
16
  def get_video_frame_total(video_path: str) -> int:
 
 
 
 
 
 
 
 
17
  capture = cv2.VideoCapture(video_path)
 
 
 
 
18
  video_frame_total = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
19
  capture.release()
20
  return video_frame_total
 
1
+ from typing import Any, Optional
2
  import cv2
3
 
4
+ def get_video_frame(video_path: str, frame_number: int = 0) -> Optional[Any]:
5
+ """Retrieve a specific frame from a video.
6
 
7
+ Args:
8
+ video_path (str): Path to the video file.
9
+ frame_number (int): Frame number to retrieve (0-based index). Default is 0.
10
+
11
+ Returns:
12
+ Optional[Any]: The requested frame as a numpy array if available, otherwise None.
13
+ """
14
  capture = cv2.VideoCapture(video_path)
15
+ if not capture.isOpened():
16
+ print(f"Error: Could not open video file {video_path}")
17
+ return None
18
+
19
  frame_total = capture.get(cv2.CAP_PROP_FRAME_COUNT)
20
+ if frame_number < 0 or frame_number >= frame_total:
21
+ print(f"Error: Frame number {frame_number} is out of range. Total frames: {frame_total}")
22
+ capture.release()
23
+ return None
24
+
25
+ capture.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
26
  has_frame, frame = capture.read()
27
  capture.release()
28
+
29
  if has_frame:
30
  return frame
31
+ else:
32
+ print(f"Error: Could not read frame {frame_number}")
33
+ return None
34
 
35
  def get_video_frame_total(video_path: str) -> int:
36
+ """Get the total number of frames in a video.
37
+
38
+ Args:
39
+ video_path (str): Path to the video file.
40
+
41
+ Returns:
42
+ int: Total number of frames in the video.
43
+ """
44
  capture = cv2.VideoCapture(video_path)
45
+ if not capture.isOpened():
46
+ print(f"Error: Could not open video file {video_path}")
47
+ return 0
48
+
49
  video_frame_total = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
50
  capture.release()
51
  return video_frame_total