I have to convert video files (MP4) to audio only (MP3, but M4A is also acceptable) and for that I am using this code:
import moviepy.editor as mp
def convert_video_to_mp3(video_file):
try:
file_mp4 = f'{video_file}_video.mp4'
file_mp3 = f'{video_file}.mp3'
print(f'file_mp4: {file_mp4}')
clip = mp.VideoFileClip(file_mp4)
clip.audio.write_audiofile(file_mp3)
except:
logger.critical(f'Failed to convert to MP3: {video_file}_video.mp4')
It works, however it starts tens or even hundreds of processes at the server to perform one single conversion, and the number of processes goes far above the server limit. The conversion also takes a lot of time, usually tens of seconds, but in some cases takes minutes (depending on the video size).
How can I optimize the code to reduce the number of processes that are started for every conversion?