Skip to content

Commit 042f4b8

Browse files
committed
Added video function to count frames and a test for it.
1 parent 5d588eb commit 042f4b8

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

PostProcessing/test_functions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
from PostProcessVideos import scan_session_dir
1313

14+
from video import video_info
15+
1416

1517
RECSYNCH_SESSION_DIR_VAR = "RECSYNCH_SESSION_DIR"
1618

@@ -97,3 +99,16 @@ def test_df_trimming(session_data):
9799
# Get the last element of the first column
98100
ts_end = df[0].iloc[-1]
99101
assert ts_end >= max_common
102+
103+
104+
@pytest.mark.parametrize("client_data", session_data_list())
105+
def test_video_sources(client_data):
106+
_, _, video_filepath = client_data
107+
108+
video_path = Path(video_filepath)
109+
110+
assert video_path.exists()
111+
assert video_path.is_file()
112+
113+
w, h, n_frames = video_info(video_path)
114+
assert n_frames >= 2

PostProcessing/video.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,44 @@
22
import os
33

44
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+
535

636
def extract_frames(video_file: str, timestamps: pd.DataFrame, output_dir: str):
37+
738
# Open the video file
839
cap = cv2.VideoCapture(video_file)
940
assert cap.isOpened() == True
10-
video_name = os.path.splitext(video_file)[0]
41+
video_name, _ = os.path.splitext(video_file)
42+
1143
# Loop over each timestamp in the CSV file
1244
for timestamp in timestamps:
1345
# Extract the frame using OpenCV
@@ -20,4 +52,3 @@ def extract_frames(video_file: str, timestamps: pd.DataFrame, output_dir: str):
2052

2153
# Release the video file
2254
cap.release()
23-

0 commit comments

Comments
 (0)