Skip to content

Commit d6b2ef0

Browse files
committed
Test duration_seconds metadata
1 parent 38aa16b commit d6b2ef0

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

test/test_metadata.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,65 @@ def test_calculate_num_frames_using_fps_and_duration(
171171
assert metadata.num_frames == expected_num_frames
172172

173173

174+
@pytest.mark.parametrize(
175+
"duration_seconds_from_header, begin_stream_seconds_from_content, end_stream_seconds_from_content, expected_duration_seconds",
176+
[(60, 5, 20, 15), (60, 1, None, 60), (None, 0, 10, 10)],
177+
)
178+
def test_duration_seconds_fallback(
179+
duration_seconds_from_header,
180+
begin_stream_seconds_from_content,
181+
end_stream_seconds_from_content,
182+
expected_duration_seconds,
183+
):
184+
"""Check that using begin_stream_seconds_from_content and end_stream_seconds_from_content to calculate `.duration_seconds`
185+
has priority. If either value is missing, duration_seconds_from_header is used.
186+
"""
187+
metadata = VideoStreamMetadata(
188+
duration_seconds_from_header=duration_seconds_from_header,
189+
bit_rate=123,
190+
num_frames_from_header=5,
191+
num_frames_from_content=10,
192+
begin_stream_seconds_from_header=0,
193+
begin_stream_seconds_from_content=begin_stream_seconds_from_content,
194+
end_stream_seconds_from_content=end_stream_seconds_from_content,
195+
codec="whatever",
196+
width=123,
197+
height=321,
198+
average_fps_from_header=5,
199+
stream_index=0,
200+
)
201+
202+
assert metadata.duration_seconds == expected_duration_seconds
203+
204+
205+
@pytest.mark.parametrize(
206+
"num_frames_from_header, average_fps_from_header, expected_duration_seconds",
207+
[(100, 10, 10), (100, None, None), (None, None, None)],
208+
)
209+
def test_calculate_duration_seconds_using_fps_and_num_frames(
210+
num_frames_from_header, average_fps_from_header, expected_duration_seconds
211+
):
212+
"""Check that duration_seconds is calculated using average_fps_from_header and num_frames_from_header
213+
if duration_seconds_from_header is missing.
214+
"""
215+
metadata = VideoStreamMetadata(
216+
duration_seconds_from_header=None, # None to test calculating duration_seconds
217+
bit_rate=123,
218+
num_frames_from_header=num_frames_from_header,
219+
num_frames_from_content=10,
220+
begin_stream_seconds_from_header=0,
221+
begin_stream_seconds_from_content=None, # None to test calculating duration_seconds
222+
end_stream_seconds_from_content=None, # None to test calculating duration_seconds
223+
codec="whatever",
224+
width=123,
225+
height=321,
226+
average_fps_from_header=average_fps_from_header,
227+
stream_index=0,
228+
)
229+
assert metadata.duration_seconds_from_header is None
230+
assert metadata.duration_seconds == expected_duration_seconds
231+
232+
174233
def test_repr():
175234
# Test for calls to print(), str(), etc. Useful to make sure we don't forget
176235
# to add additional @properties to __repr__

test/test_samplers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,9 @@ def restore_metadata():
592592
with restore_metadata():
593593
decoder.metadata.end_stream_seconds_from_content = None
594594
decoder.metadata.duration_seconds_from_header = None
595+
decoder.metadata.num_frames_from_header = (
596+
None # Set to none to prevent fallback calculation
597+
)
595598
with pytest.raises(
596599
ValueError, match="Could not infer stream end from video metadata"
597600
):

0 commit comments

Comments
 (0)