import os import glob from natsort import natsorted

当前文件夹下的所有mp4路径

L = [] dir_path = '/home/downloads' for root, dirs, files in os.walk(dir_path): files = natsorted(files) for file in files: if os.path.splitext(file)[1] == '.mp4': file_path = os.path.join(root, file) L.append(file_path)

生成ffmpeg命令

commands = [] for l in L: new_name = ''.join([i for i in os.path.splitext(os.path.basename(l))[0] if not i.isdigit()]) ts_file = os.path.join(os.path.dirname(l), new_name + '.ts') commands.append( f"ffmpeg -i "{l}" -vcodec copy -acodec copy -bsf:v h264_mp4toannexb "{ts_file}"")

concat_command = ( f'ffmpeg -i "concat:{"|".join([os.path.join(os.path.dirname(l), "".join([i for i in os.path.splitext(os.path.basename(l))[0] if not i.isdigit()]) + ".ts") for l in L])}" ' '-acodec copy -vcodec copy -bsf:a aac_adtstoasc ' f'"{os.path.join(dir_path, "".join([i for i in os.path.splitext(os.path.basename(L[0]))[0] if not i.isdigit()])[1:].upper() + ".mp4")}"' )

执行ffmpeg命令

for command in commands: os.system(command)

os.system(concat_command)

删除ts文件

for ts_file in glob.glob(os.path.join(dir_path, '*.ts')): os.remove(ts_file

修改下面python3代码首先对目录下文件重新命名删除文件名中第一个英文字母之前的数字然后再依次执行ffmpeg命令最后检查语法错误输出完整python3代码import osimport globfrom natsort import natsorted# 当前文件夹下的所有mp4路径L = dir_path = homedownloadsfor root dirs files in oswal

原文地址: https://www.cveoy.top/t/topic/c3pZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录