1 Star 0 Fork 15

雪狐/ComfyUI_Lam_1

forked from yanlang0123/ComfyUI_Lam 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
LoadVideo.py 3.31 KB
一键复制 编辑 原始数据 按行查看 历史
马噥 提交于 2024-01-19 15:24 +08:00 . 优化
import folder_paths
import cv2
import os
import torch
import numpy as np
class LamLoadVideo:
def __init__(self):
self.output_dir = os.path.join(folder_paths.get_output_directory(), 'autio')
if not os.path.exists(self.output_dir):
os .makedirs(self.output_dir)
@classmethod
def INPUT_TYPES(s):
input_dir = os.path.join(folder_paths.get_input_directory(), 'video')
if not os.path.exists(input_dir):
os .makedirs(input_dir)
files = [f for f in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir, f))]
return {"required":
{
"videoPath": ("STRING", {"forceInput": True}),
"sample_start_idx": ("INT", {"default": 1, "min": 1, "max": 10000}),
"n_sample_frames": ("INT", {"default": 0, "min": 0, "max": 100000}),
"extract_audio": ([True,False], ),
"filename_prefix": ("STRING", {"default": "comfyUI"}),
},
}
CATEGORY = "lam"
RETURN_TYPES = ("IMAGE","INT","INT","STRING",)
RETURN_NAMES = ("图片","帧率","总帧数","音频文件名",)
FUNCTION = "load_image"
OUTPUT_NODE = True
def load_image(self, videoPath,sample_start_idx,n_sample_frames,extract_audio,filename_prefix):
if os.path.exists(videoPath) == False:
raise Exception('视频文件不存在')
cap = cv2.VideoCapture(videoPath)
fps = cap.get(cv2.CAP_PROP_FPS)
frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
flag = cap.isOpened()
if not flag:
print("\033[31mLine 65 error\033[31m: open" + videoPath + "error!")
sample_frames = []
count=0
while True:
flag, image = cap.read()
if not flag: # 如果已经读取到最后一帧则退出
break
count+=1
if sample_start_idx>count:
continue
if n_sample_frames>0:
if (sample_start_idx+n_sample_frames)<=count:
break
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
image = image.squeeze()
sample_frames.append(image)
cap.release() # 释放资源
filePathName=''
if extract_audio:
full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(filename_prefix, self.output_dir)
file = f"{filename}_{counter:05}_.mp3"
filePathName=os.path.join(full_output_folder, file)
#提取音频
cmd = r"ffmpeg -y -hide_banner -loglevel error -i %s %s"%(videoPath, filePathName)
os.system(cmd)
return {"ui": {"text": "音频提取成功,保存路径:"+filePathName if extract_audio else '需要提取音频请设置extract_audio为True'}, "result": (torch.stack(sample_frames),fps,frames,filePathName,)}
NODE_CLASS_MAPPINGS = {
"LamLoadVideo": LamLoadVideo
}
# A dictionary that contains the friendly/humanly readable titles for the nodes
NODE_DISPLAY_NAME_MAPPINGS = {
"LamLoadVideo": "视频加载"
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/assassindesign/ComfyUI_Lam_1.git
git@gitee.com:assassindesign/ComfyUI_Lam_1.git
assassindesign
ComfyUI_Lam_1
ComfyUI_Lam_1
cu121

搜索帮助