Skip to content

RuntimeError when ffmpeg filter reduces # of frames #341

Closed

Description

@edhalter

System: Linux, ffmpeg 3.3.6, imageio 2.3.0

This is a follow-up to Issue #294. That user was trying to pull I-frames from a video file, and ffmpeg.py needed some fiddling to put the ffmpeg parameters in the right order. That was solved in Pull #321.

I'm trying to do the exact same thing -- actually found that issue page using google. But although the parameters now get passed in ok, a new error soon crops up. To reproduce:

import imageio
output_params = ['-vf', "select='eq(pict_type\,I)'"]
path = '/path/to/video.mp4'
reader = imageio.get_reader(path, format='FFMPEG', print_info=False, output_params=output_params)
print('num frames:', len(reader))
for i, img in enumerate(reader):
print(i)

For, say, a video w/ 100 frames and only 1 I-frame, the output is:

num frames: 100
0

... and then the exception:

Traceback (most recent call last):
  File "/home/e/.local/lib64/python3.6/site-packages/imageio/plugins/ffmpeg.py", line 565, in _read_frame_data
    (len(s), framesize))
RuntimeError: Frame is 0 bytes, but expected 230400.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/e/.local/lib64/python3.6/site-packages/imageio/core/format.py", line 397, in iter_data
    im, meta = self._get_data(i)
  File "/home/e/.local/lib64/python3.6/site-packages/imageio/plugins/ffmpeg.py", line 396, in _get_data
    result, is_new = self._read_frame()
  File "/home/e/.local/lib64/python3.6/site-packages/imageio/plugins/ffmpeg.py", line 585, in _read_frame
    s, is_new = self._read_frame_data()
  File "/home/e/.local/lib64/python3.6/site-packages/imageio/plugins/ffmpeg.py", line 571, in _read_frame_data
    raise CannotReadFrameError(fmt % (self._pos, err1, err2))
imageio.core.format.CannotReadFrameError: Could not read frame 0:
Frame is 0 bytes, but expected 230400.

The problem is that ffmpeg reports the total number of frames in the video, and imageio then expects to read that number of frames. But the pipe from ffmpeg contains fewer frames, based on the filter parameters that were used. imageio then chokes when it iterates past the last frame returned, expecting more.

Best option might be to just stop iterating silently, maybe print a warning. Don't think there's a clever way to get the "real" number of returned frames. ffmpeg's just passing raw video using the image2pipe "muxer", so there are no headers in the data w/ frame numbers, timestamps, etc. You can get frame info out of band, using e.g.

ffprobe -select_streams v -show_frames -show_entries frame=pkt_pts_time,pict_type -of csv=print_section=0 /path/to/video.mp4

But that's slow and requires parsing the video file again.

Activity

  1. almarklein commented on May 14, 2018

    @almarklein
    Member

    I'm not sure about silencing the error, but in a case like this, where you expect it to happen, you could probably do something like:

    reader = imageio.get_reader( ...):
    while True:
        try:
            im = reader.get_next_data()
        except imageio.core.CannotReadFrameError:
            break
        else:
            # ... process im
  2. almarklein commented on Feb 5, 2019

    @almarklein
    Member

    This will be fixed with #425, the new ffmpeg plugin makes no assumptions about the number of frames, because it is either too unreliable or expensive to calculate.

    BTW: in the above example, the CannotReadFrameError should be IndexErrorafter #425 gets merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions