修改对输出文件的命名获取第一个文件名从第一个英文字母开始截取不包括最后一个字母和后缀如果文件名是以h_开头的则从第二个英文字母开始截取不包括最后一个字母和后缀:import osimport globfrom natsort import natsorteddir_path = homedownloadsoutput_dir = home115up# 当前文件夹下的所有mp4路径L = for r
修改后的代码如下:
import os import glob from natsort import natsorted
dir_path = '/home/downloads' output_dir = '/home/115/up'
当前文件夹下的所有mp4路径
L = [] 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: ts_file = os.path.join(os.path.dirname(l), os.path.splitext(os.path.basename(l))[0] + '.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), os.path.splitext(os.path.basename(l))[0] + ".ts") for l in L])}" ' '-acodec copy -vcodec copy -bsf:a aac_adtstoasc ' )
修改输出文件名
first_file = os.path.basename(L[0]) if first_file.startswith('h_'): new_file_name = first_file[2:-6].capitalize() + '.mp4' else: new_file_name = first_file[:-6].capitalize() + '.mp4'
output_file = os.path.join(output_dir, new_file_name) concat_command += f'"{output_file}"'
执行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)
修改输出文件名
new_file_name = os.path.splitext(new_file_name)[0].lstrip('0123456789') for mp4_file in glob.glob(os.path.join(output_dir, '*.mp4')): os.rename(mp4_file, os.path.join(output_dir, new_file_name + os.path.splitext(mp4_file)[1])
原文地址: https://www.cveoy.top/t/topic/dcCV 著作权归作者所有。请勿转载和采集!