from moviepy.editor import VideoFileClip, concatenate_videoclips,vfx from moviepy.editor import AudioFileClip, CompositeAudioClip#, TextClip # from moviepy.video.tools.subtitles import SubtitlesClip import os # import torch # import soundfile as sf # from transformers import pipeline # synthesiser = pipeline("text-to-audio", "facebook/musicgen-stereo-small") # sampling_rate = model.config.audio_encoder.sampling_rate def combine_videos(folder_name, length_speech): # length = len(os.listdir(folder_name)) def check_len(dir): if len(dir) < len(length_speech): return check_len(dir*2) else: return dir vid_dir = os.listdir(folder_name) if len(vid_dir) == 0: raise Exception("Canot find any video! Please try different words") vid_dir = check_len(vid_dir) vids = [VideoFileClip(os.path.join(folder_name,x)) for x in sorted(vid_dir)] print(vids, len(vid_dir), len(length_speech)) combined = concatenate_videoclips( [vid.subclip(0,ls).fx(vfx.fadein,0.5).fx(vfx.fadeout,0.5) if vid.duration > ls else vid.fx(vfx.fadein,0.5).fx(vfx.fadeout,0.5) for vid, ls in zip(vids, length_speech)], # [VideoFileClip(os.path.join(folder_name,x)).subclip(0,7).fx(vfx.fadein,0.5).fx(vfx.fadeout,0.5) for x in sorted(os.listdir(folder_name))], "compose",bg_color=None, padding=0 ) combined.write_videofile(os.path.join(folder_name, "Final_Ad_Video.mp4")) # if length <= 6: # combined = concatenate_videoclips([VideoFileClip(os.path.join(folder_name,x)).subclip(0,10) for x in os.listdir(folder_name)]) # combined.write_videofile(os.path.join(folder_name, "Ad_Video.mp4")) # else : # combined = concatenate_videoclips([VideoFileClip(os.path.join(folder_name,x)).subclip(0,60//length) for x in os.listdir(folder_name)]) # combined.write_videofile(os.path.join(folder_name, "Ad_Video.mp4")) print("Done! Your ads video has been created") def combine_audio_video(folder_name, vid, speech, sentences, length_speech): videoclip = VideoFileClip(vid) audioclip = AudioFileClip(speech) # music = synthesiser("lo-fi music with a soothing melody", forward_params={"max_new_tokens": sampling_rate//sum(length_speech)}) # sf.write("musicgen_out.wav", music["audio"][0].T, music["sampling_rate"]) #generator = lambda txt: TextClip(txt, font='Arial', fontsize=24, color='white') # subs = [((0, 4), 'subs1'), # ((4, 9), 'subs2'), # ((9, 12), 'subs3'), # ((12, 16), 'subs4')] # ls = [0] + length_speech # subs = [ ( (ls[i],ls[i+1]), sub) for i,sub in enumerate(sentences)] # subtitles = SubtitlesClip(subs, generator) # videoclip = CompositeVideoClip([videoclip, subtitles.set_pos(('center','bottom'))]) new_audioclip = CompositeAudioClip([audioclip]) videoclip.audio = new_audioclip # videoclip.audio = CompositeAudioClip([audioclip, AudioFileClip("musicgen_out.wav")]) videoclip.write_videofile(os.path.join(folder_name,"new_filename.mp4"))