2017-07-19 116 views
0

我已經處理了一些音頻文件,但我只有這樣一個URL:如何在python中播放ogg stream 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,這樣我怎麼能發揮它,而不是直接保存爲一個文件?

回答

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

+0

TKS,這是works.but我想打它,而不是直接保存爲一個文件,讓你有一些好主意嗎? – wangcs

+1

您可以使用'StringIO(r.content)'(或Python3中的BytesIO)創建一個內存文件,但是您必須找到一個將從類文件對象而不是真實文件播放的庫。 –

+0

upvoting,因爲這最好地回答了OP的請求。 –

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

+0

謝謝,但我知道如何使用WDC,但現在我只有url。 – wangcs

+0

那麼這與python有什麼關係呢? –

+0

哦,我想找到如何在python中播放此音頻網址 – wangcs