.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated_examples/low_level/demuxer_free.py" .. LINE NUMBERS ARE GIVEN BELOW. .. rst-class:: sphx-glr-example-title .. _sphx_glr_generated_examples_low_level_demuxer_free.py: =============================================== Decoding packets that don't come from a Demuxer =============================================== .. currentmodule:: torchcodec.decoders Sometimes, you don't have a file to demux. Maybe your packets were demuxed ahead of time and stored in a database, maybe they arrive over the network, or maybe they come from a container format that TorchCodec can't read. You can still decode them: a :class:`VideoPacketDecoder` doesn't need a :class:`Demuxer`. All it needs are the :class:`VideoCodecParameters` of the stream, and :class:`Packet` objects, which you can build from the compressed data yourself. In this tutorial, we'll decode packets that an earlier job demuxed and stored, without the original file. .. GENERATED FROM PYTHON SOURCE LINES 27-28 Some boilerplate first: a test video, and the device we'll run on. .. GENERATED FROM PYTHON SOURCE LINES 28-50 .. code-block:: Python import subprocess import tempfile from pathlib import Path import torch device = "cuda" if torch.cuda.is_available() else "cpu" print(f"{device = }") temp_dir = Path(tempfile.mkdtemp()) video_path = temp_dir / "video.mp4" subprocess.run( [ "ffmpeg", "-y", "-hide_banner", "-loglevel", "error", "-f", "lavfi", "-i", "testsrc2=size=640x360:rate=30:duration=3", "-c:v", "libx264", "-pix_fmt", "yuv420p", "-g", "30", "-bf", "2", str(video_path), ], check=True, ) .. rst-class:: sphx-glr-script-out .. code-block:: none device = 'cuda' CompletedProcess(args=['ffmpeg', '-y', '-hide_banner', '-loglevel', 'error', '-f', 'lavfi', '-i', 'testsrc2=size=640x360:rate=30:duration=3', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-g', '30', '-bf', '2', '/tmp/tmp23j08iso/video.mp4'], returncode=0) .. GENERATED FROM PYTHON SOURCE LINES 52-59 Storing the packets ------------------- Let's play the earlier job: it demuxes the video and stores, for each packet, its data, its :term:`pts` and its flags, which is the data we'll later need to reconstruct such Packets. It also stores the values a decoder needs about the stream, which we'll look at below. .. GENERATED FROM PYTHON SOURCE LINES 59-75 .. code-block:: Python from torchcodec.decoders import Demuxer demuxer = Demuxer(video_path) stream = demuxer.streams[0] rows = [(bytes(packet.data.numpy()), packet.pts, packet.flags) for packet in demuxer] stream_info = { "codec": stream.codec_parameters.codec, "time_base": stream.codec_parameters.time_base, "extradata": stream.codec_parameters.extradata, "width": stream.codec_parameters.width, "height": stream.codec_parameters.height, "pixel_format": stream.codec_parameters.pixel_format, "r_frame_rate": stream.codec_parameters.r_frame_rate, } print(f"{len(rows)} packets stored") .. rst-class:: sphx-glr-script-out .. code-block:: none 90 packets stored .. GENERATED FROM PYTHON SOURCE LINES 76-78 From here on, there's no :class:`Demuxer` and no file: only ``rows`` and ``stream_info``, which play the role of our database. .. GENERATED FROM PYTHON SOURCE LINES 78-80 .. code-block:: Python del demuxer, stream .. GENERATED FROM PYTHON SOURCE LINES 81-88 Codec parameters ---------------- A decoder needs to know about the stream it decodes: its codec, the unit of the packets' timestamps, and some codec-specific values. That's what :class:`VideoCodecParameters` holds. Here, we build them from the stored values: .. GENERATED FROM PYTHON SOURCE LINES 88-101 .. code-block:: Python from torchcodec.decoders import VideoCodecParameters codec_parameters = VideoCodecParameters( stream_info["codec"], time_base=stream_info["time_base"], extradata=stream_info["extradata"], width=stream_info["width"], height=stream_info["height"], pixel_format=stream_info["pixel_format"], r_frame_rate=stream_info["r_frame_rate"], ) print(codec_parameters) .. rst-class:: sphx-glr-script-out .. code-block:: none VideoCodecParameters(codec='h264', time_base=Fraction(1, 15360), extradata=<47 bytes>, width=640, height=360, pixel_format='yuv420p', r_frame_rate=Fraction(30, 1)) .. GENERATED FROM PYTHON SOURCE LINES 102-116 Which fields you need depends on the codec. For H.264 in MP4, the decoder needs the ``extradata``: it's where MP4 stores the codec's setup data, outside of the packets. To decode on CUDA, ``width``, ``height`` and ``pixel_format`` are needed too, and ``r_frame_rate`` gives the frames their durations. To find out what to set for your own packets, print the codec parameters a :class:`Demuxer` reports for a similar file: ``print(Demuxer(path).streams[0].codec_parameters)``. .. note:: If the packets are demuxed with TorchCodec in the first place, as above, it's simpler and safer to store the whole :class:`VideoCodecParameters` object, e.g. pickled, rather than a few of its fields: it holds everything the decoder may need, including fields you wouldn't think of. .. GENERATED FROM PYTHON SOURCE LINES 118-126 Decoding the packets -------------------- :meth:`Packet.from_bytes` builds a packet from its data, and :meth:`Packet.from_tensor` does the same from a 1D uint8 tensor. Only the data is required, but the pts is what gives the decoded frames their timestamps. **Unlike in most TorchCodec APIs, timestamps are integers**, in units of the stream's time base. The rest is the usual decoding loop: .. GENERATED FROM PYTHON SOURCE LINES 126-139 .. code-block:: Python from torchcodec.decoders import ColorConverter, Packet, VideoPacketDecoder packets = [Packet.from_bytes(data, pts=pts, flags=flags) for data, pts, flags in rows] packet_decoder = VideoPacketDecoder(codec_parameters, device=device) color_converter = ColorConverter(device=device) frames = [] for packet in packets: frames += [color_converter.convert(f) for f in packet_decoder.decode(packet)] frames += [color_converter.convert(f) for f in packet_decoder.drain()] print(f"{len(frames)} frames, {frames[0].data.shape = }, {frames[0].data.device = }") .. rst-class:: sphx-glr-script-out .. code-block:: none 90 frames, frames[0].data.shape = torch.Size([3, 360, 640]), frames[0].data.device = device(type='cuda', index=0) .. GENERATED FROM PYTHON SOURCE LINES 140-147 Matching frames to packets -------------------------- The frames don't come out of the decoder in the order the packets went in. But :attr:`RawFrame.pts` is the pts of the packet the frame was decoded from, and both are integers: a frame can be matched exactly to its packet, e.g. to look up what you stored along with it. .. GENERATED FROM PYTHON SOURCE LINES 149-152 The codec parameters and the packets can also be pickled, e.g. to demux in one process and decode in another: see the last section of :ref:`sphx_glr_generated_examples_low_level_pipelines.py`. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.282 seconds) .. _sphx_glr_download_generated_examples_low_level_demuxer_free.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demuxer_free.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demuxer_free.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demuxer_free.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_