ComfyUI-CustomNode/nodes/compute_video_point.py

52 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import re
from datetime import datetime
from math import ceil
import loguru
def validate_time_format(time_str):
pattern = r'^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|\d{1,2}).(\d{3})$'
return bool(re.match(pattern, time_str))
def get_duration_wave(audio):
waveform, sample_rate = audio["waveform"], audio["sample_rate"]
# 防止话说不完
return waveform.shape[2] / sample_rate
class VideoStartPointDurationCompute:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"start_time": ("STRING", {"forceInput": True}),
"audio": ("AUDIO", {"forceInput": True}),
"end_padding": ("FLOAT", {"forceInput": True, "default": 0.4}),
"fps": ("INT", {"default": 25, "step": 1}),
},
}
RETURN_TYPES = ("FLOAT", "FLOAT",)
RETURN_NAMES = ("起始帧位", "帧数")
FUNCTION = "compute"
CATEGORY = "不忘科技-自定义节点🚩"
def compute(self, start_time, audio, fps, end_padding):
if not validate_time_format(start_time):
raise ValueError("start_time或者end_time时间格式不对start_time or end_time is not in time format")
time_format = "%H:%M:%S.%f"
start_dt = datetime.strptime(start_time, time_format)
start_sec = (start_dt - datetime(1900, 1, 1)).total_seconds()
start_point = start_sec * fps
duration = get_duration_wave(audio)
loguru.logger.info("audio duration %.3f s" % duration)
duration = duration + end_padding
loguru.logger.info("audio duration with padding %.3f s" % duration)
return (start_point, duration*fps,)