Skip to content

Commit

Permalink
[torchcodec] Add a simple example showing how to decode the first fra…
Browse files Browse the repository at this point in the history
…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
ahmadsharif1 authored and facebook-github-bot committed Jun 26, 2024
1 parent d4c35f1 commit 2387fa2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
33 changes: 33 additions & 0 deletions examples/basic_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
==================================================
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)}")
print(f"{first_frame.shape=}")

last_frame = simple_decoder[num_frames - 1]
print(f"{last_frame.shape=}")

# TODO: add documentation for slices and metadata.

# %%
34 changes: 0 additions & 34 deletions examples/basic_example_remove_me.py

This file was deleted.

0 comments on commit 2387fa2

Please sign in to comment.