Skip to content

FFMPEG CannotReadFrameError for 29.46 fps video #262

Closed

Description

@ekcheng

Hello,

Appreciate your advice on why imageio is having trouble reading an mp4. I suspect it has to do with the input rate of 29.46 fps. I am attempting to step through the mp4 frame by frame using the following code:

r = imageio.get_reader(file)
for i in range(r.get_length()):
  im = r.get_data(i)
  # Process image here...

Here is the print_info output for the video

ffmpeg version 3.2.2 Copyright (c) 2000-2016 the FFmpeg developers
  built with Apple LLVM version 8.0.0 (clang-800.0.42.1)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.2.2 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-frei0r --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-opencl --disable-lzma --enable-libopenjpeg --disable-decoder=jpeg2000 --extra-cflags=-I/usr/local/Cellar/openjpeg/2.1.2/include/openjpeg-2.1 --enable-nonfree --enable-vda
  libavutil      55. 34.100 / 55. 34.100
  libavcodec     57. 64.101 / 57. 64.101
  libavformat    57. 56.100 / 57. 56.100
  libavdevice    57.  1.100 / 57.  1.100
  libavfilter     6. 65.100 /  6. 65.100
  libavresample   3.  1.  0 /  3.  1.  0
  libswscale      4.  2.100 /  4.  2.100
  libswresample   2.  3.100 /  2.  3.100
  libpostproc    54.  1.100 / 54.  1.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/Users/echeng/video.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 1
    compatible_brands: isom3gp43gp5
  Duration: 00:16:05.80, start: 0.000000, bitrate: 1764 kb/s
    Stream #0:0(eng): Audio: aac (LC) (mp4a / 0x6134706D), 8000 Hz, mono, fltp, 40 kb/s (default)
    Metadata:
      handler_name    : soun
    Stream #0:1(eng): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 640x480 [SAR 1:1 DAR 4:3], 1720 kb/s, 29.46 fps, 26.58 tbr, 90k tbn, 1k tbc (default)
    Metadata:
      handler_name    : vide
Output #0, image2pipe, to 'pipe:':
  Metadata:
    major_brand     : mp42
    minor_version   : 1
    compatible_brands: isom3gp43gp5
    encoder         : Lavf57.56.100
    Stream #0:0(eng): Video: rawvideo (RGB[24] / 0x18424752), rgb24, 640x480 [SAR 1:1 DAR 4:3], q=2-31, 200 kb/s, 26.58 fps, 26.58 tbn, 26.58 tbc (default)
    Metadata:
      handler_name    : vide
      encoder         : Lavc57.64.101 rawvideo
Stream mapping:
--------------------------------------------------------------------------------

I notice the input fps is 29.46, but the Output stream fps is reported as 26.58 (same as input tbr). Is this discrepancy a problem?

imageio throws a imageio.core.format.CannotReadFrameError: Could not read frame: exception starting at frame 25676:

Traceback (most recent call last):
  File "test_video.py", line 7, in <module>
    pputils.reencode(videofile, outfile, drawframenums=True) 
  File "/Users/echeng/pputils.py", line 94, in reencode
    im = r.get_data(i)
  File "/Users/echeng/venv/lib/python2.7/site-packages/imageio/core/format.py", line 341, in get_data
    im, meta = self._get_data(index, **kwargs)
  File "/Users/echeng/venv/lib/python2.7/site-packages/imageio/plugins/ffmpeg.py", line 340, in _get_data
    result = self._read_frame()
  File "/Users/echeng/venv/lib/python2.7/site-packages/imageio/plugins/ffmpeg.py", line 543, in _read_frame
    s = self._read_frame_data()
  File "/Users/echeng/venv/lib/python2.7/site-packages/imageio/plugins/ffmpeg.py", line 529, in _read_frame_data
    raise CannotReadFrameError(fmt % (err1, err2))
imageio.core.format.CannotReadFrameError: Could not read frame:

=== stderr ===
ffmpeg version 3.2.2 Copyright (c) 2000-2016 the FFmpeg developers
  built with Apple LLVM version 8.0.0 (clang-800.0.42.1)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.2.2 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-frei0r --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-opencl --disable-lzma --enable-libopenjpeg --disable-decoder=jpeg2000 --extra-cflags=-I/usr/local/Cellar/openjpeg/2.1.2/include/openjpeg-2.1 --enable-nonfree --enable-vda
  libavutil      55. 34.100 / 55. 34.100
  libavcodec     57. 64.101 / 57. 64.101
  libavformat    57. 56.100 / 57. 56.100
  libavdevice    57.  1.100 / 57.  1.100
  libavfilter     6. 65.100 /  6. 65.100
  libavresample   3.  1.  0 /  3.  1.  0
  libswscale      4.  2.100 /  4.  2.100
  libswresample   2.  3.100 /  2.  3.100
  libpostproc    54.  1.100 / 54.  1.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/Users/echeng/video.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 1
    compatible_brands: isom3gp43gp5
  Duration: 00:16:05.80, start: 0.000000, bitrate: 1764 kb/s
    Stream #0:0(eng): Audio: aac (LC) (mp4a / 0x6134706D), 8000 Hz, mono, fltp, 40 kb/s (default)
    Metadata:
      handler_name    : soun
    Stream #0:1(eng): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 640x480 [SAR 1:1 DAR 4:3], 1720 kb/s, 29.46 fps, 26.58 tbr, 90k tbn, 1k tbc (default)
    Metadata:
      handler_name    : vide
Output #0, image2pipe, to 'pipe:':
  Metadata:
    major_brand     : mp42
    minor_version   : 1
    compatible_brands: isom3gp43gp5
    encoder         : Lavf57.56.100
    Stream #0:0(eng): Video: rawvideo (RGB[24] / 0x18424752), rgb24, 640x480 [SAR 1:1 DAR 4:3], q=2-31, 200 kb/s, 26.58 fps, 26.58 tbn, 26.58 tbc (default)
    Metadata:
      handler_name    : vide
      encoder         : Lavc57.64.101 rawvideo
Stream mapping:
... showing only last few lines ...
Past duration 0.760582 too large
Past duration 0.672997 too large
Past duration 0.822319 too large
Past duration 0.971657 too large
frame=25303 fps= 63 q=-0.0 size=22772700kB time=00:15:51.83 bitrate=195993.6kbits/s dup=18 drop=2752 speed=2.37x    
frame=25331 fps= 63 q=-0.0 size=22797900kB time=00:15:52.89 bitrate=195993.6kbits/s dup=18 drop=2755 speed=2.37x    
Past duration 1.411659 too large
Past duration 0.896416 too large
...

Due to the Past duration errors and this post, I suspect imageio is having trouble reading the video because of the odd 29.46 fps input frame rate.

Working directly with ffmpeg using this command, it fails to extract frames starting at frame 25676 as well:

ffmpeg -i video.mp4 frames/%06d.jpg

However, if I specify the input frame rate, it successfully extracts all the frames:

ffmpeg -r 29.46 -i video.mp4 frames/%06d.jpg

Is there a way to specify the input frame rate in get_reader as above so I can get past frame 25676? I've attempted to pass get_reader the ffmpeg_params keyword parameter, but it gives the following error:

In [2]: r = imageio.get_reader('video.mp4', ffmpeg_params=['-r','29.46'])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-3d2573506623> in <module>()
----> 1 r = imageio.get_reader('video.mp4', ffmpeg_params=['-r','29.46'])

/Users/echeng/anaconda2/lib/python2.7/site-packages/imageio/core/functions.pyc in get_reader(uri, format, mode, **kwargs)
    109 
    110     # Return its reader object
--> 111     return format.get_reader(request)
    112 
    113 

/Users/echeng/anaconda2/lib/python2.7/site-packages/imageio/core/format.pyc in get_reader(self, request)
    167             raise RuntimeError('Format %s cannot read in mode %r' % 
    168                                (self.name, select_mode))
--> 169         return self.Reader(self, request)
    170 
    171     def get_writer(self, request):

/Users/echeng/anaconda2/lib/python2.7/site-packages/imageio/core/format.pyc in __init__(self, format, request)
    216             self._request = request
    217             # Open the reader/writer
--> 218             self._open(**self.request.kwargs.copy())
    219 
    220         @property

TypeError: _open() got an unexpected keyword argument 'ffmpeg_params'

Apologies for the length of this issue. Thanks in advance!

Activity

  1. almarklein commented on May 31, 2017

    @almarklein
    Member

    We've found before that it is sometimes hard to predict the exact length of a video. We've included some mechanics to work around this, but I think they don't work with the way you're using the reader. Can you please try this:

    for im in imageio.get_reader(file):
      # .... process image

    In other words, use the reader as an iterator. IIRC that should make the earlier introduced workaround work. If it does not help, is there a way for me to get a (piece of a) video with which I can reproduce this?

  2. almarklein commented on May 31, 2017

    @almarklein
    Member

    If the above works, we should probably document his better and/or give an error message to help guide users who run into this issue.

  3. ekcheng commented on May 31, 2017

    @ekcheng
    Author

    Thank you. Unfortunately, using the reader as an iterator does not fix the problem. I will work on getting you an example video to reproduce.

    Regarding the ffmpeg_params option, any ideas as to why this is failing? Is this an incorrect usage of that keyword parameter?

    r = imageio.get_reader('video.mp4', ffmpeg_params=['-r','29.46'])
    

    Thanks.

  4. ekcheng commented on May 31, 2017

    @ekcheng
    Author

    UPDATE: I suspect the issue is that the video Input #0 stream has a 29.46 fps, but a 26.58 tbr. imageio looks to be assuming a 29.46 fps when reading the data, which would cause it to seek past the end of the video file.

    If I use ffmpeg directly to extract frames, it extracts N = duration * tbr frames whereas imageio assumes N = duration * fps frames. In both cases, they are using the same duration.

    Is this a plausible explanation?

  5. almarklein commented on Jun 1, 2017

    @almarklein
    Member

    Yes, that could very well be it. As it is now, ffmpeg searches for either "fps" or "tbr", and just picks the first. I guess it should prefer tbr instead.

    You could try by modifying the lines here: https://github.com/imageio/imageio/blob/master/imageio/plugins/ffmpeg.py#L503-L507
    Try adding matches.sort(key=lambda x: x[1]=='tbr', reverse=True).

    Regarding the ffmpeg_params option, any ideas as to why this is failing? Is this an incorrect usage of that keyword parameter?

    That looks good to me.

  6. ekcheng commented on Jun 1, 2017

    @ekcheng
    Author

    Thanks! It seems that preferring "tbr" in general is best, but I've also seen odd tbr values of 1k, in which case it appears that ffmpeg uses the fps value:

    ffprobe version 3.2.2 Copyright (c) 2007-2016 the FFmpeg developers
      built with Apple LLVM version 8.0.0 (clang-800.0.42.1)
      configuration: --prefix=/usr/local/Cellar/ffmpeg/3.2.2 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-frei0r --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-opencl --disable-lzma --enable-libopenjpeg --disable-decoder=jpeg2000 --extra-cflags=-I/usr/local/Cellar/openjpeg/2.1.2/include/openjpeg-2.1 --enable-nonfree --enable-vda
      libavutil      55. 34.100 / 55. 34.100
      libavcodec     57. 64.101 / 57. 64.101
      libavformat    57. 56.100 / 57. 56.100
      libavdevice    57.  1.100 / 57.  1.100
      libavfilter     6. 65.100 /  6. 65.100
      libavresample   3.  1.  0 /  3.  1.  0
      libswscale      4.  2.100 /  4.  2.100
      libswresample   2.  3.100 /  2.  3.100
      libpostproc    54.  1.100 / 54.  1.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video.mp4':
      Metadata:
        major_brand     : mp42
        minor_version   : 1
        compatible_brands: isom3gp43gp5
      Duration: 00:08:44.53, start: 0.000000, bitrate: 1830 kb/s
        Stream #0:0(eng): Audio: aac (LC) (mp4a / 0x6134706D), 8000 Hz, mono, fltp, 40 kb/s (default)
        Metadata:
          handler_name    : soun
        Stream #0:1(eng): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 640x480 [SAR 1:1 DAR 4:3], 1785 kb/s, 29.27 fps, 1k tbr, 90k tbn, 1k tbc (default)
        Metadata:
          handler_name    : vide
    

    Judging from the ffmpeg source the 1k value appears to indicate some sort of edge case in the frame rate, but I don't fully understand it.

    Do you have a good feel for the difference between "fps" and "tbr"? I've seen this post, but the explanation was lacking.

  7. almarklein commented on Jun 2, 2017

    @almarklein
    Member

    Do you have a good feel for the difference between "fps" and "tbr"?

    No, I've never found a proper explanation :( Perhaps we should collect all values, derive a minimum and maximum duration from them, report the maximum, and apply the early StopIteration trick if we fail to load a frame but we're beyond the minimum duration.

    cc @kuchi

  8. yuvallanger-tau commented on Jul 27, 2017

    @yuvallanger-tau

    Been hit by this bug as well.

    • Ubuntu 17.04
    • ffmpeg package version 7:3.2.4-1build2
    • python3.6 package version Python 3.6.1-1
    $ ffmpeg -version
    ffmpeg version 3.2.4-1build2 Copyright (c) 2000-2017 the FFmpeg developers
    built with gcc 6.3.0 (Ubuntu 6.3.0-8ubuntu1) 20170221
    configuration: --prefix=/usr --extra-version=1build2 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libebur128 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
    libavutil      55. 34.101 / 55. 34.101
    libavcodec     57. 64.101 / 57. 64.101
    libavformat    57. 56.101 / 57. 56.101
    libavdevice    57.  1.100 / 57.  1.100
    libavfilter     6. 65.100 /  6. 65.100
    libavresample   3.  1.  0 /  3.  1.  0
    libswscale      4.  2.100 /  4.  2.100
    libswresample   2.  3.100 /  2.  3.100
    libpostproc    54.  1.100 / 54.  1.100
    
    $ pip list
    alabaster (0.7.10)
    attrs (17.2.0)
    autopep8 (1.3.2)
    Babel (2.4.0)
    bleach (2.0.0)
    certifi (2017.4.17)
    chardet (3.0.4)
    click (6.7)
    cloudpickle (0.3.1)
    cvloop (0.3.4)
    cycler (0.10.0)
    dask (0.15.1)
    decorator (4.1.1)
    distributed (1.18.0)
    docopt (0.6.2)
    docutils (0.13.1)
    elpy (1.15.1)
    entrypoints (0.2.3)
    fancycompleter (0.7)
    flake8 (3.3.0)
    HeapDict (1.0.0)
    html5lib (0.999999999)
    idna (2.5)
    imageio (2.2.0)
    imagesize (0.7.1)
    importmagic (0.1.7)
    ipykernel (4.6.1)
    ipyparallel (6.0.2)
    ipython (6.1.0)
    ipython-genutils (0.2.0)
    ipywidgets (6.0.0)
    jedi (0.10.2)
    Jinja2 (2.9.6)
    jsonschema (2.6.0)
    jupyter-client (5.1.0)
    jupyter-core (4.3.0)
    MarkupSafe (1.0)
    matplotlib (2.0.2)
    mccabe (0.6.1)
    mistune (0.7.4)
    msgpack-python (0.4.8)
    mypy (0.520)
    nbconvert (5.2.1)
    nbformat (4.3.0)
    networkx (1.11)
    nose (1.3.7)
    notebook (5.0.0)
    numexpr (2.6.2)
    numpy (1.13.1)
    olefile (0.44)
    opencv-contrib-python (3.2.0.7)
    pandas (0.20.3)
    pandocfilters (1.4.1)
    pdbpp (0.9.1)
    pexpect (4.2.1)
    pickleshare (0.7.4)
    Pillow (4.2.1)
    pip (9.0.1)
    prompt-toolkit (1.0.14)
    psutil (5.2.2)
    ptyprocess (0.5.2)
    pudb (2017.1.2)
    pycodestyle (2.3.1)
    pyflakes (1.5.0)
    Pygments (2.2.0)
    pyparsing (2.2.0)
    python-dateutil (2.6.1)
    pytz (2017.2)
    PyWavelets (0.5.2)
    pyzmq (16.0.2)
    qtconsole (4.3.0)
    requests (2.18.1)
    rope (0.10.5)
    scikit-image (0.13.0)
    scipy (0.19.1)
    setuptools (36.2.0)
    simplegeneric (0.8.1)
    six (1.10.0)
    snowballstemmer (1.2.1)
    sortedcontainers (1.5.7)
    Sphinx (1.6.3)
    sphinxcontrib-websupport (1.0.1)
    tables (3.4.2)
    tblib (1.3.2)
    terminado (0.6)
    testpath (0.3.1)
    toolz (0.8.2)
    tornado (4.5.1)
    traitlets (4.3.2)
    typed-ast (1.0.4)
    urllib3 (1.21.1)
    urwid (1.3.1)
    wcwidth (0.1.7)
    webencodings (0.5.1)
    wheel (0.29.0)
    widgetsnbextension (2.0.0)
    wmctrl (0.3)
    yapf (0.16.3)
    zict (0.1.2)
    
  9. brianrusso commented on Jan 16, 2018

    @brianrusso

    I am also encountering this problem on a number of videos. Unfortunately I cannot share them, but I can confirm that the fps (At least as reported by get_meta_data()) is not variable in my case.

    Pretty much the same though.. 'cannot reshape array of size 0 into shape (480,720,3)'

    Ffmpeg 2.8.11-0ubuntu0.16.04.1

  10. almarklein commented on Jan 19, 2018

    @almarklein
    Member

    @brianrusso and @yuvallanger-tau would it be possible to share ffmpeg -i yourvideofile here? I would like to collect a few of these outputs, so we can rewrite the code that extracts the fps, and also test it in the unit tests.

  11. almarklein commented on Mar 13, 2018

    @almarklein
    Member

    In #321 I just added some fixes:

    • tbr is preferred over fps
    • added tests for the info parsing to make the parsing easier to maintain/improve while avoiding regressions (please submit the ffmpeg output for files for which it does not work correctly!)
    • added an fps param to the reader, so that the fps can be overloaded, when you want a different (fixed) fps, or when imageio fails to detect it correctly.
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