| 1 |
import av |
| 2 |
|
| 3 |
from .common import TestCase, fate_suite |
| 4 |
|
| 5 |
|
| 6 |
class TestIndexEntries(TestCase): |
| 7 |
def test_index_entries_len_mp4(self) -> None: |
| 8 |
with av.open(fate_suite("h264/interlaced_crop.mp4")) as container: |
| 9 |
stream = container.streams.video[0] |
| 10 |
assert len(stream.index_entries) == stream.frames |
| 11 |
|
| 12 |
def test_index_entries_len_webm(self) -> None: |
| 13 |
with av.open( |
| 14 |
fate_suite("vp9-test-vectors/vp90-2-00-quantizer-00.webm") |
| 15 |
) as container: |
| 16 |
stream = container.streams.video[0] |
| 17 |
index_entries_len_before_demux = len(stream.index_entries) |
| 18 |
|
| 19 |
keyframes = len([p for p in container.demux(video=0) if p.is_keyframe]) |
| 20 |
assert index_entries_len_before_demux == keyframes |
| 21 |
|
| 22 |
def test_index_entries_search_timestamp_options_mp4(self) -> None: |
| 23 |
with av.open(fate_suite("h264/interlaced_crop.mp4")) as container: |
| 24 |
stream = container.streams.video[0] |
| 25 |
fi = stream.index_entries |
| 26 |
|
| 27 |
target_ts = -1 |
| 28 |
|
| 29 |
assert fi.search_timestamp(target_ts) == 0 |
| 30 |
assert fi.search_timestamp(target_ts, any_frame=True) == 1 |
| 31 |
assert fi.search_timestamp(target_ts, backward=False) == 21 |
| 32 |
assert fi.search_timestamp(target_ts, backward=False, any_frame=True) == 1 |
| 33 |
|
| 34 |
e0 = fi[0] |
| 35 |
e1 = fi[1] |
| 36 |
e21 = fi[21] |
| 37 |
assert e0.timestamp == -2 and e0.is_keyframe |
| 38 |
assert e1.timestamp == -1 and not e1.is_keyframe |
| 39 |
assert e21.timestamp == 19 and e21.is_keyframe |
| 40 |
|
| 41 |
def test_index_entries_matches_packet_mp4(self) -> None: |
| 42 |
with av.open(fate_suite("h264/interlaced_crop.mp4")) as container: |
| 43 |
stream = container.streams.video[0] |
| 44 |
for i, packet in enumerate(container.demux(video=0)): |
| 45 |
if packet.dts is not None: |
| 46 |
assert stream.index_entries[i].timestamp == packet.dts |
| 47 |
|
| 48 |
def test_index_entries_in_bounds(self) -> None: |
| 49 |
with av.open(fate_suite("h264/interlaced_crop.mp4")) as container: |
| 50 |
stream = container.streams.video[0] |
| 51 |
first = stream.index_entries[0] |
| 52 |
first_neg = stream.index_entries[-len(stream.index_entries)] |
| 53 |
last = stream.index_entries[-1] |
| 54 |
last_pos = stream.index_entries[len(stream.index_entries) - 1] |
| 55 |
assert first.timestamp == first_neg.timestamp |
| 56 |
assert last.timestamp == last_pos.timestamp |
| 57 |
|
| 58 |
def test_index_entries_out_of_bounds(self) -> None: |
| 59 |
with av.open(fate_suite("h264/interlaced_crop.mp4")) as container: |
| 60 |
stream = container.streams.video[0] |
| 61 |
with self.assertRaises(IndexError): |
| 62 |
_ = stream.index_entries[len(stream.index_entries)] |
| 63 |
|
| 64 |
with self.assertRaises(IndexError): |
| 65 |
_ = stream.index_entries[-len(stream.index_entries) - 1] |
| 66 |
|
| 67 |
def test_index_entries_slice(self) -> None: |
| 68 |
with av.open(fate_suite("h264/interlaced_crop.mp4")) as container: |
| 69 |
stream = container.streams.video[0] |
| 70 |
|
| 71 |
individual_indices = [stream.index_entries[i] for i in range(1, 5)] |
| 72 |
slice_indices = stream.index_entries[1:5] |
| 73 |
assert len(individual_indices) == len(slice_indices) == 4 |
| 74 |
assert all( |
| 75 |
[ |
| 76 |
i.timestamp == j.timestamp |
| 77 |
for i, j in zip(individual_indices, slice_indices) |
| 78 |
] |
| 79 |
) |