-
Notifications
You must be signed in to change notification settings - Fork 385
Allow nvenc encoders #596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I saw on gitter it being said that this is already supported. Turns out, my ffmpeg installation has an issue:
Closing for now as may not be a PyAV issue |
My ffmpeg command works fine using
How to select hardware encoders like |
All right, I think the problem was the PyAV build I got from Ubuntu 20.10. When I tried to use the When building PyAV from source, all those codec options are in fact available: assert "h264_nvenc" in av.codecs_available
assert "hevc_nvenc" in av.codecs_available
# all good Here is a small, timed, low-level encoder based on PyAV: def encode_frames(
frames: typing.Iterable[av.VideoFrame], codec_name: str
) -> typing.Sequence[av.Packet]:
"""Encode a sequence of video frames using given codec_name."""
frame_size = frames[0].width, frames[0].height
codec = av.Codec(codec_name, "w")
codec_ctx = codec.create()
try:
codec_ctx.pix_fmt = "yuv420p"
codec_ctx.width, codec_ctx.height = frame_size
codec_ctx.framerate = 20
packets = []
packets += codec_ctx.encode(frames[0])
t1 = time.time()
for frame in frames[1:]:
packets += codec_ctx.encode(frame)
packets += codec_ctx.encode()
t2 = time.time()
finally:
if codec_ctx.is_open:
codec_ctx.close()
fps = (len(frames) - 1) / (t2-t1)
print(f"Encoded at {fps:.1f} fps ({codec_name})")
return packets Which gives me very nice, GPU accelerated results on a desktop (100 frames, 800x600):
|
Woah that’s an awesome speed-up!! I only ever saw a 2x speed up with nvenc. May I ask what CPU / GPU you benchmarked this on? I tested with i9-10980XE and Titan RTX. Or maybe difference is in code/settings P.S. sounds like okay to close issue? |
I think it's okay to close, the encoded video frames look fine too. The speedup is probably due to my old CPU (i7-4790K) being pitted against the NVENC from the RTX 2070. Also, the timing method I used in my example above excludes the time to create and initialize the Codec by not taking the first frame into account. This might otherwise reduce the measured speedup when using short examples. |
Hi @pwuertz @tbenst, I'm trying to use the
I built ffmpeg separately and then install the Since you had done this before and not having this problem. Can you point me some where here ? Thanks Also, here is my ffmpeg build config:
|
It looks like you're trying to create an "h264_nvenc" encoder for decoding ("r"). |
Thanks a lot @pwuertz. |
FWIW, I just tested this via a pretty straightforward test setup using docker.
Then you can run the following with h264_cuvid and test vs h264:
I get about a 6x diff on a 3090: h264: Took 0:00:18.961413 |
Overview
NVENC is more than twice as fast as cpu encoding at similar or higher quality.
Existing FFmpeg API
https://ffmpeg.org/doxygen/trunk/structNvencContext.html
Expected PyAV API
PyAV should support
h264_nvenc
andnvenc_hevc
for codec optionsThe text was updated successfully, but these errors were encountered: