from pydub import AudioSegment
def combine_wavs_to_mp3(wav_files, output_mp3_file):
combined_sound = AudioSegment.empty()
for wav_file in wav_files:
sound = AudioSegment.from_wav(wav_file)
combined_sound += sound
combined_sound.export(output_mp3_file, format="mp3")
if __name__ == '__main__':
# 示例用法
wav_files = ["1.wav", "2.wav", "3.wav", "4.wav", "5.wav"]
output_mp3_file = "combined.mp3"
combine_wavs_to_mp3(wav_files, output_mp3_file)
增强版,直接读取文件夹:
from pydub import AudioSegment
import os
def combine_wavs_to_mp3(wav_dir, output_mp3_file):
wav_files = [os.path.join(wav_dir, i) for i in os.listdir(wav_dir)]
combined_sound = AudioSegment.empty()
for wav_file in wav_files:
sound = AudioSegment.from_wav(wav_file)
combined_sound += sound
combined_sound.export(output_mp3_file, format="mp3")
if __name__ == '__main__':
# 示例用法
wav_dir = 'E:\\project\scripts_test\dataframe\\123' # wav存放文件夹
output_mp3_file = os.path.join(wav_dir, "combined.mp3")
combine_wavs_to_mp3(wav_dir, output_mp3_file)