updated the mixing.py to support mp3,wav and flac formats.

#15
by Banuka - opened
Files changed (1) hide show
  1. mixing.py +25 -7
mixing.py CHANGED
@@ -1,15 +1,33 @@
1
  import os
2
  from pydub import AudioSegment
3
 
4
- # Using raw strings to avoid issues with backslashes
5
- sound1 = AudioSegment.from_mp3(r"audio\540553_badoink_bass-sine-pulse (mp3cut.net).mp3")
6
- sound2 = AudioSegment.from_mp3(r"audio\y2mate.is - Cinematic Boom Sound Effect-_ttHanoHTL4-192k-1710149220_mastered.mp3")
 
 
 
 
 
 
 
 
 
 
7
 
8
- # mix sound2 with sound1, starting at 1000ms into sound1)
9
- output = sound1.overlay(sound2, position=1000)
 
10
 
 
 
 
11
 
12
- # save the result
13
- output.export(r"mixedAudio\mixed_haptic_audioFile.wav", format="mp3")
14
 
 
 
15
 
 
 
 
1
  import os
2
  from pydub import AudioSegment
3
 
4
+ def check_and_convert(audio_file):
5
+ """
6
+ Checks file format and converts to supported format if necessary.
7
+ """
8
+ # Get file extension
9
+ extension = os.path.splitext(audio_file)[1].lower()
10
+ if extension not in (".mp3", ".wav", ".flac"):
11
+ # Convert to a supported format (e.g. mp3)
12
+ sound = AudioSegment.from_file(audio_file)
13
+ sound.export(os.path.splitext(audio_file)[0] + ".mp3", format="mp3")
14
+ # Update filename to the converted one
15
+ audio_file = os.path.splitext(audio_file)[0] + ".mp3"
16
+ return audio_file
17
 
18
+ # Using raw strings to avoid issues with backslashes
19
+ sound1_path = r"audio\mixkit-distant-explosion-1690.wav"
20
+ sound2_path = r"audio\videoplayback_mastered (1).wav"
21
 
22
+ # Check and convert audio files if needed
23
+ sound1_path = check_and_convert(sound1_path)
24
+ sound2_path = check_and_convert(sound2_path)
25
 
26
+ sound1 = AudioSegment.from_file(sound1_path)
27
+ sound2 = AudioSegment.from_file(sound2_path)
28
 
29
+ # Mix sound2 with sound1, starting at 1000ms into sound1
30
+ output = sound1.overlay(sound2, position=1000)
31
 
32
+ # Save the result (assuming mp3 format)
33
+ output.export(r"mixedAudio\mixed_haptic_audioFile.mp3", format="mp3")