Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # custom_nodes/uh_video_config_v03.py
- import re
- from datetime import datetime
- try:
- from server import PromptServer
- except Exception:
- PromptServer = None
- class UHVideoConfig:
- @classmethod
- def INPUT_TYPES(cls):
- return {
- "required": {
- "preset": ([
- "Custom",
- "─ 9:16 ─",
- "352x640",
- "544x960",
- "720x1280",
- "864x1536",
- "1008x1792",
- "1088x1920",
- "─ 2:3 ─",
- "512x768",
- "640x960",
- "768x1152",
- "896x1344",
- "1024x1536",
- "1152x1728",
- "1280x1920",
- "─ 3:4 ─",
- "480x640",
- "720x960",
- "768x1024",
- "864x1152",
- "960x1280",
- "1152x1536",
- "1344x1792",
- "1440x1920",
- "─ 1:1 ─",
- "640x640",
- "1024x1024",
- "1920x1920",
- ], {"default": "720x1280", "tooltip": "Select a predefined output resolution."}),
- "invert": ("BOOLEAN", {"default": False, "tooltip": "Swap width and height of the selected resolution."}),
- "custom_width": ("INT", {"default": 0, "min": 0, "max": 8192, "step": 8, "tooltip": "Custom output height. Used only when preset is set to Custom."}),
- "custom_height": ("INT", {"default": 0, "min": 0, "max": 8192, "step": 8, "tooltip": "Custom output height. Used only when preset is set to Custom."}),
- "fps": ("INT", {"default": 16, "min": 1, "max": 240, "step": 1}),
- "total_steps": ("INT", {"default": 6, "min": 1, "max": 100, "step": 1}),
- "shift": ("INT", {"default": 5, "min": 0, "max": 100, "step": 1}),
- "frame_load_cap": ("INT", {"default": 0, "min": 0, "max": 10000, "step": 1, "tooltip": "Maximum number of frames to load from the source. 0 = unlimited." }),
- "skip_first_frames": ("INT", {"default": 0, "min": 0, "max": 10000, "step": 1, "tooltip": "Skip this many frames from the beginning of the input sequence."}),
- "prefix": ("STRING", {"default": "", "tooltip": "Optional filename or output label prefix."}),
- },
- }
- RETURN_TYPES = (
- "INT", # width
- "INT", # height
- "FLOAT", # fps
- "INT", # total_steps
- "FLOAT", # shift
- "INT", # frame_load_cap
- "INT", # skip_first_frames
- "STRING", # label
- "STRING", # date
- )
- RETURN_NAMES = (
- "width",
- "height",
- "fps",
- "total_steps",
- "shift",
- "frame_load_cap",
- "skip_first_frames",
- "prefix",
- "date",
- )
- FUNCTION = "select"
- CATEGORY = "utils/video"
- def select(
- self,
- preset,
- invert,
- custom_width,
- custom_height,
- fps,
- total_steps,
- shift,
- frame_load_cap,
- skip_first_frames,
- prefix,
- ):
- # ---- guard for category labels ----
- if preset.startswith("─"):
- raise ValueError("Please select a resolution, not a category label.")
- # ---- resolution ----
- if preset == "Custom":
- w = custom_width
- h = custom_height
- if w <= 0 or h <= 0:
- raise ValueError("Custom resolution must be greater than 0.")
- else:
- m = re.match(r"^(\d+)x(\d+)$", preset)
- if not m:
- raise ValueError(f"Invalid preset format: {preset}")
- w, h = int(m.group(1)), int(m.group(2))
- # Swap width/height if needed
- if invert:
- w, h = h, w
- date = datetime.now().strftime("%Y-%m-%d-%H%M%S")
- return (
- w,
- h,
- float(fps),
- int(total_steps),
- float(shift),
- frame_load_cap,
- skip_first_frames,
- prefix,
- date,
- )
- class UHVideoFramesConfig:
- @classmethod
- def INPUT_TYPES(cls):
- return {
- "required": {
- "width": ("INT",),
- "height": ("INT",),
- "fps": ("FLOAT",),
- "seconds": ("FLOAT", {
- "default": 5.0,
- "min": 0.5,
- "max": 100.0,
- "step": 0.5
- }),
- },
- "hidden": {
- "unique_id": "UNIQUE_ID",
- },
- }
- RETURN_TYPES = ("INT",)
- RETURN_NAMES = ("frames",)
- FUNCTION = "calculate"
- CATEGORY = "utils/video"
- def calculate(self, width, height, fps, seconds, unique_id=None):
- # ---- adjust frame_count (4n + 1) ----
- frames = int(round(fps * seconds))
- frames = round((frames - 1) / 4) * 4 + 1
- frames = max(1, frames)
- # ---- calculate megapixel ----
- megapixels = (width * height * frames) / 1_000_000.0
- if unique_id and PromptServer and hasattr(PromptServer, "instance"):
- try:
- PromptServer.instance.send_progress_text(
- f"<tr><td>Output: </td>"
- f"<td><b>{width}x{height}x{frames}f</b> | <b style='color:#FFA500;'>{megapixels:.2f}</b><b> MP</b></td></tr>",
- unique_id
- )
- except Exception:
- pass
- return (frames,)
- NODE_CLASS_MAPPINGS = {
- "UHVideoConfig": UHVideoConfig,
- "UHVideoFramesConfig": UHVideoFramesConfig
- }
- NODE_DISPLAY_NAME_MAPPINGS = {
- "UHVideoConfig": "UH Video Config",
- "UHVideoFramesConfig": "UH Video Frames Config"
- }
Advertisement
Add Comment
Please, Sign In to add comment