Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
xiaoqi
/
mp4giftconvert
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
7e72e89e
authored
Apr 18, 2025
by
xiaoqi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
脚本
parent
dbdabde0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
15 deletions
convert_webp_2_mp4.py
convert_webp_2_mp4.py
View file @
7e72e89e
...
...
@@ -15,6 +15,7 @@ from PIL import Image, ImageEnhance
from
moviepy
import
*
from
moviepy
import
VideoFileClip
,
clips_array
import
traceback
# p_preset = "veryslow"
p_preset
=
"veryslow"
p_bitrate
=
"2000k"
...
...
@@ -116,6 +117,7 @@ def update_vf_params():
p_vf
=
vf_params
print
(
f
"最终参数vf{vf_params}"
)
def
png2mp4
(
output_video
,
fps2
,
png_dir
,
target_fps
:
int
=
1000
):
"""
将 PNG 序列转换为 MP4 视频文件。
...
...
@@ -313,7 +315,7 @@ def convert_to_grayscale(input_folder, output_folder):
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}"
)
# 检查路径是否存在
if
not
os
.
path
.
exists
(
userinput
):
...
...
@@ -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
filename
in
filenames
:
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
):
files_to_process
.
append
((
userinput
,
output_file_path
,
output_name
))
files_to_process
.
append
((
userinput
,
output_file_path
,
output_name
))
else
:
print
(
f
"路径既不是文件也不是目录:{userinput}"
)
...
...
@@ -340,12 +342,13 @@ def check_file_or_dir(userinput,output_file_path: str = None, output_name: str =
# 使用ThreadPoolExecutor来并行处理任务
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
file_path
,
output_file_path
,
out_name
in
files_to_process
}
futures
=
{
executor
.
submit
(
process_webp_file
,
file_path
,
output_file_path
,
out_name
):
(
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
):
file_path
,
output_file_path
,
out_name
=
futures
[
future
]
file_path
,
output_file_path
,
out_name
=
futures
[
future
]
try
:
future
.
result
()
# 这里可以获取process_webp_file的返回值(如果有)
except
Exception
as
exc
:
...
...
@@ -354,13 +357,10 @@ def check_file_or_dir(userinput,output_file_path: str = None, output_name: str =
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
):
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
:
print
(
f
"跳过非 WebP 文件:{file_path}"
)
...
...
@@ -387,7 +387,7 @@ def get_total_size(input_webp_path_str):
# 原始webp文件的路径对象
webp_path
=
Path
(
input_webp_path_str
)
# 最终合并视频的输出路径保持不变,仍然位于原始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
():
# 如果mp4文件已经生成,则获取其大小并累加到总大小中
...
...
@@ -412,7 +412,7 @@ def webp2mp4core(input_webp_path_str, output_file_path: str = None, output_name:
if
output_name
is
None
:
output_merge_video_path
=
f
"{mp4_output_path}/{webp_path.stem}.mp4"
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
():
print
(
f
"文件{output_merge_video_path}已存在跳过生成"
)
return
...
...
@@ -435,8 +435,8 @@ def webp2mp4core(input_webp_path_str, output_file_path: str = None, output_name:
# 获取fps并转换视频
fps
=
get_fps
(
input_webp_path_str
)
global
p_vf
if
fps
<
12
:
global
p_fps
global
p_fps
if
fps
<
12
and
p_fps
<=
0
:
p_fps
=
12
update_vf_params
()
print
(
f
"获取到当前webp的帧数是{fps}"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment