Skip to content

make ffmpeg's can_read more sophisticated #686

Closed

Description

@FirefoxMetzger

Details in #680 (comment)

Currently, there is a discrepancy between what our FFmpeg plugin can actually read and what it claims it can read. It claims to be able to read less than it actually does. The cause of this is that _can_read makes a decision based on the file's extension alone instead of querying the file's metadata/header (e.g., via ffprobe). This is problematic because it will reject reading from bytes/BytesIO or reading any format that we have not manually registered.

Activity

  1. Kulikovpavel commented on Nov 24, 2021

    @Kulikovpavel

    Temporary fix

    from imageio.plugins.ffmpeg import FfmpegFormat 
    FfmpegFormat.can_read = lambda x, y: True
    
  2. FirefoxMetzger commented on Nov 24, 2021

    @FirefoxMetzger
    ContributorAuthor

    Thanks for adding this workaround @Kulikovpavel.

    Note, however, that this will not work in all cases, because it will also accept resources that aren't supported by ffmpeg. For example iio.imread("some.jpg", format="ffmpeg") will be accepted by FFMPEG, but produce a cryptic error message from somewhere deep in the library.

    Additionally, it will prevent any plugins/backends with a priority lower than the priority of FFMPEG to be tested when searching for a format that can read a request with an unknown or unspecified extension. For example, FreeImage has a lower priority than FFMPEG, and - when using this workaround - it will never be tested. Instead, reading such files will potentially break somewhere deep in the library again, because FFMPEG will accept the request, but may not be able to actually read it.

  3. evamaxfield commented on Dec 17, 2021

    @evamaxfield

    Bumping this after your comment @FirefoxMetzger!

    This also affects us over at aicsimageio. Until one of our own users requires a change it is a lower priority for us but will update this issue if something comes up.

  4. FirefoxMetzger commented on Feb 7, 2022

    @FirefoxMetzger
    ContributorAuthor

    For future readers, you can use the new (v2.15.0) format_hint= kwarg in v3 to help the FFmpeg backend and read the bytes/BytesIO:

    import imageio as iio
    import io
    
    frame = iio.v3.imread(video_bytes, format_hint=".mp4")
    frame = iio.v3.imread(io.BytesIO(video_bytes), format_hint=".mp4")

    On older versions of ImageIO, a workaround is to set the extension for the stream manually. Note, however, that this is more of a hack, because there is no guarantee that the (private) _extension will be around forever:

    import imageio as iio
    
    req = iio.core.Request(video_bytes, "r")
    req._extension = ".mkv"  # your format extension here
    frame = iio.imread(req)
  5. linked a pull request that will close this issueFEAT: New Plugin for pyAV #745on Apr 5, 2022
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