Spaces:
Runtime error
Runtime error
Commit
·
304ef5f
1
Parent(s):
2ba55d9
Upload utils.py
Browse files
utils.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pydub import AudioSegment
|
2 |
+
#from pydub.utils import mediainfo
|
3 |
+
from pydub.utils import make_chunks
|
4 |
+
import math
|
5 |
+
#flac_audio = AudioSegment.from_file("sample.flac", "flac")
|
6 |
+
#flac_audio.export("audio.wav", format="wav")
|
7 |
+
def split_audio_wav(filename):
|
8 |
+
myaudio = AudioSegment.from_file(filename , "wav")
|
9 |
+
channel_count = myaudio.channels #Get channels
|
10 |
+
sample_width = myaudio.sample_width #Get sample width
|
11 |
+
duration_in_sec = len(myaudio) / 1000#Length of audio in sec
|
12 |
+
sample_rate = myaudio.frame_rate
|
13 |
+
print("sample_width=", sample_width)
|
14 |
+
print("channel_count=", channel_count)
|
15 |
+
print("duration_in_sec=", duration_in_sec)
|
16 |
+
print("frame_rate=", sample_rate)
|
17 |
+
bit_rate =16 #assumption , you can extract from mediainfo("test.wav") dynamically
|
18 |
+
wav_file_size = (sample_rate * bit_rate * channel_count * duration_in_sec) / 8
|
19 |
+
print("wav_file_size = ",wav_file_size)
|
20 |
+
file_split_size = 40000000 # 40mb OR 40, 000, 000 bytes
|
21 |
+
total_chunks = wav_file_size // file_split_size
|
22 |
+
#Get chunk size by following method #There are more than one ofcourse
|
23 |
+
#for duration_in_sec (X) --> wav_file_size (Y)
|
24 |
+
#So whats duration in sec (K) --> for file size of 40Mb
|
25 |
+
# K = X * 40Mb / Y
|
26 |
+
chunk_length_in_sec = math.ceil((duration_in_sec * 40000000 ) /wav_file_size) #in sec
|
27 |
+
chunk_length_ms = chunk_length_in_sec * 1000
|
28 |
+
chunks = make_chunks(myaudio, chunk_length_ms)
|
29 |
+
number_chunks=len(chunks)
|
30 |
+
chunks_list=[]
|
31 |
+
#Export all of the individual chunks as wav files
|
32 |
+
for i, chunk in enumerate(chunks):
|
33 |
+
chunk_name = "chunk{0}.wav".format(i)
|
34 |
+
print("exporting", chunk_name)
|
35 |
+
chunk.export(chunk_name, format="wav")
|
36 |
+
chunks_list.append(chunk_name)
|
37 |
+
return chunks_list
|