LEAF-Bench / leaf /code /v1.5 /v1.4 /video_frame.py
PeterXie666's picture
Add files using upload-large-folder tool
9f84006 verified
Raw History Blame Contribute Delete
830 Bytes
"""Seek to an exact 20 Hz raw-video frame, validating its timestamp."""
from fractions import Fraction
import av
def frame_at(path, index, fps=20):
with av.open(str(path)) as container:
stream=container.streams.video[0]
assert stream.average_rate==fps, (path,stream.average_rate)
target=Fraction(stream.start_time or 0)+Fraction(index,fps)/stream.time_base
assert target.denominator==1,(path,index,target)
target=int(target)
container.seek(target,stream=stream,backward=True,any_frame=False)
for frame in container.decode(stream):
if frame.pts==target:
return frame.to_ndarray(format='rgb24')
if frame.pts is not None and frame.pts>target:
break
raise AssertionError(f'Missing exact frame {index}: {path}')