0

以下代码当前将文件保存到磁盘。我可以将其保存到内存中吗?

def synthesize(iam_token, text):
    url = 'https://example.com/tts:synthesize'
    headers = {
        'Authorization': 'Bearer ' + iam_token,
    }

    data = {
        'text': text
    }

    with requests.post(url, headers=headers, data=data, stream=True) as resp:
        for chunk in resp.iter_content(chunk_size=None):
            yield chunk


with open('/tmp/tts.ogg', 'wb') as f:
    for audio_content in synthesize(iam_token, text):
        f.write(audio_content)
4

1 回答 1

1

只需使用 io 包中的 BytesIO 对象

import io

buffer = io.BytesIO()
for audio_content in sythesize(iam_token,text):
    buffer.write(audio_content)
于 2021-04-03T14:55:23.793 回答