Python3代码:将目录下的mp4文件转换为ts并合并成一个mp4文件
import os from natsort import natsorted
获取当前文件所在目录
dir_path = os.path.dirname(os.path.abspath(file))
存储当前文件夹下的所有mp4文件路径
L = [] for root, dirs, files in os.walk(os.path.join(dir_path, 'up')): 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: commands.append(f"ffmpeg -i '{l}' -vcodec copy -acodec copy -bsf:v h264_mp4toannexb '{os.path.join(dir_path, 'up', os.path.splitext(os.path.basename(l))[0] + '.ts')}' ")
concat_command = f'ffmpeg -i 'concat:{'|'.join([os.path.join(dir_path, "up", os.path.splitext(os.path.basename(i))[0] + ".ts") for i 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, "up", "*.ts")}'
执行ffmpeg命令
for command in commands: os.system(command)
os.system(concat_command) os.system(rm_command)
原文地址: https://www.cveoy.top/t/topic/nzIQ 著作权归作者所有。请勿转载和采集!