uncleh00ru

SimpleVideoResamle

Apr 3rd, 2026
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | Source Code | 0 0
  1. # custom_nodes/simple_video_resample_v01.py
  2.  
  3. import torch
  4. from server import PromptServer
  5.  
  6. class SimpleVideoResample:
  7.     @classmethod
  8.  
  9.     def INPUT_TYPES(cls):
  10.         return {
  11.             "required": {
  12.                 "images": ("IMAGE",),
  13.                 "fps_in": ("FLOAT", {"default": 30.0, "min": 0.0, "max": 240.0}),
  14.                 "fps_target": ("FLOAT", {"default": 24.0, "min": 1.0, "max": 240.0}),
  15.             },
  16.             "hidden": {
  17.                 "unique_id": "UNIQUE_ID",
  18.             }
  19.         }
  20.  
  21.     RETURN_TYPES = ("IMAGE", "FLOAT", "INT")
  22.     RETURN_NAMES = ("images", "fps_float", "fps_int")
  23.     FUNCTION = "resample"
  24.     CATEGORY = "utils"
  25.  
  26.     def resample(self, images, fps_in, fps_target, unique_id=None):
  27.         # Fallback if input FPS is unknown
  28.         frame_count_in = images.shape[0]
  29.  
  30.         if fps_in <= 0:
  31.             fps_int = int(round(fps_target))
  32.             info = f"[Skip]: unknown fps >>> {frame_count_in}f @{fps_target:.2f}fps"
  33.  
  34.             if unique_id and PromptServer is not None:
  35.                 try:
  36.                     PromptServer.instance.send_progress_text(
  37.                         f"<tr><td>{info}</td></tr>",
  38.                         unique_id
  39.                     )
  40.                 except Exception:
  41.                     pass
  42.  
  43.             return (images, fps_target, fps_int)
  44.  
  45.         # Calculate sampling step
  46.         if fps_target <= 0:
  47.             return (images, fps_target, int(round(fps_target)))
  48.         step = fps_in / fps_target
  49.  
  50.         # Center-aligned sampling offset for better temporal accuracy
  51.         offset = step / 2.0
  52.  
  53.         # Generate frame indices efficiently
  54.         indices = torch.arange(offset, frame_count_in, step, device=images.device)
  55.         indices = indices.long()
  56.         indices = torch.clamp(indices, 0, frame_count_in - 1)
  57.  
  58.         # Avoid unnecessary copy if frame count is unchanged
  59.         if len(indices) == 0:
  60.             indices = torch.tensor([0], dtype=torch.long)
  61.  
  62.         # Perform frame selection
  63.         resampled = images[indices]
  64.  
  65.         frame_count_out = resampled.shape[0]
  66.  
  67.         # Convert FPS to integer (for LTX compatibility)
  68.         fps_int = int(round(fps_target))
  69.  
  70.         if fps_in < fps_target:
  71.             mode = "Upsample"
  72.         elif fps_in > fps_target:
  73.             mode = "Downsample"
  74.         else:
  75.             mode = "No change"
  76.  
  77.         info = f"[{mode}] {frame_count_in}f @{fps_in:.2f}fps >>> {frame_count_out}f @{fps_target:.2f}fps"
  78.  
  79.         # Display Console
  80.         if unique_id and PromptServer is not None:
  81.             try:
  82.                 PromptServer.instance.send_progress_text(
  83.                     f"<tr><td>{info}</td></tr>",
  84.                     unique_id
  85.                 )
  86.             except Exception:
  87.                 pass
  88.  
  89.         return (resampled, fps_target, fps_int)
  90.  
  91.  
  92. NODE_CLASS_MAPPINGS = {
  93.     "SimpleVideoResample": SimpleVideoResample
  94. }
  95.  
  96. NODE_DISPLAY_NAME_MAPPINGS = {
  97.     "SimpleVideoResample": "Simple Video Resample (No interpolation)"
  98. }
Advertisement
Add Comment
Please, Sign In to add comment