V 2.2.6 增加了MJ的api

This commit is contained in:
2024-06-01 15:08:22 +08:00
parent 941a86d07a
commit 8b011856c2
52 changed files with 3255 additions and 2376 deletions
+11 -3
View File
@@ -11,7 +11,15 @@ import shotSplit
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
sys.argv = ["C:\\Users\\27698\\Desktop\\LAITool\\resources\\scripts\\Lai.exe","-c","C:/Users/27698/Desktop/测试/mjTest/scripts/output_crop_00001.json"]
# 判断sys.argv 的长度,如果小于2,说明没有传入参数,设置初始参数
if len(sys.argv) < 2:
sys.argv = [
"C:\\Users\\27698\\Desktop\\LAITool\\resources\\scripts\\Lai.exe",
"-c",
"C:/Users/27698/Desktop/测试/mjTest/scripts/output_crop_00001.json",
"NVIDIA",
]
print(sys.argv)
if len(sys.argv) < 2:
@@ -46,7 +54,7 @@ set_ffmpeg_env()
# 执行剪辑的方法
if sys.argv[1] == "-c":
clip = clip.Clip(cript_directory, sys.argv[2])
clip = clip.Clip(cript_directory, sys.argv[2], sys.argv[3])
clip.MergeVideosAndClip()
pass
# 获取字体
@@ -79,4 +87,4 @@ elif sys.argv[1] == "-k":
# 智能分镜。字幕识别
elif sys.argv[1] == "-a":
print("开始算法分镜:" + sys.argv[2] + " -- 输出文件夹:" + sys.argv[3])
shotSplit.init(sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])
shotSplit.init(sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6])
Binary file not shown.
+77 -56
View File
@@ -21,7 +21,7 @@ class Clip:
剪辑合并的类
"""
def __init__(self, cript_directory, config_path) -> None:
def __init__(self, cript_directory, config_path, gpu_type) -> None:
self.audio_duration = None
self.cript_directory = cript_directory
self.ID = str(uuid.uuid4())
@@ -31,6 +31,7 @@ class Clip:
self.TEMP_FOLDER, self.ASS_ID + ".ass"
).replace("\\", "/")
self.config_path = config_path
self.gpu_type = gpu_type
self.ffmpeg_path = (
"../package/ffmpeg-2023-12-07-git-f89cff96d0-full_build/bin/ffmpeg"
)
@@ -68,7 +69,7 @@ class Clip:
self.bitRate = self.config_json["bitRate"]
# 图片数据
self.iamge_to_video = iamge_to_video.ImageToVideo()
self.iamge_to_video = iamge_to_video.ImageToVideo(self.gpu_type)
self.iamge_to_video.fps = self.config_json["frameRate"]
self.iamge_to_video.video_size = (
self.video_resolution_x,
@@ -236,32 +237,46 @@ class Clip:
# 添加txtlist表
txt_list.append(f"file '{os.path.abspath(output_file)}'")
# 删除开头,结尾,镜像,加速,放大
command = []
command.append(self.ffmpeg_path)
if self.gpu_type == "NVIDIA":
command.append("-hwaccel")
command.append("cuda") # 启用 CUDA 硬件加速
command.append("-c:v")
command.append("h264_cuvid") # 使用 NVIDIA CUVID 解码器进行解码
elif self.gpu_type == "AMD":
command.append("-hwaccel")
command.append("vaapi")
command.append("-c:v")
command.append("h264_vaapi")
command.append("-i")
command.append(random_path)
command.append("-ss")
command.append(str(start_time))
command.append("-t")
command.append(str(duration))
command.append("-vf")
command.append(
"hflip,setpts=1*PTS,format=yuv420p,scale=iw*1.1:ih*1.1,crop=iw/1.1:ih/1.1"
)
command.append("-an")
command.append("-b:v")
command.append("5000k")
command.append("-c:v")
if self.gpu_type == "NVIDIA":
command.append("h264_nvenc")
elif self.gpu_type == "AMD":
command.append("h264_vaapi")
else:
command.append("libx264")
command.append("-preset")
command.append("fast")
command.append("-loglevel")
command.append("error")
command.append(output_file)
subprocess.run(
[
self.ffmpeg_path,
"-hwaccel",
"cuda", # 启用 CUDA 硬件加速
"-c:v",
"h264_cuvid", # 使用 NVIDIA CUVID 解码器进行解码
"-i",
random_path,
"-ss",
str(start_time),
"-t",
str(duration),
"-vf",
"hflip,setpts=1*PTS,format=yuv420p,scale=iw*1.1:ih*1.1,crop=iw/1.1:ih/1.1",
"-an",
"-b:v",
"5000k",
"-c:v",
"h264_nvenc",
"-preset",
"fast",
"-loglevel",
"error",
output_file,
],
command,
check=True,
stderr=subprocess.PIPE,
)
@@ -453,35 +468,41 @@ class Clip:
# 合并视频并添加音乐和字幕
def MergeVideoAndAudio(self):
command = [
self.ffmpeg_path,
"-f",
"concat",
"-safe",
"0",
"-i",
self.mp4_file_txt,
"-i",
self.mix_audio,
"-vf",
f"subtitles=./Temp/{self.ID}/{self.ASS_ID}.ass",
# f"subtitles= {ASS_FILE_PATH}",
"-c:v",
"h264_nvenc",
"-preset",
"fast",
"-rc:v",
"cbr",
"-b:v",
str(self.bitRate) + "k",
"-c:a",
"aac",
"-strict",
"-2",
"-loglevel",
"error",
self.outpue_file,
]
command = []
command.append(self.ffmpeg_path)
command.append("-f")
command.append("concat")
command.append("-safe")
command.append("0")
command.append("-i")
command.append(self.mp4_file_txt)
command.append("-i")
command.append(self.mix_audio)
command.append("-vf")
command.append(f"subtitles=./Temp/{self.ID}/{self.ASS_ID}.ass")
command.append("-c:v")
if self.gpu_type == "NVIDIA":
command.append("h264_nvenc")
elif self.gpu_type == "AMD":
command.append("h264_vaapi")
else:
command.append("libx264")
command.append("-preset")
command.append("fast")
command.append("-rc:v")
command.append("cbr")
command.append("-b:v")
command.append(str(self.bitRate) + "k")
command.append("-c:a")
command.append("aac")
command.append("-strict")
command.append("-2")
command.append("-loglevel")
command.append("error")
command.append(self.outpue_file)
subprocess.run(command, check=True, stderr=subprocess.PIPE)
# subprocess.run(command)
pass
+30 -21
View File
@@ -9,8 +9,9 @@ import json
class ImageToVideo:
def __init__(self) -> None:
def __init__(self, gpu_type) -> None:
self.frames = 0
self.gpu_type = gpu_type
self.public_tools = public_tools.PublicTools()
self.ffmpeg_path = (
"../package/ffmpeg-2023-12-07-git-f89cff96d0-full_build/bin/ffmpeg"
@@ -202,7 +203,7 @@ class ImageToVideo:
src_y1 = max(-start_y, 0)
dst_y1 = max(start_y, 0)
copy_height = min(img_resized.shape[0] - src_y1, video_size[1] - dst_y1)
# copy_width = min(copy_width, img_resized.shape[1])
if copy_height > 0 and copy_width > 0:
@@ -455,29 +456,37 @@ class ImageToVideo:
)
temp_mp4_path = os.path.join(image_dir, "temp_" + str(number) + ".mp4")
# 开始微调
cmd = [
self.ffmpeg_path,
"-i",
mp4_path,
"-filter:v",
cmd = []
cmd.append(self.ffmpeg_path)
cmd.append("-i")
cmd.append(mp4_path)
cmd.append("-filter:v")
cmd.append(
"setpts=PTS*"
+ str(
(filtered_data[0]["end_time"] - filtered_data[0]["start_time"])
/ duration_ms
),
"-c:v",
"h264_nvenc",
"-preset",
"fast",
"-rc:v",
"cbr",
"-b:v",
str(self.bitRate) + "k",
temp_mp4_path,
"-loglevel",
"error",
"-an",
]
)
)
cmd.append("-c:v")
if self.gpu_type == "NVIDIA":
cmd.append("h264_nvenc")
elif self.gpu_type == "AMD":
cmd.append("h264_vaapi")
else:
cmd.append("libx264")
cmd.append("-preset")
cmd.append("fast")
cmd.append("-rc:v")
cmd.append("cbr")
cmd.append("-b:v")
cmd.append(str(self.bitRate) + "k")
cmd.append(temp_mp4_path)
cmd.append("-loglevel")
cmd.append("error")
cmd.append("-an")
subprocess.run(cmd, check=True)
os.remove(mp4_path)
+31 -21
View File
@@ -62,7 +62,7 @@ def createDir(file_dir):
# 切分一个视频
def ClipVideo(video_path, out_folder, image_out_folder, sensitivity):
def ClipVideo(video_path, out_folder, image_out_folder, sensitivity, gpu_type):
shijian_list = find_scenes(video_path, sensitivity) # 多组时间列表
shijian_list_len = len(shijian_list)
@@ -87,25 +87,33 @@ def ClipVideo(video_path, out_folder, image_out_folder, sensitivity):
)
# 使用 ffmpeg 裁剪视频
command = []
command.append("ffmpeg")
command.append("-i")
command.append(video_path)
command.append("-ss")
command.append(start_time_str)
command.append("-to")
command.append(end_time_str)
command.append("-c:v")
if gpu_type == "NVIDIA":
command.append("h264_nvenc")
elif gpu_type == "AMD":
command.append("h264_vaapi")
else:
command.append("libx264")
command.append("-preset")
command.append("fast")
command.append("-c:a")
command.append("copy")
command.append(out_video_file)
command.append("-loglevel")
command.append("error")
subprocess.run(
[
"ffmpeg",
"-i",
video_path,
"-ss",
start_time_str,
"-to",
end_time_str,
"-c:v",
"h264_nvenc",
"-preset",
"fast",
"-c:a",
"copy",
out_video_file,
"-loglevel",
"error",
],
command,
check=True,
stderr=subprocess.PIPE,
)
@@ -220,8 +228,10 @@ def GetText(out_folder, mp3_list):
sys.stdout.flush()
def init(video_path, video_out_folder, image_out_folder, sensitivity):
v_l = ClipVideo(video_path, video_out_folder, image_out_folder, sensitivity)
def init(video_path, video_out_folder, image_out_folder, sensitivity, gpu_type):
v_l = ClipVideo(
video_path, video_out_folder, image_out_folder, sensitivity, gpu_type
)
# 开始分离音频
m_l = SplitAudio(video_out_folder, v_l)