I want to convert mozilla common voice dataset from mp3 to wav. But this dataset is large and convertion takes many time. How can I make this convertion in colab with multiprocessing to decrease time of conversion?
1 Answer
$\begingroup$
$\endgroup$
You can use the multiprocessing library in Python to speed up the conversion of Mozilla Common Voice dataset from mp3 to wav in Colab.
Here's an example code snippet that you can use:
import multiprocessing
import os
from pydub import AudioSegment
def convert_file(file_path):
if file_path.endswith('.mp3'):
sound = AudioSegment.from_mp3(file_path)
new_file_path = file_path[:-3] + 'wav'
sound.export(new_file_path, format='wav')
os.remove(file_path)
print(f"Converted {file_path} to {new_file_path}")
else:
print(f"Skipping {file_path}")
if __name__ == '__main__':
# Set the path to your Mozilla Common Voice dataset folder
data_dir = 'path/to/mozilla_common_voice_dataset'
# Get a list of all mp3 files in the dataset folder
mp3_files = [os.path.join(dirpath, f)
for dirpath, _, filenames in os.walk(data_dir)
for f in filenames if f.endswith('.mp3')]
# Create a pool of worker processes
num_processes = multiprocessing.cpu_count()
pool = multiprocessing.Pool(processes=num_processes)
# Map the conversion function to the list of mp3 files using the pool of worker processes
pool.map(convert_file, mp3_files)
# Close the pool of worker processes
pool.close()
This code will use all available CPU cores in Colab to convert the mp3 files to wav format in parallel.