view视图
import re
import os
import mimetypes
from wsgiref.util import FileWrapper
from django.http import StreamingHttpResponse
from django.shortcuts import render
from django.conf import settings
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):
"""将视频文件以流媒体的方式响应"""
range_header = request.META.get('HTTP_RANGE', '').strip()
range_re = re.compile(r'bytes\s*=\s*(?P\d+)\s*-\s*(?P\d*)', re.I)
range_match = range_re.match(range_header)
path = request.GET.get('path')
video_path = os.path.join(settings.BASE_DIR, 'static', 'video')
file_path = os.path.join(video_path, path)
size = os.path.getsize(file_path)
content_type, encoding = mimetypes.guess_type(file_path)
content_type = content_type or 'application/octet-stream'
if range_match:
first_byte, last_byte = range_match.group('START'), range_match.group('END')
first_byte = int(first_byte) if first_byte else 0
last_byte = first_byte + 1024 * 1024 * 10
if last_byte >= size:
last_byte = size - 1
length = last_byte - first_byte + 1
resp = StreamingHttpResponse(file_iterator(file_path, offset=first_byte, length=length), status=200, 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(file_path, 'rb')), content_type=content_type)
resp['Content-Length'] = str(size)
resp['Accept-Ranges'] = 'bytes'
return resp
前端
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js">script>
head>
<body>
<video id="media" src="" width="720" height="480" controls autoplay>浏览器不支持video标签 video>
video>
body>
<script>
$(function () {
$("#media").attr('src', '/test_resp/?path=/media/video.mp4');
})
script>
html>