0
votes

I have very simple websocket in Python that is listening to live bi-directional stream from Twilio. I get data with the following information.

{'event': 'connected', 'protocol': 'Call', 'version': '1.0.0'}

{'event': 'start', 'sequenceNumber': '1', 'start': {'accountSid': 'accountSid_value', 'streamSid': 'streamSid_value', 'callSid': 'callSid_value', 'tracks': ['inbound'], 'mediaFormat': {'encoding': 'audio/x-mulaw', 'sampleRate': 8000, 'channels': 1}}, 'streamSid': 'streamSid_value'}

{'event': 'media', 'sequenceNumber': '5', 'media': {'track': 'inbound', 'chunk': '4', 'timestamp': '262', 'payload': '/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////w=='}, 'streamSid': 'streamSid_value'}

.........

{'event': 'stop', 'sequenceNumber': '332', 'streamSid': 'streamSid_value', 'stop': {'accountSid': 'accountSid_value', 'callSid': 'callSid_value'}}

The media is located in media.payload. In the example provided media is silence. The media is raw audio encoded in base64. How can I convert the string from media.payload to .wav(or any other) audio file?

2

2 Answers

1
votes

I found a solution, this code works for me:

https://github.com/saisyam/pywav

import pywav
import pickle
import base64

with open('stream', 'rb') as fp:
    stream = pickle.load(fp)

data = []
for d in stream:
    if d['event'] == 'media':
        data.append(base64.b64decode(d['media']['payload']))

data_bytes = b"".join(data)
wave_write = pywav.WavWrite("filename.wav", 1, 8000, 8, 7)  # 1 stands for mono channel, 8000 sample rate, 8 bit, 7 stands for MULAW encoding
wave_write.write(data_bytes)
wave_write.close()

Decoding with base64 and pydub worked, but the audio quality was terrible. This method returns excellent audio quality.

0
votes

First you need to decode it:

import base64
decoded_payload = base64.b64decode(media.payload)

As for your main concern, I can't really help, but I recommend you check pydyb (https://github.com/jiaaro/pydub).