import ffmpeg import os.path import os import urllib.parse import wget import shutil #before running: #install python3 #pip install ffmpeg-python #pip install whatever else it asks you to if there is an error #it just werks #doesn't seem to work with gifs.... (fixed) #https://superuser.com/questions/1508999/combine-audio-and-gif-into-a-video-while-preserving-transparency-using-ffmpeg def get_existing_path(prompt: str) -> str: while True: file = input(prompt) if os.path.exists(file[1:-1]): #ignoring the quotes at beginning and end print('Path Found') return file print('Path not found.') def make_proper_file_name(filename: str) -> str: bad_chars = ['/', '\\', ':', '?', '"', '<', '>', '|', '*'] good_chars = ['[fs]', '[bs]', '[col]', '[quest]', '[quot]', '[lt]', '[gt]', '[line]', '[star]'] new_filename = filename for char, rep in zip(bad_chars, good_chars): new_filename = new_filename.replace(char, rep) return new_filename def combine_sound(video_file, audio_file, destination, file_name): video_path = os.path.splitext(video_file[1:])[-1] print(video_path) while(os.path.isfile(os.path.join(destination, file_name+'.mkv'))): file_name += '_' file_name += '.mkv' if(video_path in ['.png', '.jpg', '.jpeg']): os.system(f'ffmpeg -i "{video_file}" -i "{audio_file}" -c copy "{os.path.join(destination, file_name)}"') print('Still image') print(f'ffmpeg -i "{video_file}" -i "{audio_file}" -c copy "{os.path.join(destination, file_name)}"') elif(video_path == '.gif'): os.system(f'ffmpeg -i "{audio_file}" -ignore_loop 0 -i "{video_file}" -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -shortest -strict -2 -c:v libx264 -threads 4 -c:a aac -b:a 192k -pix_fmt yuv420p -shortest "{os.path.join(destination, file_name)}"') print(f'ffmpeg -i "{audio_file}" -ignore_loop 0 -i "{video_file}" -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -shortest -strict -2 -c:v libx264 -threads 4 -c:a aac -b:a 192k -pix_fmt yuv420p -shortest "{os.path.join(destination[1:-1], file_name)}"') else: os.system(f'ffmpeg -stream_loop -1 -i "{video_file}" -i "{audio_file}" -shortest -c copy "{os.path.join(destination, file_name)}"') #streamloop makes the video loop until audio is done print(f'ffmpeg -stream_loop -1 -i "{video_file}" -i "{audio_file}" -shortest -c copy "{os.path.join(destination, file_name)}"') def get_all_sound_urls(file): audio_urls = [] start_index = 0 while(file[start_index:].find('[sound=') != -1): url = file[file.find('[sound=') + 7 : file.find(']')] print('url', url) if(not ('%' in url)): print('testing') if('2F' in url): url = url.replace('2F', '/') if('https' in url): url = url.replace('https', 'https:') elif('http' in url): url = url.replace('http', 'http:') else: url = urllib.parse.unquote(url) if(not url.startswith('https://') and not url.startswith('http://')): url = 'https://' + url audio_urls += url start_index = file.find(']')+1 print(file[start_index:]) print(start_index) print(file[start_index:].find('[sound=')) return audio_urls def merge_all_sound_urls(audio_urls, path): audio_file_list = [] for url in audio_urls: wget.download(audio_url, out=path) audio_file_list += audio_url[audio_url.rfind('/')+1:] for i in range(len(audio_file_list)-1): os.system(f'ffmpeg -i "{audio_file_list[i]}" -i "{audio_file_list[i+1]}" -filter_complex amix=inputs=2:duration=longest "{audio_file_list[i+1]}"') def v1(): #you want a folder with subfolders: completed, discard, and finished #completed is the converted soundposts #discard is the audio files #finished is the original soundposts without audio mypath = "C:\\Users\\awatson\\videos" #example path. change it to your folder destination_path = mypath + "completed\\" discard_path = mypath + "\\discard\\" finished_vid_path = mypath + "\\finished\\" for file in os.listdir(mypath): print(file) if(os.path.isfile(os.path.join(mypath, file)) and '[sound=' in file): audio_url = file[file.find('[sound=') + 7 : file.find(']')] if(not ('%' in audio_url)): print('testing') if('2F' in audio_url): audio_url = audio_url.replace('2F2F', '//files.catbox.moe/') if('https' in audio_url): audio_url = audio_url.replace('https', 'https:') elif('http' in audio_url): audio_url = audio_url.replace('http', 'http:') else: audio_url = urllib.parse.unquote(audio_url) if(not audio_url.startswith('https://') and not audio_url.startswith('http://')): audio_url = 'https://' + audio_url audio_file = audio_url[audio_url.rfind('/')+1:] #audio_urls = get_all_sound_urls file_name = file[0:file.find('[sound=')] print(audio_url) print(audio_file) counter = 0 success = False while(counter < 5 and success == False): try: wget.download(audio_url, out=mypath) counter = 11 success = True except: counter+=1 if(success): combine_sound(os.path.join(mypath, file), os.path.join(mypath, audio_file), destination_path, file_name.strip()) print(os.path.isfile(os.path.join(mypath, audio_file))) print(os.path.exists(discard_path)) shutil.move(os.path.join(mypath, audio_file), os.path.join(discard_path, audio_file)) shutil.move(os.path.join(mypath, file), os.path.join(finished_vid_path, file)) print() #os.remove(os.path.join(mypath, audio_file)) if __name__ == "__main__": v1() #print(get_all_sound_urls("[sound=z.zz.fo%2FMtPWa.ogg][sound=z.zz.fo%2FlGqrX.ogg][sound=z.zz.fo%2FM6HJ3.ogg][sound=z.zz.fo%2Fw4htc.ogg][sound=z.zz.fo%2F4Q2Vn.ogg][sound=z.zz.fo%2FPE0iq.ogg][sound=z.zz.fo%2FKF71C.ogg][sound=z.zz.fo%2FsYcPy.ogg].jpg")) if(False): video_file = get_existing_path('Enter the path to your video file (in quotes): ') audio_file = get_existing_path('Enter the path to your audio file (in quotes): ') destination = get_existing_path('Enter the path you want to save output to (in quotes): ') file_name = make_proper_file_name(input('Enter a filename with mkv. ex: [output.mkv] (no quotes) ')) video_path = os.path.splitext(video_file[1:-1])[-1] print(video_path) if(video_path in ['.png', '.jpg', '.jpeg']): os.system(f'ffmpeg -i {video_file} -i {audio_file} -c copy "{file_name}"') print('Still image') elif(video_path == '.gif'): os.system(f'ffmpeg -i {audio_file} -ignore_loop 0 -i {video_file} -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -shortest -strict -2 -c:v libx264 -threads 4 -c:a aac -b:a 192k -pix_fmt yuv420p -shortest "{os.path.join(destination[1:-1], file_name)}"') else: os.system(f'ffmpeg -stream_loop -1 -i {video_file} -i {audio_file} -shortest -c copy "{os.path.join(destination[1:-1], file_name)}"') #streamloop makes the video loop until audio is done #shortest makes it stop when the shortest one finishes ''' convert_to_mp4 = input('Click enter if done. Else if you want to convert to mp4 enter anything: ') if(convert_to_mp4 != ""): print(f'ffmpeg -i {destination[1:-1] + file_name} -c:a copy {file_name[0:-4]}.mp4') os.system(f'ffmpeg -i {destination[1:-1] + file_name} -c:a copy {file_name[0:-4]}.mp4') os.remove(os.path.join(destination[1:-1], file_name)) '''