5

Beginner here. I'm trying to loop through a list of paths to mp3 files and merge all the mp3s:

combined_sounds = AudioSegment.from_mp3('./00.mp3') for file in files: sound = AudioSegment.from_mp3(file) combined_sounds= combined_sounds + sound; # .... 

I need to define combined_sounds before going through the list - but I'd like to not put anything into it before going through the loop. Right now my little hack is to use an empty mp3 file 00.mp3, but this isn't the nicest solution. How would I accomplish this?

I tried combined_sounds = '' and combined_sounds = None but both get me errors when I then enter the loop and try to turn it into an AudioSegment. Can I create an empty AudioSegment before the loop maybe?

2
  • Do want to add some silence at the beginning? Commented Jun 26, 2020 at 7:24
  • No, I just want to merge all the files in the list. The 00.mp3 at the beginning isn't for silence, it's just because I didn't know how to declare the variable properly, so it's just a way around that :( Commented Jun 26, 2020 at 7:28

2 Answers 2

10

You can use the empty() function in AudioSegment to create a blank segment:

combined_sounds = AudioSegment.empty() for file in files: sound = AudioSegment.from_mp3(file) combined_sounds += sound 
Sign up to request clarification or add additional context in comments.

Comments

0

you can try this final_song = one_sec_segment + song in Adding silent frame to wav file using python, may be as above

#read wav file to an audio segment song = AudioSegment.from_wav(audio_in_file) songggg = AudioSegment.from_wav(audio_in_fileeee) #Add above two audio segments final_song = songggg + song #Either save modified audio final_song.export(audio_out_file, format="wav") 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.