Spaces:
Build error
Build error
Upload utils_audio.py
Browse files- utils_audio.py +25 -0
utils_audio.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from moviepy.editor import VideoFileClip
|
3 |
+
from pydub import AudioSegment
|
4 |
+
|
5 |
+
def convert_to_wav(input_file):
|
6 |
+
_, extension = os.path.splitext(input_file)
|
7 |
+
extension = extension.lower() # Convert to lowercase for case-insensitivity
|
8 |
+
output_wav_file = input_file.replace(extension, ".wav")
|
9 |
+
if extension == ".wav":
|
10 |
+
return output_wav_file
|
11 |
+
if extension == ".mp4":
|
12 |
+
video_clip = VideoFileClip(input_file)
|
13 |
+
audio_clip = video_clip.audio
|
14 |
+
audio_clip.write_audiofile(output_wav_file)
|
15 |
+
audio_clip.close()
|
16 |
+
print(f"{input_file} (MP4) converted to {output_wav_file}")
|
17 |
+
return output_wav_file
|
18 |
+
elif extension == ".mp3":
|
19 |
+
audio_clip = AudioSegment.from_mp3(input_file)
|
20 |
+
audio_clip.export(output_wav_file, format="wav")
|
21 |
+
print(f"{input_file} (MP3) converted to {output_wav_file}")
|
22 |
+
return output_wav_file
|
23 |
+
else:
|
24 |
+
print(f"Unsupported file format: {extension}")
|
25 |
+
return input_file
|