import os
from moviepy.editor import VideoFileClip, concatenate_videoclips
from concurrent.futures import ThreadPoolExecutor
def merge_videos_in_folder(folder_path):
# 获取所有 mp4 文件的路径
video_files = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith('.mp4')]
# 如果没有找到任何视频文件,则退出
if not video_files:
print(f"No MP4 files found in {folder_path}")
return
# 读取所有视频片段
clips = [VideoFileClip(f) for f in video_files]
# 合并视频片段
final_clip = concatenate_videoclips(clips, method="compose")
# 创建输出文件路径
output_path = os.path.join(folder_path, "merged_video.mp4")
# 写入合并后的视频
final_clip.write_videofile(output_path, codec='libx264')
# 释放资源
for clip in clips:
clip.close()
print(f"Merged videos saved to {output_path}")
def main():
# 指定目录
base_dir = "test"
# 遍历所有子目录
subdirs = [d for d in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, d))]
# 使用多线程处理每个子目录
with ThreadPoolExecutor(max_workers=100) as executor:
futures = [executor.submit(merge_videos_in_folder, os.path.join(base_dir, subdir)) for subdir in subdirs]
# 等待所有任务完成
for future in futures:
future.result()
if __name__ == "__main__":
main()
合并视频python
最新推荐文章于 2024-09-09 00:00:00 发布