Commit 7e72e89e by xiaoqi

脚本

parent dbdabde0
Showing with 15 additions and 15 deletions
...@@ -15,6 +15,7 @@ from PIL import Image, ImageEnhance ...@@ -15,6 +15,7 @@ from PIL import Image, ImageEnhance
from moviepy import * from moviepy import *
from moviepy import VideoFileClip, clips_array from moviepy import VideoFileClip, clips_array
import traceback import traceback
# p_preset = "veryslow" # p_preset = "veryslow"
p_preset = "veryslow" p_preset = "veryslow"
p_bitrate = "2000k" p_bitrate = "2000k"
...@@ -116,6 +117,7 @@ def update_vf_params(): ...@@ -116,6 +117,7 @@ def update_vf_params():
p_vf = vf_params p_vf = vf_params
print(f"最终参数vf{vf_params}") print(f"最终参数vf{vf_params}")
def png2mp4(output_video, fps2, png_dir, target_fps: int = 1000): def png2mp4(output_video, fps2, png_dir, target_fps: int = 1000):
""" """
将 PNG 序列转换为 MP4 视频文件。 将 PNG 序列转换为 MP4 视频文件。
...@@ -313,7 +315,7 @@ def convert_to_grayscale(input_folder, output_folder): ...@@ -313,7 +315,7 @@ def convert_to_grayscale(input_folder, output_folder):
convert_png_alpha_2_gray(file_path, output_path) convert_png_alpha_2_gray(file_path, output_path)
def check_file_or_dir(userinput,output_file_path: str = None, output_name: str = None): def check_file_or_dir(userinput, output_file_path: str = None, output_name: str = None):
print(f"入参:userinput={userinput} output_file_path={output_file_path} output_name={output_name}") print(f"入参:userinput={userinput} output_file_path={output_file_path} output_name={output_name}")
# 检查路径是否存在 # 检查路径是否存在
if not os.path.exists(userinput): if not os.path.exists(userinput):
...@@ -327,11 +329,11 @@ def check_file_or_dir(userinput,output_file_path: str = None, output_name: str = ...@@ -327,11 +329,11 @@ def check_file_or_dir(userinput,output_file_path: str = None, output_name: str =
for dirpath, _, filenames in os.walk(userinput): for dirpath, _, filenames in os.walk(userinput):
for filename in filenames: for filename in filenames:
full_path = os.path.join(dirpath, filename) full_path = os.path.join(dirpath, filename)
files_to_process.append((full_path,output_file_path, output_name)) files_to_process.append((full_path, output_file_path, output_name))
# 如果是文件,直接添加到待处理列表中 # 如果是文件,直接添加到待处理列表中
elif os.path.isfile(userinput): elif os.path.isfile(userinput):
files_to_process.append((userinput,output_file_path, output_name)) files_to_process.append((userinput, output_file_path, output_name))
else: else:
print(f"路径既不是文件也不是目录:{userinput}") print(f"路径既不是文件也不是目录:{userinput}")
...@@ -340,12 +342,13 @@ def check_file_or_dir(userinput,output_file_path: str = None, output_name: str = ...@@ -340,12 +342,13 @@ def check_file_or_dir(userinput,output_file_path: str = None, output_name: str =
# 使用ThreadPoolExecutor来并行处理任务 # 使用ThreadPoolExecutor来并行处理任务
with ThreadPoolExecutor(max_workers=8) as executor: with ThreadPoolExecutor(max_workers=8) as executor:
# 提交所有任务 # 提交所有任务
futures = {executor.submit(process_webp_file, file_path, output_file_path,out_name): (file_path,output_file_path, out_name) for futures = {executor.submit(process_webp_file, file_path, output_file_path, out_name): (
file_path,output_file_path, out_name in files_to_process} file_path, output_file_path, out_name) for
file_path, output_file_path, out_name in files_to_process}
# 获取已完成的任务结果 # 获取已完成的任务结果
for future in as_completed(futures): for future in as_completed(futures):
file_path, output_file_path,out_name = futures[future] file_path, output_file_path, out_name = futures[future]
try: try:
future.result() # 这里可以获取process_webp_file的返回值(如果有) future.result() # 这里可以获取process_webp_file的返回值(如果有)
except Exception as exc: except Exception as exc:
...@@ -354,13 +357,10 @@ def check_file_or_dir(userinput,output_file_path: str = None, output_name: str = ...@@ -354,13 +357,10 @@ def check_file_or_dir(userinput,output_file_path: str = None, output_name: str =
traceback.print_exc() traceback.print_exc()
def process_webp_file(file_path, output_file_path: str = None, output_name: str = None):
def process_webp_file(file_path, output_file_path: str = None,output_name: str = None):
if is_webp_animator(file_path): if is_webp_animator(file_path):
print(f"正在处理 WebP 文件:{file_path}") print(f"正在处理 WebP 文件:{file_path}")
webp2mp4core(file_path, output_file_path=output_file_path,output_name=output_name) webp2mp4core(file_path, output_file_path=output_file_path, output_name=output_name)
else: else:
print(f"跳过非 WebP 文件:{file_path}") print(f"跳过非 WebP 文件:{file_path}")
...@@ -387,7 +387,7 @@ def get_total_size(input_webp_path_str): ...@@ -387,7 +387,7 @@ def get_total_size(input_webp_path_str):
# 原始webp文件的路径对象 # 原始webp文件的路径对象
webp_path = Path(input_webp_path_str) webp_path = Path(input_webp_path_str)
# 最终合并视频的输出路径保持不变,仍然位于原始webp文件所在目录 # 最终合并视频的输出路径保持不变,仍然位于原始webp文件所在目录
output_merge_video_path = f"{webp_path.parent}/{webp_path.stem}.mp4" output_merge_video_path = f"{webp_path.parent}/{webp_path.stem}.mp4"
if Path(output_merge_video_path).exists(): if Path(output_merge_video_path).exists():
# 如果mp4文件已经生成,则获取其大小并累加到总大小中 # 如果mp4文件已经生成,则获取其大小并累加到总大小中
...@@ -412,7 +412,7 @@ def webp2mp4core(input_webp_path_str, output_file_path: str = None, output_name: ...@@ -412,7 +412,7 @@ def webp2mp4core(input_webp_path_str, output_file_path: str = None, output_name:
if output_name is None: if output_name is None:
output_merge_video_path = f"{mp4_output_path}/{webp_path.stem}.mp4" output_merge_video_path = f"{mp4_output_path}/{webp_path.stem}.mp4"
else: else:
output_merge_video_path = f"{mp4_output_path}/{webp_path.stem}-{output_name}.mp4" output_merge_video_path = f"{mp4_output_path}/{webp_path.stem}-{output_name}.mp4"
if Path(output_merge_video_path).exists(): if Path(output_merge_video_path).exists():
print(f"文件{output_merge_video_path}已存在跳过生成") print(f"文件{output_merge_video_path}已存在跳过生成")
return return
...@@ -435,8 +435,8 @@ def webp2mp4core(input_webp_path_str, output_file_path: str = None, output_name: ...@@ -435,8 +435,8 @@ def webp2mp4core(input_webp_path_str, output_file_path: str = None, output_name:
# 获取fps并转换视频 # 获取fps并转换视频
fps = get_fps(input_webp_path_str) fps = get_fps(input_webp_path_str)
global p_vf global p_vf
if fps < 12: global p_fps
global p_fps if fps < 12 and p_fps <= 0:
p_fps = 12 p_fps = 12
update_vf_params() update_vf_params()
print(f"获取到当前webp的帧数是{fps}") print(f"获取到当前webp的帧数是{fps}")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment