moviepy ruining video after combining them - python

I'm trying to make a program that downloads videos from TikTok and combines all the separate videos into one .mp4 file and moves the final video to a folder on my desktop. I've been able to make it download all the videos and when I watch the separate videos they play fine however when I combine the videos some of the videos are messed up and look like this but the audio is fine.
#slecting all .mp4 files
video_files = glob.iglob("*.mp4")
print(video_files)
clips = []
for clip in video_files: # For each mp4 file name
clips.append(VideoFileClip(clip)) # Store them as a VideoFileClip and add to the clips list
today = date.today()
final = concatenate_videoclips(clips) # Concatenate the VideoFileClips
final.write_videofile(f"{today}.mp4", codec="libx264")
#moving completed video to folder on desktop
shutil.move(f'{today}.mp4', '/Users/jacobmarrandio/Desktop/done_videos/')
thanks for any help

You want to ensure that all of your videos are of the same size before concatenating. Or, you could modify the smaller clips to have a black margin by filling up space by making this change:
final = concatenate_videoclips(clips, method='compose')

Related

How do I make multiple videos of individual images at once using images in a folder? Moviepy -Python

Soooo, I have no clue how to do this and am completely new to python however,
I would like to select all images in a folder and turn them into 10 second individual videos for each image using MoviePy.
but instead of combining all the images together into one video, I want to make multiple videos for each individual image in the folder. From what I know you can use import glob to get all images in the folder but that's about it.
I have tried:
clips = [ImageClip(clip).set_duration(10) for clip in glob("imagesfolder/*.jpg")]
video_clip = concatenate_videoclips(clips, method="compose")
audioclip = AudioFileClip("backgroundmusicforduck.mp3")
new_audio = afx.audio_loop(audioclip, duration=video_clip.duration)
video_clip.audio = new_audio
video_clip.write_videofile("memes.mp4", fps=24, remove_temp=True, codec="libx264", audio_codec="aac")
but all this does is combine all the images together into one video..
Thanks!!!

Is there a way to add a .gif or .mp4 before each image, MoviePy -Python

I'm pretty new to Python and trying to add a .gif file or .mp4 file
before every image inserted into an .mp4 video file using MoviePy.
The code below is adding .jpg files from a folder and putting them together into an .mp4 video that shows each image for 10 seconds then to the next image in the folder. However, I would like add a .gif or .mp4 file before each image.
Here's the code:
from moviepy.editor import *
from glob import glob
#MAKE MOVIE
def makeMovie():
clips = [ImageClip(clip).set_duration(10) for clip in glob("imagesfolder\*.jpg")] #This adds the images from folder together
#After every image added to the video add a .gif or .mp4 file after it
video_clip = concatenate_videoclips(clips, method="compose")
video_clip.write_videofile("memes.mp4", fps=24, remove_temp=True, codec="libx264",
audio_codec="aac")
makeMovie()
I want to do something like ==> clips = [ImageClip(clip).set_duration(10) for clip in glob("imagesfolder/*.jpg") + "duckanim0000-0079.mp4"]

Add audio to video using moviepy

Title basically explains it. Trying to add audio to a video. I have a sequence of mp3 files that I want to play sequentially during the video. There're no errors or anything with the code but when I run the code theres still no audio in the video. Just a recreation of the old video with no audio (the original video doesnt have audio either).
video = VideoFileClip("finished_video.mp4")
title_clip = AudioFileClip("title.mp3")
audio_list = [title_clip]
for x in range(1, counter):
audio = AudioFileClip("p{}.mp3".format(x))
audio_list.append(audio)
video.set_audio(audio_list)
video.write_videofile("new_filename.mp4")
it works for me
from moviepy.editor import *
videoclip = VideoFileClip("sample.mp4")
audioclip = AudioFileClip("sample_audio.mp3")
new_audioclip = CompositeAudioClip([audioclip])
videoclip.audio = new_audioclip
videoclip.write_videofile("output.mp4")
use CompositeAudioClip to merge both files.
I had the same issue. It seems like set_audio() doesn't actually set it, but returns the new video combined with the audio.
This is the way I did it :
final_video = video.set_audio(audio_list)
final_video.write_videofile("new_filename.mp4")
Hope that helps !

How can I loop a mp4 file in moviepy?

My current code is taking a mp4 video file, it adds a mp3 music file to it, the duration of the mp4 file is set to the length of the mp3 file, the clip is resized to 1920x1080 pixels and finally it saves and outputs the finished video.
Result: The finished video plays the mp4 file one time and then freezes until the mp3 file ends.
Result that I want: How can I make the mp4 file loop until the end of the mp3 file so it doesn't freeze after one play.
from moviepy.editor import *
import moviepy.editor as mp
import moviepy.video.fx.all as vfx
audio = AudioFileClip("PATH/TO/MP3_FILE")
clip = VideoFileClip("PATH/TO/MP4_FILE").set_duration(audio.duration)
# Set the audio of the clip
clip = clip.set_audio(audio)
#Resizing
clip_resized = clip.resize((1920, 1080))
#Code that doesn't work for looping
newClip = vfx.loop(clip_resized)
# Export the clip
clip_resized.write_videofile("movie_resized.mp4", fps=24)
The code itself works but the mp4 doesn't loop until the end. Thanks in advance.
3 Years late on that one, but it's still the top result for this on google.
If you want to loop it for the duration of audio, moviepy has a simple loop function that you call from the clip you want to loop, it takes either the amount of loops or the duration of time to loop for.
So in your case it would be
loopedClip = clip_resized.loop(duration = audio.duration)
Or if you don't want to create a seperate clip then
clip_resized = clip_resized.loop(duration = audio.duration)
If you get this error :
OSError: Error in file ...video.mp4, Accessing time t=101.77-101.81 seconds, with clip duration=101 seconds,
This problem occured because the loop do not match with the audio of the final looped video file
So you have to extract the audio from the video file (or your custom audio file) and loop it too as the video file
audio = AudioFileClip("video.mp4")#here i'm using the audio of the original video but if you have custom audio pass it here
audio = afx.audio_loop(audio, duration=500) #you can use n=X too
clip1 = VideoFileClip("video.mp4")
clip1 = vfx.loop(clip1, duration=500) #you can use n=X too
clip1 = clip1.set_audio(audio)
clip1.write_videofile("movie.mp4")

MoviePy: Concatenating video clips causes weird glitches in final video

Is there a way to successfully always patch up any clips together in such a way that prevents weird glitches? I put together a .mp4 from smaller .mp4 files and I got a final video with weird glitches. I am running Python 3.6.1 on Windows 10 through Sublime Text 3. I used MoviePy to do the concatenation.
The code:
from moviepy.editor import VideoFileClip, concatenate_videoclips
import os.path
path = "C:/Users/blah/videos/out/"
cliparray = []
for filename in os.listdir(path):
cliparray.append(VideoFileClip(path + filename))
final_clip = concatenate_videoclips(cliparray)
final_clip.write_videofile(path + "concatenatedvideo.mp4", codec = "libx264")
The weird glitches:
One of the clips turns into a 3x3 grid of smaller clips.
Another has the audio not lined up with the video
Another is sped up faster than what was normal.
I had also glitch while concatenating different video clips. Some had different resolutions and that was making output video file with some sort of glitches. I fixed it with
final_clip = concatenate_videoclips(cliparray, method='compose')
Resulting output was without any glitch, but since they have different resolutions, the moviepy assigns highest resolution among video clips. To fix this you might just crop to same size.
from moviepy.editor import *
#load video 1 in to variable
video_1 = VideoFileClip('video1.mp4')
#load video 2 in to variable
video_2 = VideoFileClip('video2.mp4')
clips = [video_1, video_2]
# concatenating both the clips
final = concatenate_videoclips(clips,method='compose')
#writing the video into a file / saving the combined video
final.write_videofile("merged.mp4")

Categories