Download leaf/code/v1.5/v1.4/video_frame.py from COCOMatrix/LEAF-Bench: direct link, hf CLI and curl.
- Browser
- Download file 830 Bytes
-
https://huggingface.co/datasets/COCOMatrix/LEAF-Bench/resolve/main/leaf/code/v1.5/v1.4/video_frame.py
- Command line
-
hf download hf://datasets/COCOMatrix/LEAF-Bench/leaf/code/v1.5/v1.4/video_frame.py
-
curl -L -o video_frame.py https://huggingface.co/datasets/COCOMatrix/LEAF-Bench/resolve/main/leaf/code/v1.5/v1.4/video_frame.py
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}') | |