如何在 python 中播放 ogg 流 url?

how to play ogg stream url in python?

我已经处理了一些音频文件,但我只有 URL 这样的:

 https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=en-US_LisaVoice&text=How%20can%20I%20help%20you%20today.&watson-token=r4H7NZRIVbmROHTbd6Bw6NskKwtLAmowO1Roz%2F2e0HzvdkV7iFxJGKnI8Yk%2BWRL13lBppUzSyef1gRjL2gMuQeAu%2BkEgC9JX9G9hG6sedhvC6FHVIlsWsykkjcWsP%2FxBUzXujYdOJHNoTXoDK2MOZAsD%2F155m44REIZYsyWWk7Pnl9fJNjfZQlu4lIMt38VIJKNq9UXBlTB%2FibSW57H7yRGuoEw0RUrAd6oDSt0qYEewEm0Qr6azD98HeL0NjKRcgiiEpJIwJNyXr6mwYFT7bMpq2qswZD56GtgrXkDoF2NZqi2snSk4XzFubSQsO0oE95bs3jm4VPLukQKnyDD9T63P%2B3%2FVhFokRP%2BA8%2BZ%2FITQu%2FYLo3p6juVTUsXGPL5dEU7JmHpuFSqfQQ%2FHiKJTkJiVRZJ%2B0bjoj9yzy3iDQ%2BEGhKzLxng%2BxOX7qkpoo8DVRUrGrnuCoHaQ04OmS10Ic8AVMVvgYmoJOJ8gntCBDwO5ucjn9xVpkONmzHB%2FJs6NJ%2FJc%2BYkOJsKvo2SNW39U2lE8Mrj2Q%2F0XjPzVKQ%2B40oP2oRPDA0RpvXe09Ka1JHFczt5sxQ8W8YJcKUyXJkmlZ%2FGWnmSDG17Yr4LuolUjnn38lNzhn026VwrQ7LqVYVKoc8hmfwxTDr%2BAuL%2Bv4TYGYW75JJNVzdPN5mgbI32a%2F15YHSnykpCECTQ1PzOT3TaaCu41qV6wsqHCCd%2FaGF3jsvUo%2BWQWDZq%2FM0fkbnsyYaHyOO4AAkm9nADnRiD%2FLLuoHERtuay2YhsyJKagNU4YXJ0ISakxfFvs9K1OnLIRInYliWW84DVDVLLnJHhOBFMUH4vytECdAh2%2BNlwDE2GnzgHDg9QSieItFLnYC6tp2BLSL02502%2Fjv1sw4TEfWETCcTwsK8zPc8g%2F3KX7Pwf24ZgUcjzfXtZiwjVc70vPQsxjxTzLqTzP7Mo61QLmSOkFL&accept=audio%2Fogg

好像是流url,那我怎么能直接播放而不是另存为文件呢?

试试这个:

import requests
url = "https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=en-US_LisaVoice&text=How%20can%20I%20help%20you%20today.&watson-token=r4H7NZRIVbmROHTbd6Bw6NskKwtLAmowO1Roz%2F2e0HzvdkV7iFxJGKnI8Yk%2BWRL13lBppUzSyef1gRjL2gMuQeAu%2BkEgC9JX9G9hG6sedhvC6FHVIlsWsykkjcWsP%2FxBUzXujYdOJHNoTXoDK2MOZAsD%2F155m44REIZYsyWWk7Pnl9fJNjfZQlu4lIMt38VIJKNq9UXBlTB%2FibSW57H7yRGuoEw0RUrAd6oDSt0qYEewEm0Qr6azD98HeL0NjKRcgiiEpJIwJNyXr6mwYFT7bMpq2qswZD56GtgrXkDoF2NZqi2snSk4XzFubSQsO0oE95bs3jm4VPLukQKnyDD9T63P%2B3%2FVhFokRP%2BA8%2BZ%2FITQu%2FYLo3p6juVTUsXGPL5dEU7JmHpuFSqfQQ%2FHiKJTkJiVRZJ%2B0bjoj9yzy3iDQ%2BEGhKzLxng%2BxOX7qkpoo8DVRUrGrnuCoHaQ04OmS10Ic8AVMVvgYmoJOJ8gntCBDwO5ucjn9xVpkONmzHB%2FJs6NJ%2FJc%2BYkOJsKvo2SNW39U2lE8Mrj2Q%2F0XjPzVKQ%2B40oP2oRPDA0RpvXe09Ka1JHFczt5sxQ8W8YJcKUyXJkmlZ%2FGWnmSDG17Yr4LuolUjnn38lNzhn026VwrQ7LqVYVKoc8hmfwxTDr%2BAuL%2Bv4TYGYW75JJNVzdPN5mgbI32a%2F15YHSnykpCECTQ1PzOT3TaaCu41qV6wsqHCCd%2FaGF3jsvUo%2BWQWDZq%2FM0fkbnsyYaHyOO4AAkm9nADnRiD%2FLLuoHERtuay2YhsyJKagNU4YXJ0ISakxfFvs9K1OnLIRInYliWW84DVDVLLnJHhOBFMUH4vytECdAh2%2BNlwDE2GnzgHDg9QSieItFLnYC6tp2BLSL02502%2Fjv1sw4TEfWETCcTwsK8zPc8g%2F3KX7Pwf24ZgUcjzfXtZiwjVc70vPQsxjxTzLqTzP7Mo61QLmSOkFL&accept=audio%2Fogg"
r = requests.get(url)

with open("download.ogg", "wb") as o:
    o.write(r.content)

您可以使用 StringIO(r.content)(或 Python3 中的 BytesIO)创建一个内存文件,但您必须找到一个库,该库将从类似文件的对象而不是一个真实的文件。

有关详细信息,请参阅此处:Play audio with Python

首先我会推荐使用Python WDC SDK为您处理信息。

例如:

from watson_developer_cloud import TextToSpeechV1

voice = 'en-US_LisaVoice'
message = 'How can I help you today.'
audio_type = 'audio/ogg'

username='...'
password='...'


tts = TextToSpeechV1(username=username, password=password) 
response = tts.synthesize(message, accept=audio_type,voice=voice)

响应对象包含您的音频。

如果您使用 Jupyter notebooks,您可以像这样使用 IPython.display.Audio

from IPython.display import Audio
Audio(response)

(注意:由于某些原因在 OSX Safari 上不起作用)

对于 python 的通用音频库,请参阅此答案。

Play audio with Python