Skip to content

Commit

Permalink
BUG: seeking to last frame caused EoF in pyav (#855)
Browse files Browse the repository at this point in the history
  • Loading branch information
FirefoxMetzger committed Aug 6, 2022
1 parent 6eedab8 commit 7ff7f02
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
14 changes: 8 additions & 6 deletions imageio/plugins/pyav.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"""

from math import ceil
from typing import Any, Dict, List, Optional, Tuple, Union
from typing import Any, Dict, List, Optional, Tuple, Union, Generator
from fractions import Fraction

import av
Expand Down Expand Up @@ -412,8 +412,8 @@ def read(
ffmpeg_filter = self._build_filter(filter_sequence, filter_graph)
ffmpeg_filter.send(None) # init

self._seek(index, constant_framerate=constant_framerate)
desired_frame = next(self._container.decode(video=0))
decoder = self._seek(index, constant_framerate=constant_framerate)
desired_frame = next(decoder)

return self._unpack_frame(ffmpeg_filter.send(desired_frame), format=format)

Expand Down Expand Up @@ -936,8 +936,8 @@ def metadata(
metadata.update(self._video_stream.metadata)
return metadata

self._seek(index, constant_framerate=constant_framerate)
desired_frame = next(self._container.decode(video=0))
decoder = self._seek(index, constant_framerate=constant_framerate)
desired_frame = next(decoder)

# useful flags defined on the frame
metadata.update(
Expand All @@ -953,7 +953,7 @@ def metadata(

return metadata

def _seek(self, index, *, constant_framerate: bool = True) -> None:
def _seek(self, index, *, constant_framerate: bool = True) -> Generator:
"""Seeks to the frame at the given index."""

# this may be made faster for formats that have some kind
Expand All @@ -976,6 +976,8 @@ def _seek(self, index, *, constant_framerate: bool = True) -> None:
for _ in range(frames_to_yield):
next(frame_generator)

return frame_generator

def close(self) -> None:
"""Close the Video."""

Expand Down
5 changes: 5 additions & 0 deletions tests/test_pyav.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,8 @@ def test_uri_reading(test_images):
actual = iio.imread(uri, plugin="pyav", index=250)

np.allclose(actual, expected)


def test_seek_last(test_images):
frame = iio.imread(test_images / "cockatoo.mp4", plugin="pyav", index=279)
assert frame.shape == (720, 1280, 3)

0 comments on commit 7ff7f02

Please sign in to comment.