2
2
import os
3
3
4
4
import pandas as pd
5
+ import ffmpeg
6
+
7
+ from typing import Tuple
8
+
9
+
10
+ def video_info (video_path : str ) -> Tuple [int , int , int ]:
11
+ """
12
+ Uses the ffmpeg.probe function to retrieve information about a video file.
13
+
14
+ :param video_path: Path to a valid video file
15
+ :return: A 3-tuple with integers for (width, height, number_of_frames)
16
+ """
17
+
18
+ #
19
+ # Fetch video info
20
+ info = ffmpeg .probe (video_path )
21
+ # Get the list of all video streams
22
+ video_streams = [stream for stream in info ['streams' ] if stream ['codec_type' ] == 'video' ]
23
+ if len (video_streams ) == 0 :
24
+ raise BaseException ("No video streams found in file '{}'" .format (video_path ))
25
+
26
+ # retrieve the first stream of type 'video'
27
+ info_video = video_streams [0 ]
28
+
29
+ video_w = info_video ['width' ]
30
+ video_h = info_video ['height' ]
31
+ n_frames = int (info_video ['nb_frames' ])
32
+
33
+ return video_w , video_h , n_frames
34
+
5
35
6
36
def extract_frames (video_file : str , timestamps : pd .DataFrame , output_dir : str ):
37
+
7
38
# Open the video file
8
39
cap = cv2 .VideoCapture (video_file )
9
40
assert cap .isOpened () == True
10
- video_name = os .path .splitext (video_file )[0 ]
41
+ video_name , _ = os .path .splitext (video_file )
42
+
11
43
# Loop over each timestamp in the CSV file
12
44
for timestamp in timestamps :
13
45
# Extract the frame using OpenCV
@@ -20,4 +52,3 @@ def extract_frames(video_file: str, timestamps: pd.DataFrame, output_dir: str):
20
52
21
53
# Release the video file
22
54
cap .release ()
23
-
0 commit comments