请对代码单独增加对output_dir目录下文件名处理:如果命名中包含h_则删除h_请确保_也可以被移除import osimport globfrom natsort import natsorteddir_path = rEhomeupoutput_dir = rEhomeup1# 当前文件夹下的所有mp4路径L = for root dirs files in oswalkdir_path
修改后的代码如下:
import os import glob from natsort import natsorted
dir_path = r'E:\home\up' output_dir = r'E:\home\up\1'
当前文件夹下的所有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}"")
获取文件名 + '.mp4'
new_file_name = os.path.splitext(os.path.basename(L[0]))[0].replace('h_','') + '.mp4' 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 ' f'"{os.path.join(output_dir, new_file_name)}"' )
执行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)
输出文件命名
for mp4_file in glob.glob(os.path.join(output_dir, '*.mp4')): file_name = os.path.splitext(os.path.basename(mp4_file))[0].replace('h_','') + '.mp4' os.rename(mp4_file, os.path.join(output_dir, file_name)
原文地址: http://www.cveoy.top/t/topic/dd3c 著作权归作者所有。请勿转载和采集!