gpt4 book ai didi

python - Django,使用 FileWrapper (HTML5) 流式传输 H.264 视频

转载 作者:太空宇宙 更新时间:2023-11-04 13:26:47 25 4
gpt4 key购买 nike

我在尝试使用 FileWrapper 类通过 Django 流式传输 H.264 视频时遇到一个奇怪的问题。我正在使用以下 View 函数:

def mp4(request, path):
wrapper = FileWrapper(open(path, 'rb'))
content_type = mimetypes.guess_type(path)[0]
response = HttpResponse(wrapper, content_type=content_type)
response['Content-Length'] = os.path.getsize(path)
return response

函数映射到这个 URL:

  (r'^/mp4/(.*)$', 'mp4'),

我在 HTML5 视频标签中引用 URL:

<video width="560" height="340" controls>
<source src='/video/mp4//tmp/test.mp4' type='video/mp4 codecs="avc1.42E01E, mp4a.40.2"'>
</video>

但是,当我打开包含视频的页面时,视频无法播放并且 Django 开发服务器发出以下错误:

Traceback (most recent call last):  File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 280, in run    self.finish_response()  File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 320, in finish_response    self.write(data)  File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 416, in write    self._write(data)  File "/usr/lib/python2.6/socket.py", line 300, in write    self.flush()  File "/usr/lib/python2.6/socket.py", line 286, in flush    self._sock.sendall(buffer)error: [Errno 104] Connection reset by peer[05/Dec/2010 13:08:00] "GET /video/mp4//tmp/test.mp4 HTTP/1.1" 200 384329753Traceback (most recent call last):  File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 280, in run    self.finish_response()  File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 320, in finish_response    self.write(data)  File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 416, in write    self._write(data)  File "/usr/lib/python2.6/socket.py", line 300, in write    self.flush()  File "/usr/lib/python2.6/socket.py", line 286, in flush    self._sock.sendall(buffer)error: [Errno 104] Connection reset by peerTraceback (most recent call last):  File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 280, in run    self.finish_response()  File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 320, in finish_response    self.write(data)  File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 416, in write    self._write(data)  File "/usr/lib/python2.6/socket.py", line 300, in write    self.flush()  File "/usr/lib/python2.6/socket.py", line 286, in flush    self._sock.sendall(buffer)error: [Errno 32] Broken pipe

浏览器 Google Chrome 似乎多次尝试检索视频,前两次重置连接,最后一次断开连接。请注意,Django 会返回 200 OK 响应以及正确的视频大小。

这是奇怪的部分:即使视频没有播放,我也可以右键单击播放器控件,选择将视频另存为...,Google Chrome 会很乐意下载整个视频并存储它在本地。然后,我仍然可以在 Google Chrome 中通过使用 file://URL 打开保存的视频来播放它。

我也试过将视频文件放在本地网络服务器上,并在视频标签中引用它,效果也不错。

所以我认为这个问题与 FileWrapper 和/或 Django 使用迭代器处理 HttpResponse 的方式有关。数据在那里,可以使用将视频另存为... 保存,但为什么不能播放?

谢谢!

最佳答案

对于那些在 2019 年(及以后)遇到这个问题的人,答案已经发布 here .

关于python - Django,使用 FileWrapper (HTML5) 流式传输 H.264 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4360718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com