1### install moviepy library ###
2pip install moviepy
3"""
4this function will combine audio and video files to file named Output_Video.MP4
5in output folder created in the same directory you are running this function from
6"""
7 ### for more information for advance use check moviepy documentation ###
8### https://zulko.github.io/moviepy/getting_started/getting_started.html ###
9
10 from moviepy.editor import *
11import os
12def combine_audio_video(audiofile,videofile,output_file_name_with_extension):
13 download_path = os.getcwd()
14 videoclip = VideoFileClip(videofile)
15 audioclip = AudioFileClip(audiofile)
16 video = videoclip.set_audio(audioclip)
17 if not os.path.exists(f'{download_path}\\output'): # if directory output dosen't
18 os.makedirs(f'{download_path}\\output') # exist create it
19 video.write_videofile(f'{download_path}\\output\\{output_file_name_with_extension}')
20 while True: ### check if audio and video files are closed to delete them
21 try:
22 myfile = open(f'{download_path}\\{files[1]}', "r+")
23 myfile.close()
24 os.remove(f'{download_path}\\{audiofile}')
25 os.remove(f'{download_path}\\{videofile}')
26 break
27 except IOError:
28 pass