Python3 代码使用 ffmpeg 删除 ts 文件,无法正确选择目录的解决方法
import os from natsort import natsorted
存储当前文件夹下的所有 mp4 文件路径
L = [] dir_path = '/home/115/up' for root, dirs, files in os.walk(dir_path): files = natsorted(files) for file in files: # 只处理 mp4 文件 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.basename(l).replace('.mp4', '.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.basename(l).replace('.mp4', '.ts')) for l in L])}' -acodec copy -vcodec copy -bsf:a aac_adtstoasc '{os.path.join(dir_path, 'output.mp4')}'
rm_command = f'rm '{os.path.join(dir_path, '*.ts')}'
执行 ffmpeg 命令
for command in commands: os.system(command)
os.system(concat_command) os.system(rm_command)
原文地址: https://www.cveoy.top/t/topic/nzJu 著作权归作者所有。请勿转载和采集!