uncleh00ru

UH_VideoConfig_v03

Jul 15th, 2026
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.03 KB | Source Code | 0 0
  1. # custom_nodes/uh_video_config_v03.py
  2. import re
  3. from datetime import datetime
  4.  
  5. try:
  6.     from server import PromptServer
  7. except Exception:
  8.     PromptServer = None
  9.  
  10. class UHVideoConfig:
  11.     @classmethod
  12.     def INPUT_TYPES(cls):
  13.         return {
  14.             "required": {
  15.                 "preset": ([
  16.                     "Custom",
  17.  
  18.                     "─ 9:16 ─",
  19.                     "352x640",
  20.                     "544x960",
  21.                     "720x1280",
  22.                     "864x1536",
  23.                     "1008x1792",
  24.                     "1088x1920",
  25.  
  26.                     "─ 2:3 ─",
  27.                     "512x768",
  28.                     "640x960",
  29.                     "768x1152",
  30.                     "896x1344",
  31.                     "1024x1536",
  32.                     "1152x1728",
  33.                     "1280x1920",
  34.  
  35.                     "─ 3:4 ─",
  36.                     "480x640",
  37.                     "720x960",
  38.                     "768x1024",
  39.                     "864x1152",
  40.                     "960x1280",
  41.                     "1152x1536",
  42.                     "1344x1792",
  43.                     "1440x1920",
  44.  
  45.                     "─ 1:1 ─",
  46.                     "640x640",
  47.                     "1024x1024",
  48.                     "1920x1920",
  49.                 ], {"default": "720x1280", "tooltip": "Select a predefined output resolution."}),
  50.                 "invert": ("BOOLEAN", {"default": False, "tooltip": "Swap width and height of the selected resolution."}),
  51.                 "custom_width":  ("INT", {"default": 0, "min": 0, "max": 8192, "step": 8, "tooltip": "Custom output height. Used only when preset is set to Custom."}),
  52.                 "custom_height": ("INT", {"default": 0, "min": 0, "max": 8192, "step": 8, "tooltip": "Custom output height. Used only when preset is set to Custom."}),
  53.                 "fps":     ("INT",   {"default": 16, "min": 1, "max": 240, "step": 1}),
  54.                 "total_steps": ("INT", {"default": 6, "min": 1, "max": 100, "step": 1}),
  55.                 "shift": ("INT", {"default": 5, "min": 0, "max": 100, "step": 1}),
  56.                 "frame_load_cap":    ("INT", {"default": 0, "min": 0, "max": 10000, "step": 1, "tooltip": "Maximum number of frames to load from the source. 0 = unlimited." }),
  57.                 "skip_first_frames": ("INT", {"default": 0, "min": 0, "max": 10000, "step": 1, "tooltip": "Skip this many frames from the beginning of the input sequence."}),
  58.                 "prefix": ("STRING", {"default": "", "tooltip": "Optional filename or output label prefix."}),
  59.             },
  60.         }
  61.  
  62.     RETURN_TYPES = (
  63.         "INT",    # width
  64.         "INT",    # height
  65.         "FLOAT",  # fps
  66.         "INT",    # total_steps
  67.         "FLOAT",    # shift
  68.         "INT",    # frame_load_cap
  69.         "INT",    # skip_first_frames
  70.         "STRING", # label
  71.         "STRING", # date
  72.     )
  73.  
  74.     RETURN_NAMES = (
  75.         "width",
  76.         "height",
  77.         "fps",
  78.         "total_steps",
  79.         "shift",
  80.         "frame_load_cap",
  81.         "skip_first_frames",
  82.         "prefix",
  83.         "date",
  84.     )
  85.  
  86.     FUNCTION = "select"
  87.     CATEGORY = "utils/video"
  88.  
  89.     def select(
  90.         self,
  91.         preset,
  92.         invert,
  93.         custom_width,
  94.         custom_height,
  95.         fps,
  96.         total_steps,
  97.         shift,
  98.         frame_load_cap,
  99.         skip_first_frames,
  100.         prefix,
  101.     ):
  102.         # ---- guard for category labels ----
  103.         if preset.startswith("─"):
  104.             raise ValueError("Please select a resolution, not a category label.")
  105.  
  106.         # ---- resolution ----
  107.         if preset == "Custom":
  108.             w = custom_width
  109.             h = custom_height
  110.             if w <= 0 or h <= 0:
  111.                 raise ValueError("Custom resolution must be greater than 0.")
  112.         else:
  113.             m = re.match(r"^(\d+)x(\d+)$", preset)
  114.             if not m:
  115.                 raise ValueError(f"Invalid preset format: {preset}")
  116.             w, h = int(m.group(1)), int(m.group(2))
  117.  
  118.         # Swap width/height if needed
  119.         if invert:
  120.             w, h = h, w
  121.  
  122.         date = datetime.now().strftime("%Y-%m-%d-%H%M%S")
  123.  
  124.         return (
  125.             w,
  126.             h,
  127.             float(fps),
  128.             int(total_steps),
  129.             float(shift),
  130.             frame_load_cap,
  131.             skip_first_frames,
  132.             prefix,
  133.             date,
  134.         )
  135.  
  136. class UHVideoFramesConfig:
  137.     @classmethod
  138.     def INPUT_TYPES(cls):
  139.         return {
  140.             "required": {
  141.                 "width": ("INT",),
  142.                 "height": ("INT",),
  143.                 "fps": ("FLOAT",),
  144.                 "seconds": ("FLOAT", {
  145.                     "default": 5.0,
  146.                     "min": 0.5,
  147.                     "max": 100.0,
  148.                     "step": 0.5
  149.                 }),
  150.             },
  151.             "hidden": {
  152.                 "unique_id": "UNIQUE_ID",
  153.             },
  154.         }
  155.  
  156.     RETURN_TYPES = ("INT",)
  157.     RETURN_NAMES = ("frames",)
  158.  
  159.     FUNCTION = "calculate"
  160.  
  161.     CATEGORY = "utils/video"
  162.  
  163.     def calculate(self, width, height, fps, seconds, unique_id=None):
  164.  
  165.         # ---- adjust frame_count (4n + 1) ----
  166.         frames = int(round(fps * seconds))
  167.         frames = round((frames - 1) / 4) * 4 + 1
  168.         frames = max(1, frames)
  169.  
  170.         # ---- calculate megapixel ----
  171.         megapixels = (width * height * frames) / 1_000_000.0
  172.  
  173.         if unique_id and PromptServer and hasattr(PromptServer, "instance"):
  174.             try:
  175.                 PromptServer.instance.send_progress_text(
  176.                     f"<tr><td>Output: </td>"
  177.                     f"<td><b>{width}x{height}x{frames}f</b> | <b style='color:#FFA500;'>{megapixels:.2f}</b><b> MP</b></td></tr>",
  178.                     unique_id
  179.                 )
  180.             except Exception:
  181.                 pass
  182.  
  183.         return (frames,)
  184.  
  185.  
  186. NODE_CLASS_MAPPINGS = {
  187.     "UHVideoConfig": UHVideoConfig,
  188.     "UHVideoFramesConfig": UHVideoFramesConfig
  189. }
  190.  
  191. NODE_DISPLAY_NAME_MAPPINGS = {
  192.     "UHVideoConfig": "UH Video Config",
  193.     "UHVideoFramesConfig": "UH Video Frames Config"
  194. }
Advertisement
Add Comment
Please, Sign In to add comment