我正在使用Python2.7,django==1.7
和uwsgi
来将视频/mp4文件流式传输到播放器。
我的代码如下:
def stream(request):
with open('/path/video.mp4', 'r') as video_file:
response = HttpResponse(video_file.read(), content_type='video/mp4')
response['Content-Disposition'] = 'inline; filename=%s' % 'video.mp4'
return response
video_file.close
当我使用一些小于1MB的小视频时,它可以在浏览器中流式传输,但是在iPhone播放器中出现了以下错误:
[uwsgi-http key: 127.0.0.1:8008 client_addr: 192.168.0.172 client_port: 14563] hr_write(): Broken pipe [plugins/http/http.c line 564]
当视频大小超过5MB时,在浏览器和iPhone播放器中都无法流式传输,都会出现相同的错误。
我尝试通过使用StreamHttpRespose分块返回来实现这个目标,代码如下:
def read(chunksize=8192):
with open('/path/video.mp4', 'rb') as video_file:
byte = video_file.read(chunksize)
while byte:
yield byte
return StreamingHttpResponse(read(), content_type='video/mp4')
但是仍然出现相同的错误:Broken pipe
。
我可以流式传输PDF和图像文件。这个问题只出现在MP4文件中。我已将content_type更改为“video-mpeg”,浏览器下载了该文件,而我想要防止文件下载。你有什么想法吗?有解决方法吗?