Skip to content

feat: add get_frames_played_in_range to video decoder hierarchy#14

Merged
MilkClouds merged 5 commits intomainfrom
feat/get-frames-played-in-range
Feb 6, 2026
Merged

feat: add get_frames_played_in_range to video decoder hierarchy#14
MilkClouds merged 5 commits intomainfrom
feat/get-frames-played-in-range

Conversation

@MilkClouds
Copy link
Member

@MilkClouds MilkClouds commented Feb 6, 2026

Summary

Add get_frames_played_in_range(start_seconds, stop_seconds, fps=None) method to the video decoder hierarchy.

Changes

  • BaseVideoDecoder — Add abstract method
  • TorchCodecVideoDecoder — Delegate to TorchCodec's native method. Raises NotImplementedError when fps is specified on TorchCodec <=0.10.0 (which doesn't support it)
  • PyAVVideoDecoder — Full implementation:
    • fps=None: Seek and decode all frames with pts in [start_seconds, stop_seconds)
    • fps specified: Generate timestamps at 1/fps intervals and delegate to get_frames_played_at
    • Validation matching TorchCodec semantics (start ≤ stop, stream bounds checking)

Tests

  • test_pyav_decoder.py — 13 tests added
  • test_torchcodec_decoder.py — 6 tests added
  • Full video_decoder test suite: 93 passed, 6 xfailed

- Add abstract method to BaseVideoDecoder
- Add TorchCodec implementation (raises NotImplementedError for fps on <=0.10.0)
- Add PyAV implementation with native decode and fps resampling
- Add 13 tests for PyAV, 6 tests for TorchCodec
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @MilkClouds, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new, flexible method for extracting video frames within a specified time interval, allowing users to retrieve frames either at the video's native frame rate or a custom resampled rate. This enhancement provides a more granular control over frame access, improving the utility of the video decoder hierarchy for various multimedia processing tasks.

Highlights

  • New Abstract Method: An abstract method get_frames_played_in_range(start_seconds, stop_seconds, fps=None) has been added to the BaseVideoDecoder class, defining a standard interface for retrieving frames within a specified time range.
  • PyAVVideoDecoder Implementation: The PyAVVideoDecoder now fully implements get_frames_played_in_range. It supports decoding frames at the native video rate by seeking and iterating, and also handles fps based resampling by generating timestamps and delegating to get_frames_played_at.
  • TorchCodecVideoDecoder Implementation: The TorchCodecVideoDecoder implements get_frames_played_in_range by delegating to TorchCodec's native method. It includes a check to raise NotImplementedError if the fps parameter is used with older TorchCodec versions (<=0.10.0) that do not support it.
  • Comprehensive Testing: Extensive tests have been added for both PyAVVideoDecoder (13 new tests) and TorchCodecVideoDecoder (6 new tests) to ensure the correct functionality and error handling of the new get_frames_played_in_range method across various scenarios, including range validation, fps resampling, and edge cases.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/mediaref/video_decoder/base.py
    • Added abstract method get_frames_played_in_range to define a common interface for range-based frame retrieval.
    • Included detailed docstrings for the new method, specifying parameters, return types, and potential exceptions.
  • src/mediaref/video_decoder/pyav_decoder.py
    • Implemented get_frames_played_in_range to support both native frame rate decoding and fps based resampling.
    • Added robust validation for start_seconds and stop_seconds to ensure they are within valid stream bounds and ordered correctly.
    • Integrated logic to convert av.VideoFrame objects into NCHW NumPy arrays for consistent output.
  • src/mediaref/video_decoder/torchcodec_decoder.py
    • Implemented get_frames_played_in_range by delegating to the underlying TorchCodec library's functionality.
    • Added a version compatibility check that raises NotImplementedError if fps is provided to an older TorchCodec version (<=0.10.0).
  • tests/video_decoder/test_pyav_decoder.py
    • Added a new test class TestPyAVVideoDecoderGetFramesPlayedInRange with 13 individual tests.
    • Tests cover various scenarios including basic range queries, full video range, single-frame ranges, mid-video ranges, NCHW format verification, empty results for start == stop, and error handling for invalid input parameters.
    • Included tests specifically for fps resampling, verifying correct frame counts for higher fps and native rate behavior when fps=None.
  • tests/video_decoder/test_torchcodec_decoder.py
    • Added a new test class TestTorchCodecVideoDecoderGetFramesPlayedInRange with 6 individual tests.
    • Tests validate basic range queries, full video range, NCHW format, error handling for invalid ranges, and the NotImplementedError for fps with older TorchCodec versions.
    • Confirmed that fps=None correctly returns frames at the native rate.
Activity
  • The author, MilkClouds, initiated this pull request to introduce a new feature for video frame decoding.
  • The PR includes a detailed summary of changes and test results, indicating a thorough approach to development.
  • The test suite reports 93 passed tests and 6 xfailed tests, suggesting a comprehensive test coverage with some known, expected failures.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

gemini-code-assist[bot]

This comment was marked as resolved.

- Replace while loop with np.arange for float timestamp generation
- Extract _create_empty_batch helper to reduce duplication
- Extract _convert_av_frames_to_nchw helper to reduce duplication
- Strengthen test assertion from >= 2 to == 3
Consistent with existing _frame_to_rgba pattern - no self dependency.
Also fix ruff format issue (extra blank line)
@MilkClouds MilkClouds merged commit fb47edc into main Feb 6, 2026
16 checks passed
@MilkClouds MilkClouds deleted the feat/get-frames-played-in-range branch February 6, 2026 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant