如何在python中播放ogg流URL?

时间:2017-07-19 04:24:02

标签: python ogg watson

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

<button class="circle btn btn-danger pull-right" onclick="$('#dialogid').dialog('close')">X</button>

它似乎是一个流网址,所以如何直接播放而不是保存为文件?

2 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:0)

首先,我建议您使用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