初期处理
借鉴https://www.jb51.net/article/144137.htm
import re
import os
from wsgiref.util import FileWrapper
from django.http import StreamingHttpResponse
def file_iterator(file_name, chunk_size=8192, offset=0, length=None):
with open(file_name, "rb") as f:
f.seek(offset, os.SEEK_SET)
remaining = length
while True:
bytes_length = chunk_size if remaining is None else min(remaining, chunk_size)
data = f.read(bytes_length)
if not data:
break
if remaining:
remaining -= len(data)
yield data
def stream_video(request, path):
"""将视频文件以流媒体的方式响应"""
range_header = request.META.get('HTTP_RANGE', '').strip()
range_re = re.compile(r'bytes\s*=\s*(\d+)\s*-\s*(\d*)', re.I)
range_match = range_re.match(range_header)
size = os.path.getsize(path)
content_type, encoding = mimetypes.guess_type(path)
content_type = content_type or 'application/octet-stream'
if range_match:
first_byte, last_byte = range_match.groups()
first_byte = int(first_byte) if first_byte else 0
last_byte = first_byte + 1024 * 1024 * 8 # 8M 每片,响应体最大体积
if last_byte >= size:
last_byte = size - 1
length = last_byte - first_byte + 1
resp = StreamingHttpResponse(file_iterator(path, offset=first_byte, length=length), status=206, content_type=content_type)
resp['Content-Length'] = str(length)
resp['Content-Range'] = 'bytes %s-%s/%s' % (first_byte, last_byte, size)
else:
# 不是以视频流方式的获取时,以生成器方式返回整个文件,节省内存
resp = StreamingHttpResponse(FileWrapper(open(path, 'rb')), content_type=content_type)
resp['Content-Length'] = str(size)
resp['Accept-Ranges'] = 'bytes'
return resp
可实现视频流响应
但经过实践,无法满足在ios下播放,原因:苹果为了节省用户流量,会先发一个字节的请求,bytes=0-1并判断你是否有Content-range响应。这个请求会获取到你的视频类型,总长度,成功之后才会发起第二个请求,响应状态为206,通常是断点续传,否则就不再发起请求。所以我们在用ios打开视频的时候,会闪一下出现进度条,然后是图像破裂的样式,显示播放失败
借鉴于https://blog.csdn.net/weixin_42042680/article/details/81321648
最终:
def file_iterator(file_name, chunk_size=8192, offset=0, length=None):
with open(file_name, "rb") as f:
f.seek(offset, os.SEEK_SET)
remaining = int(length)
while True:
bytes_length = chunk_size if remaining is None else min(remaining, chunk_size)
data = f.read(bytes_length)
if not data:
break
if remaining:
remaining -= len(data)
yield data
def stream_video(request, path):
"""将视频文件以流媒体的方式响应"""
range_header = request.META.get('HTTP_RANGE', '').strip()
range_re = re.compile(r'bytes\s*=\s*(\d+)\s*-\s*(\d*)', re.I)
range_match = range_re.match(range_header)
size = os.path.getsize(path)
content_type, encoding = mimetypes.guess_type(path)
content_type = content_type or 'application/octet-stream'
if range_match:
first_byte, last_byte = range_match.groups()
first_byte = int(first_byte) if first_byte else 0
end = -1
if len(last_byte) > 0:
end = int(last_byte)
if end == -1:
# 此处的md['size']是文件大小
length = str(size - first_byte)
else:
length = str(end - first_byte + 1)
if end < 0:
content_range_header_value = "bytes %d-%d/%d" % (first_byte, size - 1, size)
else:
content_range_header_value = "bytes %d-%d/%d" % (first_byte, end, size)
resp = StreamingHttpResponse(file_iterator(path, offset=first_byte, length=length), status=206,
content_type=content_type)
resp['Content-Length'] = length
# resp['Content-Range'] = 'bytes %s-%s/%s' % (first_byte, last_byte, size)
resp["Content-Range"] = content_range_header_value
else:
# 不是以视频流方式的获取时,以生成器方式返回整个文件,节省内存
resp = StreamingHttpResponse(FileWrapper(open(path, 'rb')), content_type=content_type)
resp['Content-Length'] = str(size)
resp['Accept-Ranges'] = 'bytes'
return resp
仅作记录