问题
I'm reading a file in Python using either of librosa, wave or soundfile libraries and I need to push the chunk (any size) to a HTTP stream. By specification, stream string input requires me to convert frames into RAW s16le format.
I tried multiple options including:
soundarray,rate = librosa.load(pathToWavFile, dtype="<i2")
str = b''.join(soundarray)
But this just creates an empty audio stream. What am I doing wrong?
回答1:
You can try pydub to convert audio to audio-segment, split audio-segment into chunks that are playable (i.e you can play each chunk), then convert them to raw as needed.
Here is a quick code.
from pydub import AudioSegment
from pydub.utils import make_chunks
myaudio = AudioSegment.from_file("myaudio.wav" , "wav")
chunk_length_ms = 1000 # pydub calculates in millisec
chunks = make_chunks(myaudio, chunk_length_ms) #Make chunks of one sec
#Convert chunks to raw audio data which you can then feed to HTTP stream
for i, chunk in enumerate(chunks):
raw_audio_data = chunk.raw_data
By default raw audio is 16bit
>>>
bytes_per_sample= 2 # 2 byte (16 bit) samples
Since raw_audio_data
is raw, if above format doesn't work, you can convert to any other format as needed. Check pydub utils api for details.
回答2:
If you want to get raw data from a wav file in one go, then pydub can also be used like this:
from pydub import AudioSegment
sound = AudioSegment.from_wav('your_audio.wav') # can do same for mp3 and other formats
raw = sound._data # returns byte string
print(raw) # prints "b'k\xffe\xffw\xff\x83\xffu\xff\x85\xff\x81\xff\x85\xff\xa5....."
来源:https://stackoverflow.com/questions/46326922/read-wav-file-from-python-and-convert-frames-into-raw-s16le-string