So I have written a very simple Python 3 program to take all of the .wav audio files in the working directory and convert them to .flac. Don't worry about any assumptions it makes (like the .wav files being valid, output filenames already existing, etc.). See below:
import os
from multiprocessing import Pool
from pydub import AudioSegment
def worker(filename):
song = AudioSegment.from_wav(filename)
song.export(filename.replace(".wav",".flac"), format = "flac")
if __name__ == '__main__':
converted_count = 0
convertlist = []
for filename in os.listdir(os.getcwd()):
if filename.endswith(".wav"):
convertlist.append(filename)
converted_count += 1
p = Pool(processes=min(converted_count, os.cpu_count()))
p.map(worker, convertlist)
I've timed this and, on my system, have noticed a significant speedup as compared to using no multiprocessing at all. However, this almost seems too simple; I'm not very experienced with multiprocessing (nor multithreading), so I'm honestly not sure if there's a faster, more efficient way to do this.
If you were tasked with writing this simple converter in Python, how would you do it? What would you do to make it more efficient than this?