简体   繁体   中英

python make_chunks from an audio stream wav or mp3

I want to write a python program that write chunks from an audio file. I can write chunks from an audio file available locally using following code,

from pydub import AudioSegment
from pydub.utils import make_chunks

myaudio = AudioSegment.from_file("file1.wav" , "wav") 
chunk_length_ms = 10000 # pydub calculates in millisec
chunks = make_chunks(myaudio, chunk_length_ms) #Make chunks of one sec

#Export all of the individual chunks as wav files

for i, chunk in enumerate(chunks):
    chunk_name = "chunk{0}.wav".format(i)
    print "exporting", chunk_name
    chunk.export(chunk_name, format="wav")

The above code will create chunks with 10000 milliseconds of the audio file "file1.wav". But I want to write chunks from an audio stream, the stream could be wav or mp3. Can someone help me on this?

change the audio chunk to numpy array and use the function.get_array_of_samples()

np.array(chunk[0].get_array_of_samples())

I Tried Above code for my project purpose I found Error

import AudioSegment

ImportError: No module named 'AudioSegment'

even after Installed the Audiosegment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM