Skip to content

Commit 7e82740

Browse files
authored
Polish Encoder docs (#1430)
1 parent 96ef7a4 commit 7e82740

8 files changed

Lines changed: 41 additions & 30 deletions

File tree

docs/source/api_ref_encoders.rst

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ torchcodec.encoders
77
.. currentmodule:: torchcodec.encoders
88

99

10-
Multi-stream encoder
11-
--------------------
10+
Multi-stream encoder for audio and video
11+
----------------------------------------
1212

1313
For a tutorial, see: :ref:`sphx_glr_generated_examples_encoding_multi_stream_encoding.py`.
1414

@@ -25,10 +25,6 @@ For a tutorial, see: :ref:`sphx_glr_generated_examples_encoding_multi_stream_enc
2525
Single-stream encoders
2626
----------------------
2727

28-
For a video encoder tutorial, see: :ref:`sphx_glr_generated_examples_encoding_video_encoding.py`.
29-
30-
For an audio encoder tutorial, see: :ref:`sphx_glr_generated_examples_encoding_audio_encoding.py`.
31-
3228
.. autosummary::
3329
:toctree: generated/
3430
:nosignatures:

docs/source/index.rst

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,11 @@ Encoding
9898
.. grid:: 3
9999

100100
.. grid-item-card:: :octicon:`file-code;1em`
101-
Audio Encoding
102-
:link: generated_examples/encoding/audio_encoding.html
101+
Video and Audio Encoding
102+
:link: generated_examples/encoding/multi_stream_encoding.html
103103
:link-type: url
104104

105-
How encode audio samples
106-
107-
.. grid-item-card:: :octicon:`file-code;1em`
108-
Video Encoding
109-
:link: generated_examples/encoding/video_encoding.html
110-
:link-type: url
111-
112-
How to encode video frames
105+
How encode audio and video streams
113106

114107
.. toctree::
115108
:maxdepth: 1

examples/encoding/audio_encoding.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
1212
In this example, we'll learn how to encode audio samples to a file or to raw
1313
bytes using the :class:`~torchcodec.encoders.AudioEncoder` class.
14+
15+
.. note::
16+
This is a convenience class for simple, one-shot audio encoding. For
17+
multi-stream encoding (e.g. video + audio), incremental encoding, or
18+
encoding multiple audio streams, use
19+
:class:`~torchcodec.encoders.Encoder` instead. See
20+
:ref:`sphx_glr_generated_examples_encoding_multi_stream_encoding.py` for
21+
a tutorial.
1422
"""
1523

1624
# %%

examples/encoding/multi_stream_encoding.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,11 @@
172172
#
173173
# Instead of encoding to a file path, you can encode to any file-like object
174174
# (e.g. ``io.BytesIO()``) using
175-
# :meth:`~torchcodec.encoders.Encoder.open_file_like`. In this case, you must
176-
# specify the container ``format`` explicitly since there is no file extension to
177-
# infer it from.
175+
# :meth:`~torchcodec.encoders.Encoder.open_file_like`. This is useful for
176+
# example when you need to upload the encoded data directly to a remote server
177+
# or cloud storage without writing it to disk. In this case, you must specify
178+
# the container ``format`` explicitly since there is no file extension to infer
179+
# it from.
178180

179181
import io
180182

@@ -192,4 +194,7 @@
192194
encoded_bytes = buf.getvalue()
193195
print(f"Encoded to BytesIO, size: {len(encoded_bytes)} bytes")
194196

197+
# Or convert to a bytes tensor:
198+
bytes_tensor = torch.frombuffer(encoded_bytes, dtype=torch.uint8)
199+
195200
# %%

examples/encoding/video_encoding.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def play_video(encoded_bytes):
9494
)
9595
with encoder.open_file(output_path):
9696
video_stream.add_frames(frames)
97+
# More frames can be submitted by calling video_stream.add_frames
9798

9899
print(f"Encoded to {output_path}, size: {Path(output_path).stat().st_size} bytes")
99100

src/torchcodec/encoders/_audio_encoder.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
class AudioEncoder:
1010
"""A single-stream audio encoder.
1111
12-
This is a convenience class for simple, one-shot audio encoding. For
13-
multi-stream encoding (e.g. video + audio), incremental encoding, or
14-
encoding multiple audio streams, use :class:`~torchcodec.encoders.Encoder`
15-
instead. See :ref:`sphx_glr_generated_examples_encoding_multi_stream_encoding.py`
16-
for a tutorial.
12+
.. note::
13+
This is a convenience class for simple, one-shot audio encoding. For
14+
multi-stream encoding (e.g. video + audio), incremental encoding, or
15+
encoding multiple audio streams, use
16+
:class:`~torchcodec.encoders.Encoder` instead. See
17+
:ref:`sphx_glr_generated_examples_encoding_multi_stream_encoding.py` for
18+
a tutorial.
1719
1820
Args:
1921
samples (``torch.Tensor``): The samples to encode. This must be a 2D

src/torchcodec/encoders/_multi_stream_encoder.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def add_samples(self, samples: Tensor) -> None:
6666

6767

6868
class Encoder:
69-
"""A multi-stream encoder for encoding video and/or audio into a file or file-like object.
69+
"""A multi-stream encoder for encoding video and/or audio streams.
7070
7171
Unlike :class:`VideoEncoder` and :class:`AudioEncoder` which encode a
7272
single stream in one shot, ``Encoder`` supports multiple streams and
@@ -89,6 +89,8 @@ class Encoder:
8989
with encoder.open_file("output.mp4"):
9090
video_stream.add_frames(frames_tensor)
9191
audio_stream.add_samples(samples_tensor)
92+
# Add more frames by calling video_stream.add_frames again
93+
# Add more samples by calling audio_stream.add_samples again
9294
9395
To encode to a file-like object (e.g. ``io.BytesIO()``), use
9496
:meth:`open_file_like` instead:
@@ -103,6 +105,8 @@ class Encoder:
103105
with encoder.open_file_like(buf, format="mp4"):
104106
video_stream.add_frames(frames_tensor)
105107
encoded_bytes = buf.getvalue()
108+
# Optionally convert to a uint8 tensor of bytes with
109+
# bytes_tensor = torch.frombuffer(encoded_bytes, dtype=torch.uint8)
106110
"""
107111

108112
def __init__(self):

src/torchcodec/encoders/_video_encoder.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
class VideoEncoder:
1111
"""A single-stream video encoder on CPU or CUDA.
1212
13-
This is a convenience class for simple, one-shot video encoding. For
14-
multi-stream encoding (e.g. video + audio), incremental encoding, or mixing
15-
CPU and CUDA streams, use :class:`~torchcodec.encoders.Encoder` instead.
16-
See :ref:`sphx_glr_generated_examples_encoding_video_encoding.py` for a
17-
tutorial.
13+
.. note::
14+
This is a convenience class for simple, one-shot video encoding. For
15+
multi-stream encoding (e.g. video + audio), incremental encoding, or
16+
mixing CPU and CUDA streams, use :class:`~torchcodec.encoders.Encoder`
17+
instead. See
18+
:ref:`sphx_glr_generated_examples_encoding_video_encoding.py` for a
19+
tutorial.
1820
1921
Args:
2022
frames (``torch.Tensor``): The frames to encode. This must be a 4D

0 commit comments

Comments
 (0)