| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- import os
- from moviepy.editor import VideoFileClip, concatenate_videoclips
- def concat_videos(clips_path, output_path):
- """
- 拼接多个视频文件为一个视频。
- :param clips_path: 视频文件路径列表,例如 ['1.mp4', '2.mp4']
- :param output_path: 输出视频文件路径,例如 'output.mp4'
- """
- clips = []
- for path in clips_path:
- if not os.path.isfile(path):
- raise FileNotFoundError(f"视频文件不存在: {path}")
- clips.append(VideoFileClip(path))
- # 拼接所有视频片段
- final_clip = concatenate_videoclips(clips, method="compose") # 使用 compose 可处理不同尺寸
- # 写入输出文件
- final_clip.write_videofile(
- output_path,
- codec='libx264',
- audio_codec='aac',
- temp_audiofile='temp-audio.m4a',
- remove_temp=True
- )
- # 关闭所有 clip 以释放资源
- for clip in clips:
- clip.close()
- final_clip.close()
- if __name__ == "__main__":
- clips_path = ["./output/run_20251222_091230/video_clips/scene0_len0.mp4", "./output/run_20251222_091230/video_clips/scene0_len1.mp4"]
- output_path = "video.mp4"
- concat_videos(clips_path, output_path)
|