Batch concatenate MP4 videos with fade in/out transitions #

  1. Install MoviePy (pip3 install moviepy) and ffmpeg

  2. Edit Python script below, adjusting fade_duration and input_folder as desired, then save (e.g., combine_videos.py):

    import os
    from moviepy.editor import VideoFileClip, concatenate_videoclips
    
    def add_fade_effects(clips, duration=2):
        faded_clips = []
        for i, clip in enumerate(clips):
            if i != 0:
                clip = clip.set_start(clips[i-1].end)
                clip = clip.crossfadein(duration)
            clip = clip.fadein(duration).fadeout(duration)  # Apply fade-in and fade-out to each clip
            faded_clips.append(clip)
        return faded_clips
    
    def combine_videos_with_fades(input_folder, output_file, fade_duration=0.5):
        video_files = [os.path.join(input_folder, f) for f in sorted(os.listdir(input_folder)) if f.endswith('.mp4')]
        video_clips = [VideoFileClip(video) for video in video_files]
        video_clips = add_fade_effects(video_clips, fade_duration)
        final_clip = concatenate_videoclips(video_clips, method="compose")
        final_clip.write_videofile(output_file, codec="libx264")
    
    if __name__ == "__main__":
        input_folder = "/path/to/mp4s"  # Replace with the path to your folder containing MP4 files
        output_file = "combined_video_with_fades.mp4"
        combine_videos_with_fades(input_folder, output_file)
  3. Run python3 combine_videos.py

/nix | Jun 15, 2024


Subscribe or visit the archives.