.. 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 `__ .. warning:: Starting with version 2.8, we are refactoring TorchAudio to transition it into a maintenance phase. As a result: - The APIs described in this tutorial are deprecated in 2.8 and will be removed in 2.9. - The decoding and encoding capabilities of PyTorch for both audio and video are being consolidated into TorchCodec. Please see https://github.com/pytorch/audio/issues/3902 for more information. 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 24-33 .. 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 34-41 .. code-block:: default import torch import torchaudio print(torch.__version__) print(torchaudio.__version__) .. rst-class:: sphx-glr-script-out .. code-block:: none 2.8.0+cu126 2.8.0 .. GENERATED FROM PYTHON SOURCE LINES 43-49 .. code-block:: default import os import time import matplotlib.pyplot as plt from torchaudio.io import StreamReader .. GENERATED FROM PYTHON SOURCE LINES 50-56 Check the prerequisites ----------------------- First, we check that TorchAudio correctly detects FFmpeg libraries that support HW decoder/encoder. .. GENERATED FROM PYTHON SOURCE LINES 57-60 .. code-block:: default from torchaudio.utils import ffmpeg_utils .. GENERATED FROM PYTHON SOURCE LINES 62-67 .. 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: /pytorch/audio/examples/tutorials/nvdec_tutorial.py:64: UserWarning: torio.utils.ffmpeg_utils.get_versions has been deprecated. This deprecation is part of a large refactoring effort to transition TorchAudio into a maintenance phase. Please see https://github.com/pytorch/audio/issues/3902 for more information. It will be removed from the 2.9 release. for k, ver in ffmpeg_utils.get_versions().items(): 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 69-75 .. 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: /pytorch/audio/examples/tutorials/nvdec_tutorial.py:71: UserWarning: torio.utils.ffmpeg_utils.get_video_decoders has been deprecated. This deprecation is part of a large refactoring effort to transition TorchAudio into a maintenance phase. Please see https://github.com/pytorch/audio/issues/3902 for more information. It will be removed from the 2.9 release. for k in ffmpeg_utils.get_video_decoders().keys(): - 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 77-81 .. 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=22598MB, multi_processor_count=80, uuid=566db26d-0405-a011-4198-2699df443f87, pci_bus_id=0, pci_device_id=30, pci_domain_id=0, L2_cache_size=6MB) .. GENERATED FROM PYTHON SOURCE LINES 82-94 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 98-103 .. 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 /pytorch/audio/examples/tutorials/nvdec_tutorial.py:99: UserWarning: torchaudio.utils.download.download_asset has been deprecated. This deprecation is part of a large refactoring effort to transition TorchAudio into a maintenance phase. Please see https://github.com/pytorch/audio/issues/3902 for more information. It will be removed from the 2.9 release. src = torchaudio.utils.download_asset( 0%| | 0.00/31.8M [00:00 .. GENERATED FROM PYTHON SOURCE LINES 338-360 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 364-368 .. code-block:: default test_src = torchaudio.utils.download_asset("tutorial-assets/mptestsrc.mp4") .. rst-class:: sphx-glr-script-out .. code-block:: none /pytorch/audio/examples/tutorials/nvdec_tutorial.py:365: UserWarning: torchaudio.utils.download.download_asset has been deprecated. This deprecation is part of a large refactoring effort to transition TorchAudio into a maintenance phase. Please see https://github.com/pytorch/audio/issues/3902 for more information. It will be removed from the 2.9 release. test_src = torchaudio.utils.download_asset("tutorial-assets/mptestsrc.mp4") 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 `_