125 lines
3.6 KiB
Python
125 lines
3.6 KiB
Python
import glob
|
|
import json
|
|
import os
|
|
import shutil
|
|
import traceback
|
|
import uuid
|
|
from datetime import datetime
|
|
|
|
import ffmpy
|
|
|
|
|
|
class VideoCut:
|
|
"""FFMPEG视频剪辑 -- !有卡顿问题 暂废弃"""
|
|
|
|
@classmethod
|
|
def INPUT_TYPES(s):
|
|
return {
|
|
"required": {
|
|
"config": ("STRING",),
|
|
"video_path": ("STRING",),
|
|
"mod": ("INT",),
|
|
"fps": ("FLOAT",),
|
|
"period_length": (
|
|
"INT",
|
|
{
|
|
"default": 10,
|
|
"min": 4,
|
|
"max": 100,
|
|
"step": 1,
|
|
"forceInput": True,
|
|
},
|
|
),
|
|
},
|
|
}
|
|
|
|
RETURN_TYPES = ("STRING",)
|
|
RETURN_NAMES = ("视频路径",)
|
|
|
|
FUNCTION = "cut"
|
|
|
|
# OUTPUT_NODE = False
|
|
|
|
CATEGORY = "不忘科技-自定义节点🚩"
|
|
|
|
def cut(self, config, video_path, mod, fps, period_length):
|
|
# 原文件名
|
|
origin_fname = ".".join(video_path.split(os.sep)[-1].split(".")[:-1])
|
|
# 配置获取
|
|
mul = mod / fps
|
|
print("fps", fps)
|
|
config = json.loads(config)
|
|
if len(config.keys()) == 0:
|
|
return ("无法生成符合要求的片段",)
|
|
start, end = config["start"], config["end"]
|
|
# 新文件名 复制改名适配ffmpeg
|
|
uid = uuid.uuid1()
|
|
temp_fname = os.sep.join(
|
|
[
|
|
*video_path.split(os.sep)[:-1],
|
|
"%s.%s" % (str(uid), video_path.split(".")[-1]),
|
|
]
|
|
)
|
|
try:
|
|
shutil.copy(video_path, temp_fname)
|
|
except:
|
|
return ("请检查输入文件权限",)
|
|
video_path = temp_fname
|
|
# 组装输出文件名
|
|
output_name = ".".join(
|
|
[
|
|
*video_path.split(os.sep)[-1].split(".")[:-2],
|
|
video_path.split(os.sep)[-1].split(".")[-2]
|
|
+ "_output_%%03d_%s" % datetime.now().strftime("%Y%m%d_%H%M%S"),
|
|
video_path.split(os.sep)[-1].split(".")[-1],
|
|
]
|
|
)
|
|
output = (
|
|
os.sep.join([*video_path.split(os.sep)[:-1], output_name])
|
|
.replace(
|
|
os.sep.join(["ComfyUI", "input"]), os.sep.join(["ComfyUI", "output"])
|
|
)
|
|
.replace(" ", "")
|
|
)
|
|
# 调用ffmpeg
|
|
ff = ffmpy.FFmpeg(
|
|
inputs={video_path: ["-accurate_seek"]},
|
|
outputs={
|
|
output: [
|
|
"-f",
|
|
"segment",
|
|
"-ss",
|
|
str(round(start * mul, 3)),
|
|
"-to",
|
|
str(round(end * mul, 3)),
|
|
"-segment_times",
|
|
str(period_length),
|
|
"-c",
|
|
"copy",
|
|
"-map",
|
|
"0",
|
|
"-avoid_negative_ts",
|
|
"1",
|
|
]
|
|
},
|
|
)
|
|
print(ff.cmd)
|
|
ff.run()
|
|
# uuid填充改回原文件名
|
|
try:
|
|
os.remove(temp_fname)
|
|
except:
|
|
pass
|
|
try:
|
|
files = glob.glob(output.replace("%03d", "*"))
|
|
for file in files:
|
|
shutil.move(file, file.replace(str(uid), origin_fname))
|
|
files = glob.glob(
|
|
output.replace(str(uid), origin_fname).replace("%03d", "*")
|
|
)
|
|
return (str(files),)
|
|
except:
|
|
files = glob.glob(output.replace("%03d", "*"))
|
|
traceback.print_exc()
|
|
return (str(files),)
|