写了个脚本如下
from traceback import format_exc
import subprocess
import os
from multiprocessing import Pool
from psutil import cpu_percent
import locale
from time import sleep
import sys
def main():
print('功能列表:\n1、MP4转GIF')
choice = input('功能选择:\n')
try:
if choice == '1':
mp4togif()
except BaseException:
print(format_exc())
morework = input('是否继续运行:\n1、是 2、否\n')
if morework == '1':
main()
elif morework == '2':
input('输入回车可退出程序\n')
sys.exit()
def mp4togif():
location = input('请输入MP4文件路径:\n')
mp4files = []
for root, dirs, files in os.walk(location):
for file in files:
if file.endswith('.mp4'):
mp4files.append(os.path.join(root, file))
cpu_cores = os.cpu_count() # 检查CPU核心数
pool = Pool(cpu_cores - 1) # 根据CPU核心数创建进程池
results = []
errors = []
for mp4file in mp4files:
while True:
if cpu_percent() < 75: # CPU占用率超过75%时停发任务,避免卡死
result = pool.apply_async(func=mp4togif_single, args=(mp4file,), callback=wait)
results.append(result)
break
else:
sleep(1)
pool.close()
pool.join() # 主进程阻塞,等待子进程全部完成
for result in results: # 提取所有报错,进程传参需要格式转换
error = result.get()
if error is not None:
errors.append(error)
# 设置本地化信息为中国(或者其他适合的本地化信息)
locale.setlocale(locale.LC_ALL, 'zh_CN.UTF-8')
# 根据本地化信息排序汉字列表
errors = sorted(errors, key=locale.strxfrm)
# 反馈异常情况
for error in errors:
print(error)
return
def mp4togif_single(mp4file):
print(11)
fps = 15
error = None
try:
while True:
ffmpeg_command = [
'D:\\11190\\Documents\\PycharmProjects\\H\\ffmpeg-6.0-essentials_build\\bin\\ffmpeg.exe',
'-y',
'-i', mp4file,
'-vf',
f'fps={fps},scale=500:300:force_original_aspect_ratio=decrease,pad=500:300:(ow-iw)/2:(oh-ih)/2,split[s0][s1];[s0]palettegen=stats_mode=diff[p];[s1][p]paletteuse=dither=sierra2_4a',
'-c:v', 'gif',
'-pix_fmt', 'pal8',
'-loop', '0',
mp4file[0:-3] + 'gif'
]
subprocess.call(ffmpeg_command)
file_size = os.path.getsize(mp4file[0:-3] + 'gif')
if file_size > 10485760 and fps > 10:
fps = fps - 1
continue
else:
break
except BaseException:
error = format_exc()
return error
def wait(result):
pass
if __name__ == '__main__':
main()
在pycharm里运行正常,用pyinstaller打包后,再运行exe文件,指定到输入路径那一步后,程序开始逻辑错误,将main放进进程池跑路好几次,如下图所示,试过其他方式打包,bug一样
可以帮我看看怎么回事吗