-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnode.py
More file actions
289 lines (246 loc) · 10.3 KB
/
node.py
File metadata and controls
289 lines (246 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import os
import torch
import time
import json
from torchvision.io import read_video
from PIL import Image
import requests
import base64
from tqdm import tqdm
def save_hwc_tensor_as_png(tensor, filename):
# 确保张量在CPU且无梯度,并转换为HWC格式的NumPy数组
np_array = tensor.detach().cpu().numpy()
# 缩放值域并转换为uint8
np_array = (np_array * 255).astype('uint8')
# 使用PIL保存图像
Image.fromarray(np_array).save(filename)
class TI2V:
CATEGORY = "StepVideo"
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image_input": ("IMAGE",),
"remote_server_url": ("STRING", {
"multiline": False,
"default": "127.0.0.1",
"lazy": True
}),
"model_dir": ("STRING", {
"multiline": False,
"default": "",
"lazy": True
}),
"script_dir": ("STRING", {
"multiline": False,
"default": "",
"lazy": True
}),
"infer_steps": ("INT", {
"default": 50,
"min": 0, #Minimum value
"max": 100, #Maximum value
"step": 1, #Slider's step
"display": "number", # Cosmetic only: display as "number" or "slider"
# "lazy": True # Will only be evaluated if check_lazy_status requires it
}),
"cfg_scale": ("FLOAT", {
"default": 9,
"min": 0.0,
"max": 50.0,
"step": 0.1,
# "round": 0.001, #The value representing the precision to round to, will be set to the step value by default. Can be set to False to disable rounding.
"display": "number",
# "lazy": True
}),
"time_shift": ("FLOAT", {
"default": 13.0,
"min": 0.0,
"max": 50.0,
"step": 0.1,
# "round": 0.001, #The value representing the precision to round to, will be set to the step value by default. Can be set to False to disable rounding.
"display": "number",
# "lazy": True
}),
"num_frames": ("INT", {
"default": 102,
"min": 0, #Minimum value
"max": 204, #Maximum value
"step": 1, #Slider's step
"display": "number", # Cosmetic only: display as "number" or "slider"
# "lazy": True # Will only be evaluated if check_lazy_status requires it
}),
"motion_score": ("FLOAT", {
"default": 5,
"min": 0.0,
"max": 50.0,
"step": 0.1,
# "round": 0.001, #The value representing the precision to round to, will be set to the step value by default. Can be set to False to disable rounding.
"display": "number",
# "lazy": True
}),
"text_prompt": ("STRING", {
"multiline": True,
"default": "笑起来",
"lazy": True
}),
}
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "ti2v"
def ti2v(self, image_input, remote_server_url, model_dir, script_dir, infer_steps, cfg_scale, time_shift, num_frames, motion_score, text_prompt):
os.makedirs(f'{script_dir}/results', exist_ok=True)
task_name = text_prompt[:50]
save_hwc_tensor_as_png(image_input[0], f'{script_dir}/results/{task_name}_img.png')
parallel = 1 # or parallel = 8
command = f'cd {script_dir} && torchrun --nproc_per_node {parallel} run_parallel.py --model_dir {model_dir} --vae_url {remote_server_url} --caption_url {remote_server_url} --ulysses_degree {parallel} --first_image_path results/{task_name}_img.png --prompt "{text_prompt}" --infer_steps {infer_steps} --cfg_scale {cfg_scale} --time_shift {time_shift} --num_frames {num_frames} --output_file_name {task_name}_vid --motion_score {motion_score} --name_suffix comfyui'
os.system(command)
video_path = f'{script_dir}/results/{task_name}_vid-comfyui.mp4'
# 读取视频文件,返回形状为 (T, H, W, C) 的uint8张量
video, _, _ = read_video(video_path, pts_unit="sec") # 默认output_format="THWC"
# 转换为浮点张量并归一化到0~1范围
video_tensor = video.to(torch.float32) / 255.0
return (video_tensor,)
class TI2V_API:
CATEGORY = "StepVideo"
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image_input": ("IMAGE",),
"api_url": ("STRING", {
"multiline": False,
"default": "https://api.stepfun.com/v1/video/generations",
"lazy": True
}),
"api_key": ("STRING", {
"multiline": False,
"default": "",
"lazy": True
}),
"video_size": (['960x540', '544x992', '768x768'], {"default": '960x540'}),
"text_prompt": ("STRING", {
"multiline": True,
"default": "笑起来",
"lazy": True
}),
}
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "ti2v"
def download_video(self, url, save_path=None, chunk_size=1024):
"""
下载视频文件到本地
参数:
url (str): 视频URL
save_path (str): 保存路径(可选)
chunk_size (int): 下载块大小(默认1024字节)
返回:
str: 最终保存路径
"""
try:
# 设置默认保存路径
if not save_path:
filename = url.split("/")[-1].split("?")[0] # 从URL提取文件名
save_path = os.path.join(os.getcwd(), filename)
# 创建请求头(模拟浏览器)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
# 发起带流式传输的GET请求
response = requests.get(url, headers=headers, stream=True)
response.raise_for_status() # 检查HTTP错误
# 获取文件总大小(字节)
total_size = int(response.headers.get('content-length', 0))
# 创建进度条
progress = tqdm(
total=total_size,
unit='B',
unit_scale=True,
desc=f"Downloading {os.path.basename(save_path)}"
)
# 写入文件
with open(save_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=chunk_size):
if chunk: # 过滤保持连接的空白块
f.write(chunk)
progress.update(len(chunk))
progress.close()
# 验证文件大小
if total_size != 0 and progress.n != total_size:
raise RuntimeError("下载不完整,请重试")
return save_path
except requests.exceptions.RequestException as e:
print(f"下载失败: {str(e)}")
if os.path.exists(save_path):
os.remove(save_path) # 删除不完整文件
return None
except Exception as e:
print(f"发生未知错误: {str(e)}")
return None
def ti2v(self, image_input, api_url, api_key, video_size, text_prompt):
task_dir = 'output'
os.makedirs(f'{task_dir}', exist_ok=True)
# 准备图片
task_name = text_prompt[:50]
img_path = f'{task_dir}/{task_name}_img.png'
save_hwc_tensor_as_png(image_input[0], img_path)
# 请求内容
with open(img_path, "rb") as img_file:
encoded = base64.b64encode(img_file.read()).decode('utf-8')
image_b64 = f"data:image/png;base64,{encoded}"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
payload = {
"model": "step-video",
"image_b64": image_b64,
"prompt": text_prompt,
'size': video_size,
}
# 上传请求
response_post = requests.post(
api_url,
headers=headers,
json=payload,
)
print(f'{response_post.content=}')
# 获取生成任务 task_id
response = json.loads(response_post.content)
if response['status'] == 'fail':
raise Exception('generation failed')
else:
task_id = response['task_id']
# 等待视频生成
while True:
response_get = requests.get(
f"{api_url}/{task_id}",
headers=headers,
)
print(f'{response_get.content=}')
response = json.loads(response_get.content)
if response['status'] == 'fail':
raise Exception('generation failed')
elif response['status'] == 'success':
url = response['video']['url']
break
else:
time.sleep(10)
# 下载视频
video_path = f'{task_dir}/{task_name}_vid.mp4'
self.download_video(url, save_path=video_path)
# 读取视频文件,返回形状为 (T, H, W, C) 的uint8张量
video, _, _ = read_video(video_path, pts_unit="sec") # 默认output_format="THWC"
# 转换为浮点张量并归一化到0~1范围
video_tensor = video.to(torch.float32) / 255.0
return (video_tensor,)
NODE_CLASS_MAPPINGS = {
"TI2V" : TI2V,
"TI2V_API" : TI2V_API,
}
# Optionally, you can rename the node in the `NODE_DISPLAY_NAME_MAPPINGS` dictionary.
NODE_DISPLAY_NAME_MAPPINGS = {
"TI2V": "TI2V",
"TI2V_API": "TI2V_API",
}