Skip to content

Consolidate VideoDecoder creation with python wrapper#1250

Merged
NicolasHug merged 6 commits intometa-pytorch:mainfrom
zy1git:consolidate-python-version-video
Mar 4, 2026
Merged

Consolidate VideoDecoder creation with python wrapper#1250
NicolasHug merged 6 commits intometa-pytorch:mainfrom
zy1git:consolidate-python-version-video

Conversation

@zy1git
Copy link
Copy Markdown
Contributor

@zy1git zy1git commented Feb 19, 2026

This PR introduces a create_video_decoder() wrapper function that consolidates the two-step video decoder creation process (create decoder + add video stream) into a single function call.

This is the Python-only phase of the video decoder consolidation for issue #1064.

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented Feb 19, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/meta-pytorch/torchcodec/1250

Note: Links to docs will display an error until the docs builds have been completed.

⏳ No Failures, 1 Pending

As of commit 024ac78 with merge base 068e58a (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Feb 19, 2026
@zy1git zy1git marked this pull request as draft February 19, 2026 08:24
@zy1git zy1git force-pushed the consolidate-python-version-video branch from c42ad34 to 4391528 Compare February 25, 2026 10:24
@zy1git zy1git marked this pull request as ready for review February 25, 2026 10:26
Comment thread src/torchcodec/_core/_decoder_utils.py Outdated
Comment thread src/torchcodec/decoders/_video_decoder.py Outdated
Comment thread src/torchcodec/_core/_decoder_utils.py Outdated
Comment on lines +110 to +119
"""Get and validate video stream metadata from a decoder.

Args:
decoder: The decoder tensor.
stream_index: The stream index to use, or None to use the best stream.

Returns:
A tuple of (metadata, stream_index, begin_stream_seconds,
end_stream_seconds, num_frames).
"""
Copy link
Copy Markdown
Contributor

@NicolasHug NicolasHug Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helper didn't have docs, I feel like it's not really necessary to add them. The interface is pretty straightforward, this isn't user-facing, and any modification to the underlying logic would mean we'd have to keep the docs up to date consequently. So I'd suggest to just remove the docstrings and keep it they it was.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in the new commit.

Comment thread src/torchcodec/_core/_decoder_utils.py Outdated
Comment on lines +207 to +210
if device is None:
device = str(torch.get_default_device())
elif isinstance(device, torch_device):
device = str(device)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now done twice, here and in __init__. Let's just do it once and let this function accept the string as input.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in the new commit.

Comment thread src/torchcodec/_core/_decoder_utils.py Outdated
Comment on lines +159 to +161
begin_stream_seconds,
end_stream_seconds,
num_frames,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize this was already the case in main, but let's use this PR as opportunity to clean this up too: all of begin_stream_seconds, begin_stream_seconds and num_frames are just fields of metadata, so let's not return them. We should just use the info from metadata whenever relevant.

We can keep the existing fields like self._begin_stream_seconds on the VideoDecoder` however.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in the new commit.

Copy link
Copy Markdown
Contributor

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the progress. Can you look into the mypy lint issue?

Comment thread src/torchcodec/_core/_decoder_utils.py Outdated
Comment on lines +160 to +178
"""Create a video decoder and add a video stream.

This function consolidates the creation of a decoder and adding a video stream
into a single operation, performing all necessary validation.

Args:
source: The source of the video.
seek_mode: The seek mode for the decoder.
stream_index: The stream index to decode, or None to use the best stream.
dimension_order: The dimension order for decoded frames.
num_ffmpeg_threads: Number of FFmpeg threads for CPU decoding.
device: The device for decoding (as a string).
device_variant: The CUDA backend variant to use ("ffmpeg" or "beta").
transforms: Optional sequence of transforms to apply.
custom_frame_mappings: Optional pre-processed frame mappings data.

Returns:
A tuple of (decoder, metadata, stream_index).
"""
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not add a docstring here. Same reason as in #1250 (comment)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in the new commit.

Comment thread src/torchcodec/_core/_decoder_utils.py Outdated
decoder,
metadata,
stream_index,
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the return order consistent with create_audio_decoder which is returning decoder, stream_index, metadata.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, as Nit, here and in other places, just make the return fit in one line instead of 3 if possible.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in the new commit

Comment thread src/torchcodec/_core/_decoder_utils.py Outdated
return (decoder, stream_index, metadata)


def _get_and_validate_stream_metadata(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's name this _get_and_validate_video_stream_metadata since this is video only now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in the new commit

@zy1git zy1git force-pushed the consolidate-python-version-video branch from ad903bb to 024ac78 Compare March 4, 2026 01:50
@NicolasHug NicolasHug merged commit 3d6046a into meta-pytorch:main Mar 4, 2026
65 of 66 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants