Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # custom_nodes/simple_video_resample_v01.py
- import torch
- from server import PromptServer
- class SimpleVideoResample:
- @classmethod
- def INPUT_TYPES(cls):
- return {
- "required": {
- "images": ("IMAGE",),
- "fps_in": ("FLOAT", {"default": 30.0, "min": 0.0, "max": 240.0}),
- "fps_target": ("FLOAT", {"default": 24.0, "min": 1.0, "max": 240.0}),
- },
- "hidden": {
- "unique_id": "UNIQUE_ID",
- }
- }
- RETURN_TYPES = ("IMAGE", "FLOAT", "INT")
- RETURN_NAMES = ("images", "fps_float", "fps_int")
- FUNCTION = "resample"
- CATEGORY = "utils"
- def resample(self, images, fps_in, fps_target, unique_id=None):
- # Fallback if input FPS is unknown
- frame_count_in = images.shape[0]
- if fps_in <= 0:
- fps_int = int(round(fps_target))
- info = f"[Skip]: unknown fps >>> {frame_count_in}f @{fps_target:.2f}fps"
- if unique_id and PromptServer is not None:
- try:
- PromptServer.instance.send_progress_text(
- f"<tr><td>{info}</td></tr>",
- unique_id
- )
- except Exception:
- pass
- return (images, fps_target, fps_int)
- # Calculate sampling step
- if fps_target <= 0:
- return (images, fps_target, int(round(fps_target)))
- step = fps_in / fps_target
- # Center-aligned sampling offset for better temporal accuracy
- offset = step / 2.0
- # Generate frame indices efficiently
- indices = torch.arange(offset, frame_count_in, step, device=images.device)
- indices = indices.long()
- indices = torch.clamp(indices, 0, frame_count_in - 1)
- # Avoid unnecessary copy if frame count is unchanged
- if len(indices) == 0:
- indices = torch.tensor([0], dtype=torch.long)
- # Perform frame selection
- resampled = images[indices]
- frame_count_out = resampled.shape[0]
- # Convert FPS to integer (for LTX compatibility)
- fps_int = int(round(fps_target))
- if fps_in < fps_target:
- mode = "Upsample"
- elif fps_in > fps_target:
- mode = "Downsample"
- else:
- mode = "No change"
- info = f"[{mode}] {frame_count_in}f @{fps_in:.2f}fps >>> {frame_count_out}f @{fps_target:.2f}fps"
- # Display Console
- if unique_id and PromptServer is not None:
- try:
- PromptServer.instance.send_progress_text(
- f"<tr><td>{info}</td></tr>",
- unique_id
- )
- except Exception:
- pass
- return (resampled, fps_target, fps_int)
- NODE_CLASS_MAPPINGS = {
- "SimpleVideoResample": SimpleVideoResample
- }
- NODE_DISPLAY_NAME_MAPPINGS = {
- "SimpleVideoResample": "Simple Video Resample (No interpolation)"
- }
Advertisement
Add Comment
Please, Sign In to add comment