How to split long audio (EX:1hour ) file into multiple short length (5s) audio file using python

2,695 views
Skip to first unread message

ikt...@gmail.com

unread,
Aug 29, 2020, 5:15:05 AM8/29/20
to librosa

I am doing masters thesis on audio detection using machine learning.I have some long audio files.I want to split this audio file into multiple short length audio file using python.Ex:The audio long length is more than 1 hour and want to split into multiple short length 5s files. i want to extract features for the whole audio file in each 5s or so on. or please guide that i can split the long audio file into multiple smaller pieces and all the smaller value has almost same value.

Thank's in advance

raghawendra singh

unread,
Aug 29, 2020, 6:01:16 AM8/29/20
to ikt...@gmail.com, librosa
first read the file:


On Sat, Aug 29, 2020 at 2:45 PM ikt...@gmail.com <ikt...@gmail.com> wrote:

I am doing masters thesis on audio detection using machine learning.I have some long audio files.I want to split this audio file into multiple short length audio file using python.Ex:The audio long length is more than 1 hour and want to split into multiple short length 5s files. i want to extract features for the whole audio file in each 5s or so on. or please guide that i can split the long audio file into multiple smaller pieces and all the smaller value has almost same value.

Thank's in advance

--
You received this message because you are subscribed to the Google Groups "librosa" group.
To unsubscribe from this group and stop receiving emails from it, send an email to librosa+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/librosa/4b1709c9-14f4-4369-bb2f-a94bf8f97c57n%40googlegroups.com.


--
Raghawendra prakash singh

raghawendra singh

unread,
Aug 29, 2020, 6:08:43 AM8/29/20
to ikt...@gmail.com, librosa
import librosa
import soundfile as sf
file = "your_source_file.wav"
x, sr = librosa.load(file, sr=None)
you will get x as audio file in numpy array and sr as original sampling rate
for i in range(0, len(x),5 * sr)
    y = x[5 * sr * i: 5 * sr *(i+1)]
    sf.write("dest_audio"+str(i)+".wav", y, sr)
--
Raghawendra prakash singh

ikt...@gmail.com

unread,
Aug 29, 2020, 6:35:23 AM8/29/20
to librosa
Dear  Raghaw

I cant find the splitting files(5s) after writing  in the destination directory.even the value of x is showing 0.Could you please help me out?
Here is my code given  bellow.

Code:
import librosa
import soundfile as sf


path_dir=r"C:\Users\iktea\OneDrive\Desktop\Test audio data\Test"#source path of the long audio file
splitaudio=r"C:\Users\iktea\OneDrive\Desktop\Test audio data\splitaudio"#Destination path after splitting

for filename in os.listdir(path_dir):
    print(filename)
    path_file = os.sep.join([path_dir, filename])
    x, sr = librosa.load(path_file, sr=None)
    print(x)
   
    #you will get x as audio file in numpy array and sr as original sampling rate

    for i in range(0, len(x),5 * sr):
        y = x[5 * sr * i: 5 * sr *(i+1)]
        sf.write(path_dir+str(i)+".ogg", y, sr)


output:
1-11023-4.ogg
[0. 0. 0. ... 0. 0. 0.]
Reply all
Reply to author
Forward
0 new messages