feat: VIDEO_EDIT input type for video trim/crop rich widgets

This commit is contained in:
Terry Jia
2026-07-26 07:48:30 -04:00
parent 2a68ce33b4
commit e567f78876
7 changed files with 438 additions and 54 deletions

View File

@@ -4,7 +4,7 @@ from fractions import Fraction
from typing import Optional, Union, IO
import io
import av
from .._util import VideoContainer, VideoCodec, VideoComponents
from .._util import VideoContainer, VideoCodec, VideoComponents, normalize_crop_rect
class VideoInput(ABC):
"""
@@ -54,6 +54,45 @@ class VideoInput(ABC):
"""
pass
def as_cropped(
self,
x: int = 0,
y: int = 0,
width: int = 0,
height: int = 0,
) -> VideoInput:
"""
Create a new VideoInput spatially cropped to the given pixel rectangle.
The rectangle is clamped to the frame and even-aligned for encoder
compatibility. An empty or full-frame rectangle returns the input
unchanged.
Default implementation materializes the video via get_components();
subclasses should override with lazier strategies when possible.
"""
components = self.get_components()
rect = normalize_crop_rect(
x, y, width, height, components.images.shape[2], components.images.shape[1]
)
if rect is None:
return self
from .._input_impl.video_types import VideoFromComponents
cx, cy, cw, ch = rect
return VideoFromComponents(
VideoComponents(
images=components.images[:, cy:cy + ch, cx:cx + cw, :],
audio=components.audio,
frame_rate=components.frame_rate,
metadata=components.metadata,
alpha=components.alpha[:, cy:cy + ch, cx:cx + cw]
if components.alpha is not None
else None,
),
bit_depth=self.get_bit_depth(),
)
def get_stream_source(self) -> Union[str, io.BytesIO]:
"""
Get a streamable source for the video. This allows processing without