Skip to content

How do I send EOF to a av.filter.Graph? #886

Closed

Description

@FirefoxMetzger

Overview

I'm trying to use the FFMPEG filter palettegen to compute a color pallette for a video (and then turn it into a GIF later on). However, the filter expects to read a full video and wishes to receive an EOF error (relevant line inside FFmpeg source) before it outputs a single 256 color pallette.

When using the binary, this can be achieved as: ffmpeg -i testsrc.mp4 -vf palettegen pallette.png
(To generate a test video: ffmpeg -f lavfi -i testsrc -t 10 -pix_fmt yuv444p testsrc.mp4)

However, writing the same in pyAV the filter never produces an output, because it waits for the EOF signal. I don't know how to send it, nor have I been able to work it out from the source code. A pointer would be highly appreciated :)

Here is the code as far as I got:

import av
import av.filter

graph = av.filter.Graph()

with av.open("testsrc.mp4", "r") as container:
    video_stream = container.streams.video[0]
    
    # setup filter graph
    video_in = graph.add_buffer(template=video_stream)
    palette_gen_filter = graph.add("palettegen")
    video_out = graph.add("buffersink")
    video_in.link_to(palette_gen_filter)
    palette_gen_filter.link_to(video_out)
    graph.configure()

    for frame in container.decode(video=0):
        graph.push(frame)

    # TODO: Send EOF ... but how?

    palette_frame = graph.pull()  # this currently fails   

The above currently fails with the following exception:

Traceback (most recent call last):
  File "delete_me.py", line 22, in <module>
    palette_frame = graph.pull()  # this currently fails
  File "av/filter/graph.pyx", line 212, in av.filter.graph.Graph.pull
  File "av/filter/context.pyx", line 107, in av.filter.context.FilterContext.pull
  File "av/error.pyx", line 336, in av.error.err_check
av.error.BlockingIOError: [Errno 11] Resource temporarily unavailable

Activity

  1. FirefoxMetzger commented on Feb 17, 2022

    @FirefoxMetzger
    ContributorAuthor

    I found the solution: It is currently impossible to do so.

    The FFmpeg docs specify that sending EOF to an input buffer is done by sending NULL to av_buffersrc_add_frame_flags. However, pyAV currently calls av_buffersrc_add_frame.

    From the same docs it sounds like the latter is a wrapper around the former that sets flags explicitly, so I wonder if sending NULL to av_buffersrc_add_frame would do the same thing. Unfortunately, I can't test this with a precompiled version of pyAV, because I don't think there is a way to pass NULL into a function from python land.

    @jlaine Assuming it is as simple as that to support this, would you accept a PR for this? If so, what would be the expected timeline until the next release?


    I'm asking because I'd like to enable this feature in ImageIO. Creating GIFs appears to be quite popular and FFmpeg's palettegen is a pretty nice addition here.

    On the same note, I have some code I'd be happy to contribute that makes VideoFrame to/from ndarray conversion more efficient [and support some more formats, because it generically converts anything that is stridable]. I did read in the issues though that you guys are pretty busy with other work and that there aren't many new features happening because of that, so I've been holding off with PRs for now to not add more to your plate.

  2. jlaine commented on Feb 24, 2022

    @jlaine
    Member

    Hi @FirefoxMetzger ! I think you have a valid point here. The graph code has had some changes fairly recently so it would be good to address this.

  3. added a commit that references this issue on Feb 25, 2022
    a18c396
  4. added a commit that references this issue on Feb 25, 2022
    01b89d9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions