You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
importavimportav.filtergraph=av.filter.Graph()
withav.open("testsrc.mp4", "r") ascontainer:
video_stream=container.streams.video[0]
# setup filter graphvideo_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()
forframeincontainer.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
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.
Activity
FirefoxMetzger commentedon Feb 17, 2022
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_framewould 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 passNULLinto 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.
jlaine commentedon Feb 24, 2022
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.
[filters] allow flushing by sending `None` (fixes: PyAV-Org#886)
[filters] allow flushing by sending `None` (fixes: #886)