How to splice an audio file (wav format) into 1 sec splices in python? - google colab (with sample code)

Google colab : How to splice an audio file

Please go through the following post before starting ,As I'm writing this as a continuation of my previous post!

How to install package/modules in colab? - python  

How to create a folder in Drive using google colab? 

We are going to use pydub module for this purpose!

pydub has a specific method for this scenario💓(make_chunks), In which we can simply specify the required length for the chunks/slices of our wav file ,and it will splice our wav file into multiple pieces (obviously it will be playable😌)

The length of chunk is expressed in Milliseconds,Which make it more precise!

 I'm using google colab as my coding platform 

 Python Program :

!pip install pydub
from google.colab import drive
import os
from pydub import AudioSegment
from pydub.utils import make_chunks
 
In cause you are confused about the above piece of code please read this previous post-click here
 
So we have imported all modules required for our project 💥
 
now we have to take audio file from our drive  and  give it to pydub module for creating chunks !
 

code for creating chunks using pydub:

 
 ______________________________________________________________________
 
myaudio = AudioSegment.from_file("/content/gdrive/My Drive/Wav_file, "wav")
chunk_length_ms = 1500 # pydub calculates in millisecchunks = make_chunks(myaudio, chunk_length_ms) #Make chunks of one sec 
for k, chunk in enumerate(chunks):
    chunk_name=str(k)+"chunk"
    print ("exporting", chunk_name)
    chunk.export("/content/gdrive/My Drive/"+chunk_name, format="wav")
 
 _______________________________________________
 
 
 
/content/gdrive/My Drive/ -->is the path/directory in which the initial wav file is stored (which we want to slice)
 
Wav_file  --> Input wave file name  
 
"wav"--> file format
 
 
chunk_name=str(k)+"chunk" --> newly created wav file name 
 
It will be like - 0chunk.wav , 1chunk.wav , 2chunk.wav...
depends on the length of the input /initial wav file 
 
Here Im giving input as a 6 sec long wav file and slice/chunk length as 1.5 sec(1500millliseconds),
 
6/1.5=4
 
so I will get four 1.5 sec wav files as output!
(0chunk.wav , 1chunk.wav , 2chunk.wav , 3chunk.wav)
 
 
If You are using google colab and you want to store the out put in a new folder 
then use this complete code:
 

-------------------------------------------------------------------------------------------------------------------

!pip install pydub
from google.colab import drive
import os
from pydub import AudioSegment
from pydub.utils import make_chunks
 

drive.mount('/content/gdrive')
 
myaudio = AudioSegment.from_file("/content/gdrive/My Drive/Wav_file, "wav")
chunk_length_ms = 1500 # pydub calculates in millisecchunks = make_chunks(myaudio, chunk_length_ms) #Make chunks of one sec 
for k, chunk in enumerate(chunks):
    chunk_name=str(k)+"chunk"
    print ("exporting", chunk_name)
    if k==0:
        try:
        os.mkdir("/content/gdrive/My Drive/Output")
        except:
        print("Folder already found")
 chunk.export("/content/gdrive/My Drive/Output"+chunk_name, format="wav")
 
 ---------------------------------------------------------------------------------------------------------------