-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[torchcodec] Add a simple example showing how to decode the first fra…
…me of a video (#51) Summary: Add a basic example of how to decode a frame from a video. Pull Request resolved: #51 Differential Revision: D59022932
- Loading branch information
1 parent
d4c35f1
commit 229ab89
Showing
2 changed files
with
38 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
================================================== | ||
Basic Example to use TorchCodec to decode a video. | ||
================================================== | ||
""" | ||
|
||
# A simple example showing how to decode the first few frames of a video. | ||
# using the :class:`~torchcodec.decoders.SimpleVideoDecoder` class. | ||
# %% | ||
import inspect | ||
import os | ||
|
||
from torchcodec.decoders import SimpleVideoDecoder | ||
|
||
my_path = os.path.abspath(inspect.getfile(inspect.currentframe())) | ||
video_file_path = os.path.dirname(my_path) + "/../test/resources/nasa_13013.mp4" | ||
simple_decoder = SimpleVideoDecoder(video_file_path) | ||
# Since `simple_decoder` is an iterable, you can get the total frame count for | ||
# the best video stream by calling len(). | ||
num_frames = len(simple_decoder) | ||
print(f"{video_file_path=} has {num_frames} frames") | ||
|
||
# You can get the decoded frame by using the subscript operator. | ||
first_frame = simple_decoder[0] | ||
print(f"decoded frame has type {type(first_frame)}") | ||
# The shape of the decoded frame is (H, W, C) where H and W are the height | ||
# and width of the video frame, respectively. C is 3 because we have 3 channels | ||
# red, green, and blue. | ||
print(f"{first_frame.shape=}") | ||
# The dtype of the decoded frame is uint8. | ||
print(f"{first_frame.dtype=}") | ||
|
||
last_frame = simple_decoder[num_frames - 1] | ||
print(f"{last_frame.shape=}") | ||
|
||
# TODO: add documentation for slices and metadata. | ||
|
||
# %% |
This file was deleted.
Oops, something went wrong.