Allow configuring hardware encoder sw_format#2332
Merged
WyattBlue merged 1 commit intoJun 29, 2026
Merged
Conversation
CarlosRDomin
commented
Jun 29, 2026
| stream.pix_fmt = hw_format | ||
| stream.codec_context.sw_format = "yuv420p" | ||
|
|
||
| assert stream.codec_context.sw_format.name == "yuv420p" |
Contributor
Author
There was a problem hiding this comment.
Note that this used to return None (hw_frames_ctx was NULL), now we allow getting (and setting) the value
CarlosRDomin
commented
Jun 29, 2026
| container.mux(packet) | ||
|
|
||
| assert stream.codec_context.pix_fmt == hw_format | ||
| assert stream.codec_context.sw_format.name == "yuv420p" |
Contributor
Author
There was a problem hiding this comment.
Note that this used to return "nv12" (and would make the encoder use nv12 as the pixel format, with no way to control the value), now the sw_format property is respected
bbb01aa to
72650ff
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently hardware encode setup derives
AVHWFramesContext.sw_formatfromAVCodecContext.pix_fmt. If the user setspix_fmt="cuda", PyAV treats that as the hardware format and falls back tonv12for software format, with no public way to request a different software format such asyuv420p.This PR adds a setter for
VideoCodecContext.sw_formatand makes_setup_encode_hwframes()preferAVCodecContext.sw_pix_fmtwhen it is set, while preserving the existingstream.pix_fmt = "nv12"path and the currentnv12default.The new
test_hardware_encode_honors_sw_formattest verifies thatpix_fmt="cuda"plussw_format="yuv420p"opens the encoder with a CUDA hardware frames context whose software format remainsyuv420pinstead ofnv12(before this PR). Also tested on Mac withvideotoolboxhwaccel with similar results.