.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/nvdec_tutorial.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_nvdec_tutorial.py: Accelerated video decoding with NVDEC ===================================== .. _nvdec_tutorial: **Author**: `Moto Hira `__ This tutorial shows how to use NVIDIA’s hardware video decoder (NVDEC) with TorchAudio, and how it improves the performance of video decoding. .. GENERATED FROM PYTHON SOURCE LINES 14-23 .. note:: This tutorial requires FFmpeg libraries compiled with HW acceleration enabled. Please refer to :ref:`Enabling GPU video decoder/encoder ` for how to build FFmpeg with HW acceleration. .. GENERATED FROM PYTHON SOURCE LINES 24-31 .. code-block:: default import torch import torchaudio print(torch.__version__) print(torchaudio.__version__) .. rst-class:: sphx-glr-script-out .. code-block:: none 2.3.0 2.3.0 .. GENERATED FROM PYTHON SOURCE LINES 33-39 .. code-block:: default import os import time import matplotlib.pyplot as plt from torchaudio.io import StreamReader .. GENERATED FROM PYTHON SOURCE LINES 40-46 Check the prerequisites ----------------------- First, we check that TorchAudio correctly detects FFmpeg libraries that support HW decoder/encoder. .. GENERATED FROM PYTHON SOURCE LINES 47-50 .. code-block:: default from torchaudio.utils import ffmpeg_utils .. GENERATED FROM PYTHON SOURCE LINES 52-57 .. code-block:: default print("FFmpeg Library versions:") for k, ver in ffmpeg_utils.get_versions().items(): print(f" {k}:\t{'.'.join(str(v) for v in ver)}") .. rst-class:: sphx-glr-script-out .. code-block:: none FFmpeg Library versions: libavcodec: 60.3.100 libavdevice: 60.1.100 libavfilter: 9.3.100 libavformat: 60.3.100 libavutil: 58.2.100 .. GENERATED FROM PYTHON SOURCE LINES 59-65 .. code-block:: default print("Available NVDEC Decoders:") for k in ffmpeg_utils.get_video_decoders().keys(): if "cuvid" in k: print(f" - {k}") .. rst-class:: sphx-glr-script-out .. code-block:: none Available NVDEC Decoders: - av1_cuvid - h264_cuvid - hevc_cuvid - mjpeg_cuvid - mpeg1_cuvid - mpeg2_cuvid - mpeg4_cuvid - vc1_cuvid - vp8_cuvid - vp9_cuvid .. GENERATED FROM PYTHON SOURCE LINES 67-71 .. code-block:: default print("Avaialbe GPU:") print(torch.cuda.get_device_properties(0)) .. rst-class:: sphx-glr-script-out .. code-block:: none Avaialbe GPU: _CudaDeviceProperties(name='NVIDIA A10G', major=8, minor=6, total_memory=22515MB, multi_processor_count=80) .. GENERATED FROM PYTHON SOURCE LINES 72-84 We will use the following video which has the following properties; - Codec: H.264 - Resolution: 960x540 - FPS: 29.97 - Pixel format: YUV420P .. raw:: html .. GENERATED FROM PYTHON SOURCE LINES 88-93 .. code-block:: default src = torchaudio.utils.download_asset( "tutorial-assets/stream-api/NASAs_Most_Scientifically_Complex_Space_Observatory_Requires_Precision-MP4_small.mp4" ) .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0.00/31.8M [00:00 .. GENERATED FROM PYTHON SOURCE LINES 328-350 Comparing resizing methods -------------------------- Unlike software scaling, NVDEC does not provide an option to choose the scaling algorithm. In ML applicatoins, it is often necessary to construct a preprocessing pipeline with a similar numerical property. So here we compare the result of hardware resizing with software resizing of different algorithms. We will use the following video, which contains the test pattern generated using the following command. .. code:: ffmpeg -y -f lavfi -t 12.05 -i mptestsrc -movflags +faststart mptestsrc.mp4 .. raw:: html .. GENERATED FROM PYTHON SOURCE LINES 354-358 .. code-block:: default test_src = torchaudio.utils.download_asset("tutorial-assets/mptestsrc.mp4") .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0.00/232k [00:00` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: nvdec_tutorial.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_