先给出代码,大佬们看看,能否优化一下,或者改写成批处理的bat脚本文件也行
[Python] 纯文本查看 复制代码 # encoding=utf-8
import os
import itertools
def randomMerge(ffmpeg_path, videos_path, concat_list_path):
# 定义一个数组
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)
# 计算排列组合(两两组合)
pp = list(itertools.permutations(list(L), 2))
total = len(pp)
for i in range(0, total):
# print(pp[i])
concat_file_path = concat_list_path + str(i) + ".txt"
# print(concat_file_path)
with open(concat_file_path, 'a', encoding='utf-8') as f:
video_one = "file " + "'" + pp[i][0] + "'" + "\n"
video_two = "file " + "'" + pp[i][1] + "'" + "\n"
f.write(video_one)
f.write(video_two)
result_file_path = videos_path + "\\random\\" + str(i) + ".mp4"
# print(result_file_path)
# 拼接好运行ffmpeg的命令行语句
cmd = ffmpeg_path + " -f concat -safe 0 -i " + concat_file_path + " -c copy " + result_file_path
# print(cmd)
# 调用cmd命令行执行ffmpeg拼接视频
os.popen(cmd)
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\\"
mp4_flie_path = videos_path + "\\mp4"
# 从多个视频中,每次选2个视频合并,两两组合
# randomMerge(ffmpeg_path, videos_path, concat_list_path)
if __name__ == '__main__':
main()
|