[英]Streaming mp3 files with django, read from a page with <audio>
我正在嘗試制作一個小型應用程序,以通過Web界面流式傳輸mp3文件,並且我考慮過使用django在python中進行播放並在服務器端進行處理。
我想使用/ stream / ID之類的網址來流化與該ID對應的mp3。 我在django中創建了一個視圖,以使用不同的方法來提供這些文件,我嘗試的最后一個是此處所述的方法
如果我從firefox訪問/ stream / ID,它將直接用firefox-totem或類似的插件播放mp3。 如果我在音頻播放器上使用與源網址相同的頁面,則該頁面根本不起作用(可以使用apache提供的mp3文件的鏈接)。
這是我視圖的代碼(僅發送一個測試文件)
def stream(request): resp = HttpResponse(FileIterWrapper(open('/.../test.mp3',"rb")),mimetype='audio/mpeg') resp['Content-Length'] = os.path.getsize("/.../test.mp3") resp['Content-Disposition'] = 'filename=test.mp3' return resp
我切斷了全部路徑,這不是問題。
在查看django runserver輸出時,我注意到每次音頻播放器嘗試時,都會出現這兩個錯誤,
Traceback (most recent call last): File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 281, in run self.finish_response() File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 321, in finish_response self.write(data) File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 417, in write self._write(data) File "/usr/lib/python2.6/socket.py", line 318, in write self.flush() File "/usr/lib/python2.6/socket.py", line 297, in flush self._sock.sendall(buffer(data, write_offset, buffer_size)) error: [Errno 104] Connection reset by peer ---------------------------------------- Exception happened during processing of request from ('127.0.0.1', 42891) Traceback (most recent call last): File "/usr/lib/python2.6/SocketServer.py", line 283, in _handle_request_noblock self.process_request(request, client_address) File "/usr/lib/python2.6/SocketServer.py", line 309, in process_request self.finish_request(request, client_address) File "/usr/lib/python2.6/SocketServer.py", line 322, in finish_request self.RequestHandlerClass(request, client_address, self) File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 562, in __init__ BaseHTTPRequestHandler.__init__(self, *args, **kwargs) File "/usr/lib/python2.6/SocketServer.py", line 618, in __init__ self.finish() File "/usr/lib/python2.6/SocketServer.py", line 661, in finish self.wfile.flush() File "/usr/lib/python2.6/socket.py", line 297, in flush self._sock.sendall(buffer(data, write_offset, buffer_size)) error: [Errno 32] Broken pipe
當我直接訪問流URL時,沒有問題/錯誤。
我在鉻(最新版本,安裝了ffmpeg-extra)中嘗試了同樣的方法,它與apache中的mp3可以正常工作,但是在使用流url時超時。
我嘗試在響應中設置不同的標題,但沒有成功。 我目前正在設置內容長度,內容類型和內容處置
我正在尋找新的想法嘗試。
謝謝你的幫助!
您可以嘗試使用Wireshark觀察瀏覽器/媒體播放器/ apache正常運行的情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.