| 1 |
import av |
| 2 |
|
| 3 |
from .common import TestCase, fate_suite |
| 4 |
|
| 5 |
|
| 6 |
def timestamp_to_frame(timestamp: int, stream: av.video.stream.VideoStream) -> float: |
| 7 |
fps = stream.average_rate |
| 8 |
time_base = stream.time_base |
| 9 |
start_time = stream.start_time |
| 10 |
assert time_base is not None and start_time is not None and fps is not None |
| 11 |
return (timestamp - start_time) * float(time_base) * float(fps) |
| 12 |
|
| 13 |
|
| 14 |
class TestSeek(TestCase): |
| 15 |
def test_seek_float(self) -> None: |
| 16 |
container = av.open(fate_suite("h264/interlaced_crop.mp4")) |
| 17 |
self.assertRaises(TypeError, container.seek, 1.0) |
| 18 |
|
| 19 |
def test_seek_int64(self) -> None: |
| 20 |
# Assert that it accepts large values. |
| 21 |
# Issue 251 pointed this out. |
| 22 |
container = av.open(fate_suite("h264/interlaced_crop.mp4")) |
| 23 |
container.seek(2**32) |
| 24 |
|
| 25 |
def test_seek_start(self) -> None: |
| 26 |
container = av.open(fate_suite("h264/interlaced_crop.mp4")) |
| 27 |
|
| 28 |
# count all the packets |
| 29 |
total_packet_count = 0 |
| 30 |
for packet in container.demux(): |
| 31 |
total_packet_count += 1 |
| 32 |
|
| 33 |
# seek to beginning |
| 34 |
container.seek(-1) |
| 35 |
|
| 36 |
# count packets again |
| 37 |
seek_packet_count = 0 |
| 38 |
for packet in container.demux(): |
| 39 |
seek_packet_count += 1 |
| 40 |
|
| 41 |
assert total_packet_count == seek_packet_count |
| 42 |
|
| 43 |
def test_seek_middle(self) -> None: |
| 44 |
container = av.open(fate_suite("h264/interlaced_crop.mp4")) |
| 45 |
assert container.duration is not None |
| 46 |
|
| 47 |
# count all the packets |
| 48 |
total_packet_count = 0 |
| 49 |
for packet in container.demux(): |
| 50 |
total_packet_count += 1 |
| 51 |
|
| 52 |
# seek to middle |
| 53 |
container.seek(container.duration // 2) |
| 54 |
|
| 55 |
seek_packet_count = 0 |
| 56 |
for packet in container.demux(): |
| 57 |
seek_packet_count += 1 |
| 58 |
|
| 59 |
assert seek_packet_count < total_packet_count |
| 60 |
|
| 61 |
def test_seek_end(self) -> None: |
| 62 |
container = av.open(fate_suite("h264/interlaced_crop.mp4")) |
| 63 |
assert container.duration is not None |
| 64 |
|
| 65 |
# seek to middle |
| 66 |
container.seek(container.duration // 2) |
| 67 |
middle_packet_count = 0 |
| 68 |
|
| 69 |
for packet in container.demux(): |
| 70 |
middle_packet_count += 1 |
| 71 |
|
| 72 |
# you can't really seek to to end but you can to the last keyframe |
| 73 |
container.seek(container.duration) |
| 74 |
|
| 75 |
seek_packet_count = 0 |
| 76 |
for packet in container.demux(): |
| 77 |
seek_packet_count += 1 |
| 78 |
|
| 79 |
# there should be some packet because we're seeking to the last keyframe |
| 80 |
assert seek_packet_count > 0 |
| 81 |
assert seek_packet_count < middle_packet_count |
| 82 |
|
| 83 |
def test_decode_half(self) -> None: |
| 84 |
container = av.open(fate_suite("h264/interlaced_crop.mp4")) |
| 85 |
video_stream = container.streams.video[0] |
| 86 |
|
| 87 |
total_frame_count = 0 |
| 88 |
for frame in container.decode(video_stream): |
| 89 |
total_frame_count += 1 |
| 90 |
|
| 91 |
assert video_stream.frames == total_frame_count |
| 92 |
assert video_stream.average_rate is not None |
| 93 |
|
| 94 |
# set target frame to middle frame |
| 95 |
target_frame = total_frame_count // 2 |
| 96 |
target_timestamp = int( |
| 97 |
(target_frame * av.time_base) / video_stream.average_rate |
| 98 |
) |
| 99 |
|
| 100 |
# should seek to nearest keyframe before target_timestamp |
| 101 |
container.seek(target_timestamp) |
| 102 |
|
| 103 |
current_frame = None |
| 104 |
frame_count = 0 |
| 105 |
|
| 106 |
for frame in container.decode(video_stream): |
| 107 |
assert frame.pts is not None |
| 108 |
|
| 109 |
if current_frame is None: |
| 110 |
current_frame = timestamp_to_frame(frame.pts, video_stream) |
| 111 |
else: |
| 112 |
current_frame += 1 |
| 113 |
|
| 114 |
# start counting once we reach the target frame |
| 115 |
if current_frame is not None and current_frame >= target_frame: |
| 116 |
frame_count += 1 |
| 117 |
|
| 118 |
assert frame_count == total_frame_count - target_frame |
| 119 |
|
| 120 |
def test_stream_seek(self) -> None: |
| 121 |
container = av.open(fate_suite("h264/interlaced_crop.mp4")) |
| 122 |
video_stream = container.streams.video[0] |
| 123 |
|
| 124 |
assert video_stream.time_base is not None |
| 125 |
assert video_stream.start_time is not None |
| 126 |
assert video_stream.average_rate is not None |
| 127 |
|
| 128 |
total_frame_count = 0 |
| 129 |
for frame in container.decode(video_stream): |
| 130 |
total_frame_count += 1 |
| 131 |
|
| 132 |
target_frame = total_frame_count // 2 |
| 133 |
time_base = float(video_stream.time_base) |
| 134 |
target_sec = target_frame * 1 / float(video_stream.average_rate) |
| 135 |
|
| 136 |
target_timestamp = int(target_sec / time_base) + video_stream.start_time |
| 137 |
container.seek(target_timestamp, stream=video_stream) |
| 138 |
|
| 139 |
current_frame = None |
| 140 |
frame_count = 0 |
| 141 |
|
| 142 |
for frame in container.decode(video_stream): |
| 143 |
if current_frame is None: |
| 144 |
assert frame.pts is not None |
| 145 |
current_frame = timestamp_to_frame(frame.pts, video_stream) |
| 146 |
else: |
| 147 |
current_frame += 1 |
| 148 |
|
| 149 |
# start counting once we reach the target frame |
| 150 |
if current_frame is not None and current_frame >= target_frame: |
| 151 |
frame_count += 1 |
| 152 |
|
| 153 |
assert frame_count == total_frame_count - target_frame |