# encoding=utf-8 import os import itertools def main(): #使用前,要先配置好ffmpeg的环境变量,并删除videos_path中txt文件夹下的所有文件 ffmpeg_path = "D:\\FFmpeg\\bin\\ffmpeg" videos_path = "C:\\Users\\Yan\\Desktop\\videos" concat_list_path = videos_path + "\\txt\\" # 定义一个数组 L = [] # 访问 videos 文件夹 (假设视频都放在这里面) for root, dirs, files in os.walk(videos_path): # 按文件名排序 files.sort() # 遍历所有文件 for file in files: # 如果后缀名为 .mp4 if os.path.splitext(file)[1] == '.mp4': # 拼接成完整路径 filePath = os.path.join(root, file) # 添加到数组 L.append(filePath) ts_file_path = videos_path + "\\ts" total = len(L) for i in range(0, total): video_name = os.path.basename(L[i]) # 去掉文件名前面的文件路径 ts_name = os.path.splitext(video_name)[0] # 去掉文件名的后缀 # 拼接好运行ffmpeg的命令行语句 cmd = ffmpeg_path + " -i " + L[i] + " -y -codec copy -map 0 -f segment -segment_time 4 -q:v 1 " + ts_file_path + "\\" + ts_name + "-%03d.ts" print(cmd) # 调用cmd命令行执行ffmpeg切片视频 os.popen(cmd) if __name__ == '__main__': main()