i use moviepy
for merge mp3 and mp4 file. it’s working good but the process is so slow. i use multi threads but the main process(merging) is still slow. i search and know that i use ffmpeg
but i get error at first try and i cant fix it. so i came back to moviepy and is there any way to speed up this process?
my code is so simple: just read the name of my files and get them and merge mp3 file with mute mp4 files. btw i coded with threading
and as i say it have not progress. (Of course, there is a possibility that I don’t drink well with Multi-Thread! God knows!)
import threading
from os import walk
from moviepy.editor import VideoFileClip, AudioFileClip
from concurrent.futures import ThreadPoolExecutor
def merge_audio_and_video(name):
print(f"Processing {name}...")
video = VideoFileClip(name + '.mp4')
audio = AudioFileClip(name + '.m4a')
final_video = video.set_audio(audio)
final_video.write_videofile(
f'./merge/{name}.mp4', codec="libx264", audio_codec="aac"
)
filenames = next(walk('.'), (None, None, []))[2]
mp4_files = [file for file in filenames if file.endswith('.mp4')]
mp4_files_without_extension = [file.replace('.mp4', '') for file in mp4_files]
with ThreadPoolExecutor(max_workers=1) as executor:
futures = [executor.submit(merge_audio_and_video, name)
for name in mp4_files_without_extension]
for future in futures:
future.result()
print("All video files have been processed.")