Skip to content

Add FPS to get_frames_played_in_range()#1148

Merged
mollyxu merged 13 commits intometa-pytorch:mainfrom
mollyxu:implement-fps
Jan 23, 2026
Merged

Add FPS to get_frames_played_in_range()#1148
mollyxu merged 13 commits intometa-pytorch:mainfrom
mollyxu:implement-fps

Conversation

@mollyxu
Copy link
Copy Markdown
Contributor

@mollyxu mollyxu commented Dec 30, 2025

Add FPS to get_frames_played_in_range()

Context: #1133

This PR adds support for specifying a target frame rate when calling get_frames_played_in_range(). When target_fps is provided, the method resamples the video to output frames at evenly-spaced timestamps matching the target FPS.

def get_frames_played_in_range(
    self,
    start_seconds: float,
    stop_seconds: float,
    fps: float | None = None,
) -> FrameBatch:

The way to read this API if fps is:

  • None, default: returns decoded frames between start_seconds and stop_seconds at the input frame rate. This is the existing behavior.
  • float: returns decoded frames between start_seconds and stop_seconds where the output frame rate matches the value of fps.

Implementation

  1. Frame count calculation: num_output_frames = round((stop - start) * fps)
  2. Timestamp generation: For each output frame i, compute target_pts = start + i / fps
  3. Source frame lookup: Find the source frame displayed at target_pts using lower-bound search
  4. Decode optimization: When consecutive output frames map to the same source frame (common when upsampling), the already-decoded tensor is copied rather than re-decoded

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Dec 30, 2025
Comment thread src/torchcodec/_core/SingleStreamDecoder.cpp
@mollyxu mollyxu marked this pull request as ready for review January 5, 2026 17:33
Comment thread src/torchcodec/_core/SingleStreamDecoder.cpp
Comment thread src/torchcodec/_core/SingleStreamDecoder.cpp Outdated
Comment thread src/torchcodec/_core/SingleStreamDecoder.cpp Outdated
Comment thread src/torchcodec/_core/SingleStreamDecoder.cpp Outdated
Comment thread src/torchcodec/_core/SingleStreamDecoder.cpp Outdated
Comment thread test/test_decoders.py
Comment thread test/test_decoders.py Outdated
Comment thread test/test_decoders.py
Comment thread src/torchcodec/decoders/_video_decoder.py
Comment thread src/torchcodec/_core/SingleStreamDecoder.cpp Outdated
Comment thread src/torchcodec/_core/SingleStreamDecoder.cpp Outdated
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 great PR @mollyxu ! Left a first pass, this looks good

Comment thread test/test_decoders.py
Comment thread test/test_decoders.py Outdated
Comment thread test/test_decoders.py Outdated
Comment thread test/test_decoders.py Outdated
Comment thread test/test_decoders.py
Comment thread test/test_decoders.py Outdated
Comment thread test/test_decoders.py Outdated
Comment thread src/torchcodec/_core/SingleStreamDecoder.cpp Outdated
Comment thread src/torchcodec/_core/SingleStreamDecoder.cpp Outdated
Comment thread src/torchcodec/_core/SingleStreamDecoder.cpp Outdated
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 great work @mollyxu ! I left two comments below and followed-up on the FFmpeg 4 thing above (#1148 (comment)). But this LGTM!

Comment thread src/torchcodec/decoders/_video_decoder.py
Comment thread src/torchcodec/_core/SingleStreamDecoder.cpp Outdated
Comment thread test/test_decoders.py Outdated
@mollyxu
Copy link
Copy Markdown
Contributor Author

mollyxu commented Jan 23, 2026

Thank you everyone for your feedback and reviews!

@mollyxu mollyxu merged commit db0a147 into meta-pytorch:main Jan 23, 2026
65 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.

3 participants