-
Notifications
You must be signed in to change notification settings - Fork 75
Create Python API for VideoEncoder #990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1e06ea5
video encoder python file
1e7dc34
testing
cf7b75c
delete contiguous todo
ee2285e
use randint suggestion, remove test skips
d14deb8
assert contiguity with channels_last
7ed7f21
incorporate feedback
Dan-Flores b10d80b
update test_bad_input match text
Dan-Flores File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| from ._audio_encoder import AudioEncoder # noqa | ||
| from ._video_encoder import VideoEncoder # noqa |
This file contains hidden or 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,92 @@ | ||
| from pathlib import Path | ||
| from typing import Union | ||
|
|
||
| import torch | ||
| from torch import Tensor | ||
|
|
||
| from torchcodec import _core | ||
|
|
||
|
|
||
| class VideoEncoder: | ||
| """A video encoder. | ||
|
|
||
| Args: | ||
| frames (``torch.Tensor``): The frames to encode. This must be a 4D | ||
| tensor of shape ``(N, C, H, W)`` where N is the number of frames, | ||
| C is 3 channels (RGB), H is height, and W is width. | ||
| Values must be uint8 in the range ``[0, 255]``. | ||
| frame_rate (int): The frame rate of the **input** ``frames``. Also defines the encoded **output** frame rate. | ||
| """ | ||
|
|
||
| def __init__(self, frames: Tensor, *, frame_rate: int): | ||
| torch._C._log_api_usage_once("torchcodec.encoders.VideoEncoder") | ||
| if not isinstance(frames, Tensor): | ||
| raise ValueError(f"Expected frames to be a Tensor, got {type(frames) = }.") | ||
| if frames.ndim != 4: | ||
| raise ValueError(f"Expected 4D frames, got {frames.shape = }.") | ||
| if frames.dtype != torch.uint8: | ||
| raise ValueError(f"Expected uint8 frames, got {frames.dtype = }.") | ||
| if frame_rate <= 0: | ||
| raise ValueError(f"{frame_rate = } must be > 0.") | ||
|
|
||
| self._frames = frames | ||
| self._frame_rate = frame_rate | ||
|
|
||
| def to_file( | ||
| self, | ||
| dest: Union[str, Path], | ||
| ) -> None: | ||
| """Encode frames into a file. | ||
|
|
||
| Args: | ||
| dest (str or ``pathlib.Path``): The path to the output file, e.g. | ||
| ``video.mp4``. The extension of the file determines the video | ||
| container format. | ||
| """ | ||
| _core.encode_video_to_file( | ||
| frames=self._frames, | ||
| frame_rate=self._frame_rate, | ||
| filename=str(dest), | ||
| ) | ||
|
|
||
| def to_tensor( | ||
| self, | ||
| format: str, | ||
| ) -> Tensor: | ||
| """Encode frames into raw bytes, as a 1D uint8 Tensor. | ||
|
|
||
| Args: | ||
| format (str): The container format of the encoded frames, e.g. "mp4", "mov", | ||
| "mkv", "avi", "webm", "flv", or "gif" | ||
|
|
||
| Returns: | ||
| Tensor: The raw encoded bytes as 4D uint8 Tensor. | ||
| """ | ||
| return _core.encode_video_to_tensor( | ||
| frames=self._frames, | ||
| frame_rate=self._frame_rate, | ||
| format=format, | ||
| ) | ||
|
|
||
| def to_file_like( | ||
| self, | ||
| file_like, | ||
| format: str, | ||
| ) -> None: | ||
| """Encode frames into a file-like object. | ||
|
|
||
| Args: | ||
| file_like: A file-like object that supports ``write()`` and | ||
| ``seek()`` methods, such as io.BytesIO(), an open file in binary | ||
| write mode, etc. Methods must have the following signature: | ||
| ``write(data: bytes) -> int`` and ``seek(offset: int, whence: | ||
| int = 0) -> int``. | ||
| format (str): The container format of the encoded frames, e.g. "mp4", "mov", | ||
| "mkv", "avi", "webm", "flv", or "gif". | ||
| """ | ||
| _core.encode_video_to_file_like( | ||
| frames=self._frames, | ||
| frame_rate=self._frame_rate, | ||
| format=format, | ||
| file_like=file_like, | ||
| ) |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.