Pyhton – Loop Videos & Add Audio track

Quick guide on how to loop a video in python and add ad audio track to it:

Here the .py script to do so (comment in line)

from moviepy.editor import *
import moviepy.editor as mp
import moviepy.video.fx.all as vfx

# path where to get video and audio track
main_path = "my_path"
folder = "my_path"

audio_location = main_path + folder

audio = AudioFileClip(main_path+folder+"the_audio_track.mp3")

clip = VideoFileClip(main_path+folder+"the_video_track.mp4")#.set_duration(audio.duration)

# loop (w loops) the vidoe to match the audio track length
newClip = vfx.loop(clip, duration=audio.duration)#_resized)

# Set the audio of the looped clip 
newClip = newClip.set_audio(audio)

#Resizing
#clip_resized = clip.resize((1920, 1080)) 

# possile to add the vfx.loop here to make the video + audio clip as long as we want.

# Export the clip
newClip.write_videofile(main_path+folder+"\\"+"new_video_name.mp4")#, fps=24)

Other resources