Is there any example how to use nvenc/nvdec in python?

I’m reading the document and how to activate cuda and hardware acceleration on agx orin:
I’m using ffmpeg as backend using this library: GitHub - abhiTronix/deffcode: A cross-platform High-performance FFmpeg based Real-time Video Frames Decoder in Pure Python 🎞️⚡
simple code:

# import the necessary packages
from deffcode import FFdecoder
import cv2

# define suitable FFmpeg parameter
ffparams = {
    "-vcodec": "h264_nvmpi",  # use H.264 CUVID Video-decoder
    "-enforce_cv_patch": True # enable OpenCV patch for YUV(YUV420p) frames
}

# initialize and formulate the decoder with `foo.mp4` source
decoder = FFdecoder(
    "demo.mp4",
    frame_format="yuv420p",  # use YUV420p frame pixel format
    verbose=True, # enable verbose output
    **ffparams # apply various params and custom filters
).formulate()

# grab the YUV420p frame from the decoder
for frame in decoder.generateFrame():

    # check if frame is None
    if frame is None:
        break

    # convert it to `BGR` pixel format,
    # since imshow() method only accepts `BGR` frames
    frame = cv2.cvtColor(frame, cv2.COLOR_YUV2BGR_I420)

    # {do something with the BGR frame here}

    # Show output window
    cv2.imshow("Output", frame)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

# close output window
cv2.destroyAllWindows()

# terminate the decoder
decoder.terminate()

But it doesn’t work. With the example of Jetson Linux API Reference: 02_video_dec_cuda (CUDA processing with decode) | NVIDIA Docs it is working.
Screenshot 2024-04-28 at 22.40.52

Hi,
We have a ffmpeg pachage enabling hardware decoding. Please refer to

Jetson AGX Orin FAQ
Q: Is hardware acceleration enabled in ffmpeg?

If you need hardware decoding and encoding in python, we would suggest use gstreamer. There are nvv4l2h264enc, nvv4l2h265enc, nvv4l2decoder and you ca construct gstreamer pipelines like the samples:
Nvv4l2decoder sometimes fails to negotiate with downstream after several pipeline re-launches - #16 by DaneLLL
Camera liveview cannot show up again after repeated open/close the liveview for several times - #19 by DaneLLL
Nvv4l2decoder plugin is not shutting down cleanly and leaking memory - #8 by DaneLLL

1 Like

one question @DaneLLL ,
when I do sudo apt-get install ffmpeg, it installs generic ffmpeg or from apt jetson source with cuda and encoders enabled?

if I try to build ffmpeg:

In file included from libavcodec/nvv4l2_dec.c:44:
/usr/src/jetson_multimedia_api/include/libv4l2.h:30:5: warning: "HAVE_VISIBILITY" is not defined, evaluates to 0 [-Wundef]
   30 | #if HAVE_VISIBILITY
      |     ^~~~~~~~~~~~~~~
CC	libavcodec/opus.o
CC	libavcodec/options.o
In file included from libavcodec/nvv4l2_dec.c:56:
libavcodec/v4l2_nv_extensions.h:195:8: error: redefinition of ‘struct v4l2_ctrl_h264_sps’
  195 | struct v4l2_ctrl_h264_sps {
      |        ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/linux/videodev2.h:65,
                 from libavcodec/nvv4l2_dec.c:45:
/usr/include/linux/v4l2-controls.h:1306:8: note: originally defined here
 1306 | struct v4l2_ctrl_h264_sps {
      |        ^~~~~~~~~~~~~~~~~~
In file included from libavcodec/nvv4l2_dec.c:56:
libavcodec/v4l2_nv_extensions.h:224:8: error: redefinition of ‘struct v4l2_ctrl_h264_pps’
  224 | struct v4l2_ctrl_h264_pps {
      |        ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/linux/videodev2.h:65,
                 from libavcodec/nvv4l2_dec.c:45:
/usr/include/linux/v4l2-controls.h:1362:8: note: originally defined here
 1362 | struct v4l2_ctrl_h264_pps {
      |        ^~~~~~~~~~~~~~~~~~
In file included from libavcodec/nvv4l2_dec.c:56:
libavcodec/v4l2_nv_extensions.h:238:8: error: redefinition of ‘struct v4l2_ctrl_h264_scaling_matrix’
  238 | struct v4l2_ctrl_h264_scaling_matrix {
      |        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/linux/videodev2.h:65,
                 from libavcodec/nvv4l2_dec.c:45:
/usr/include/linux/v4l2-controls.h:1394:8: note: originally defined here
 1394 | struct v4l2_ctrl_h264_scaling_matrix {
      |        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from libavcodec/nvv4l2_dec.c:56:
libavcodec/v4l2_nv_extensions.h:243:8: error: redefinition of ‘struct v4l2_h264_weight_factors’
  243 | struct v4l2_h264_weight_factors {
      |        ^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/linux/videodev2.h:65,
                 from libavcodec/nvv4l2_dec.c:45:
/usr/include/linux/v4l2-controls.h:1399:8: note: originally defined here
 1399 | struct v4l2_h264_weight_factors {
      |        ^~~~~~~~~~~~~~~~~~~~~~~~
In file included from libavcodec/nvv4l2_dec.c:56:
libavcodec/v4l2_nv_extensions.h:304: warning: "V4L2_H264_DPB_ENTRY_FLAG_ACTIVE" redefined
  304 | #define V4L2_H264_DPB_ENTRY_FLAG_ACTIVE     0x01
      | 
In file included from /usr/include/linux/videodev2.h:65,
                 from libavcodec/nvv4l2_dec.c:45:
/usr/include/linux/v4l2-controls.h:1521: note: this is the location of the previous definition
 1521 | #define V4L2_H264_DPB_ENTRY_FLAG_ACTIVE         0x02
      | 
In file included from libavcodec/nvv4l2_dec.c:56:
libavcodec/v4l2_nv_extensions.h:305: warning: "V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM" redefined
  305 | #define V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM  0x02
      | 
In file included from /usr/include/linux/videodev2.h:65,
                 from libavcodec/nvv4l2_dec.c:45:
/usr/include/linux/v4l2-controls.h:1522: note: this is the location of the previous definition
 1522 | #define V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM      0x04
      | 
In file included from libavcodec/nvv4l2_dec.c:56:
libavcodec/v4l2_nv_extensions.h:306:8: error: redefinition of ‘struct v4l2_h264_dpb_entry’
  306 | struct v4l2_h264_dpb_entry {
      |        ^~~~~~~~~~~~~~~~~~~
In file included from /usr/include/linux/videodev2.h:65,
                 from libavcodec/nvv4l2_dec.c:45:
/usr/include/linux/v4l2-controls.h:1542:8: note: originally defined here
 1542 | struct v4l2_h264_dpb_entry {
      |        ^~~~~~~~~~~~~~~~~~~
libavcodec/nvv4l2_dec.c: In function ‘query_set_capture’:
libavcodec/nvv4l2_dec.c:589:5: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
  589 |     NvBufSurface *nvbuf_surf = 0;
      |     ^~~~~~~~~~~~
libavcodec/nvv4l2_dec.c:698:13: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
  698 |             NvBufSurface *nvbuf_surf_dest = 0;
      |             ^~~~~~~~~~~~
libavcodec/nvv4l2_dec.c: In function ‘capture_thread’:
libavcodec/nvv4l2_dec.c:873:13: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
  873 |             NvBufSurface *decoded_nvbuf_surf = 0;
      |             ^~~~~~~~~~~~
libavcodec/nvv4l2_dec.c:885:13: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
  885 |             NvBufSurface *nvbuf_surf_parm = 0;
      |             ^~~~~~~~~~~~
libavcodec/nvv4l2_dec.c:917:13: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
  917 |             NvBufSurface *nvbuf_surf = NULL;
      |             ^~~~~~~~~~~~
make: *** [ffbuild/common.mak:59: libavcodec/nvv4l2_dec.o] Error 1
make: *** Waiting for unfinished jobs....
libavcodec/options.c: In function ‘codec_child_class_next’:
libavcodec/options.c:61:5: warning: ‘av_codec_next’ is deprecated [-Wdeprecated-declarations]
   61 |     while (prev && (c = av_codec_next(c)))
      |     ^~~~~
In file included from libavcodec/options.c:27:
libavcodec/avcodec.h:4118:10: note: declared here
 4118 | AVCodec *av_codec_next(const AVCodec *c);
      |          ^~~~~~~~~~~~~
libavcodec/options.c:66:5: warning: ‘av_codec_next’ is deprecated [-Wdeprecated-declarations]
   66 |     while (c = av_codec_next(c))
      |     ^~~~~
In file included from libavcodec/options.c:27:
libavcodec/avcodec.h:4118:10: note: declared here
 4118 | AVCodec *av_codec_next(const AVCodec *c);
      |          ^~~~~~~~~~~~~

(py311) johnny@johnny-jetson:~/Projects/ffmpeg-4.2.7$ dpkg-query -L nvidia-l4t-multimedia
/.
/usr
/usr/bin
/usr/bin/nvaudio_opus_app
/usr/lib
/usr/lib/aarch64-linux-gnu
/usr/lib/aarch64-linux-gnu/libv4l
/usr/lib/aarch64-linux-gnu/libv4l/plugins
/usr/lib/aarch64-linux-gnu/libv4l/plugins/nv
/usr/lib/aarch64-linux-gnu/nvidia
/usr/lib/aarch64-linux-gnu/nvidia/libnvbufsurftransform.so.1.0.0
/usr/lib/aarch64-linux-gnu/nvidia/libnvcuvidv4l2.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvdecode2eglimage.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvdsbufferpool.so.1.0.0
/usr/lib/aarch64-linux-gnu/nvidia/libnveventlib.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvexif.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvid_mapper.so.1.0.0
/usr/lib/aarch64-linux-gnu/nvidia/libnvjpeg.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmedia.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmedia2d.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmedia_2d.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmedia_dla.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmedia_eglstream.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmedia_ide_parser.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmedia_ide_sci.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmedia_iep_sci.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmedia_ijpd_sci.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmedia_ijpe_sci.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmedia_iofa_sci.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmedia_tensor.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmedialdc.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmm.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmm_contentpipe.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmm_parser.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmm_utils.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmmlite.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmmlite_image.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmmlite_utils.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvmmlite_video.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvofsdk.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvoggopus.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvomx.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvomxilclient.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvosd.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvparser.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvtracebuf.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvtvmr.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvtvmr_2d.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvv4l2.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvv4lconvert.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvvideo.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvvideoencode_ppe.so
/usr/lib/aarch64-linux-gnu/nvidia/libtegrav4l2.so
/usr/lib/aarch64-linux-gnu/nvidia/libv4l2_nvcuvidvideocodec.so
/usr/lib/aarch64-linux-gnu/nvidia/libv4l2_nvvideocodec.so
/usr/share
/usr/share/doc
/usr/share/doc/nvidia-l4t-multimedia
/usr/share/doc/nvidia-l4t-multimedia/changelog.Debian.gz
/usr/share/doc/nvidia-l4t-multimedia/copyright
/usr/share/doc/nvidia-tegra
/usr/share/doc/nvidia-tegra/COPYING.opus
/usr/share/doc/nvidia-tegra/LICENSE.libnveventlib
/usr/share/doc/nvidia-tegra/LICENSE.libnvjpeg
/usr/share/doc/nvidia-tegra/LICENSE.libnvtracebuf
/usr/share/doc/nvidia-tegra/LICENSE.libnvv4l2.gz
/usr/share/doc/nvidia-tegra/LICENSE.libnvv4lconvert.gz
/usr/share/doc/nvidia-tegra/SOURCE.libnvv4l2
/usr/share/doc/nvidia-tegra/SOURCE.libnvv4lconvert
/usr/lib/aarch64-linux-gnu/libv4l/plugins/nv/libv4l2_nvcuvidvideocodec.so
/usr/lib/aarch64-linux-gnu/libv4l/plugins/nv/libv4l2_nvvideocodec.so
/usr/lib/aarch64-linux-gnu/libv4l2.so.0.0.999999
/usr/lib/aarch64-linux-gnu/libv4lconvert.so.0.0.999999
/usr/lib/aarch64-linux-gnu/nvidia/libnvbufsurftransform.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvdsbufferpool.so
/usr/lib/aarch64-linux-gnu/nvidia/libnvid_mapper.so
/usr/lib/aarch64-linux-gnu/nvidia/libv4l2.so.0
/usr/lib/aarch64-linux-gnu/nvidia/libv4lconvert.so.0

Hi,
Please use latest Jetpack 5.1.3. We have not seen the error. If you use previous version, please upgrade and give it a try.

In the ffmpeg package, hardware decoding is implemented through jetson_multimedia_api interface. It uses hardware decoding engine NVDEC.

@DaneLLL I’m using 6.0DP with AGX Orin 64gb.
I got it with gstreamer, but please check with ffmpeg because it doesn’t compile for me.
and apt’s ffmpeg is compiled without jetson’s hw accelerators.

Hi,
Jetpack 6.0GA is published. Please upgrade and give it a try.

Same problem if I do sudo make install

@DaneLLL
./configure is working
make -j12 is working
but when I do sudo make install I have these problem

Hi,
Please try to install the prebuilt package:
Accelerated Decode with ffmpeg — NVIDIA Jetson Linux Developer Guide 1 documentation

$ cat /etc/apt/sources.list.d/nvidia-l4t-apt-source.list | grep ffmpeg
deb https://repo.download.nvidia.com/jetson/ffmpeg r36.3 main
$ sudo apt update
$ sudo apt install ffmpeg
$ ffmpeg -decoders | grep nvv4l2

Please run above steps to install the prebuilt package with hardware decoding.

it is working! including with deffcode! awesome

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.