A union of curiosity and data science

Knowledgebase and brain dump of a database engineer


Combine MP3's into one file

from pydub import AudioSegment
import os

# Path to the "MP3" directory
directory_path = "./<MP3's>"

# List all MP3 files in the directory and sort them by name
# 01_file.mp3, 02_file.mp3 and so on 
mp3_files = sorted([f for f in os.listdir(directory_path) if f.endswith('.mp3')])

# Initialize an empty AudioSegment
combined = AudioSegment.empty()

# Loop through the sorted MP3 files and concatenate them
for mp3_file in mp3_files:
mp3_path = os.path.join(directory_path, mp3_file)
audio = AudioSegment.from_mp3(mp3_path)
combined += audio

# Export the concatenated MP3 file
output_path = os.path.join(directory_path, "combined.mp3")
combined.export(output_path, format="mp3")



Add comment