# 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)
# 把要合并的视频文件写入txt中
concat_file_path = concat_list_path + "concat_list.txt"
print(concat_file_path)
total = len(L)
for i in range(0,total):
print(L[i])
with open(concat_file_path, 'a', encoding='utf-8') as f:
video_file = "file " + "'" + L[i] + "'" + "\n"
f.write(video_file)
result_file_path = videos_path + "\\" + "merge.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)
if __name__ == '__main__':
main()
03-26
894

06-13
1053

04-23
3244
