網站首頁 編程語言 正文
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):
?? ?# 每次最多讀取8Kb
? ? 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<START>\d+)\s*-\s*(?P<END>\d*)', re.I)
? ? range_match = range_re.match(range_header)
? ? path = request.GET.get('path')
? #這里根據實際情況改變,我的views.py在core文件夾下但是folder_path卻只到core的上一層,media也在core文件夾下
? ? video_path = os.path.join(settings.BASE_DIR, 'static', 'video') ?# 視頻放在目錄的static下的video文件夾中
? ? file_path = os.path.join(video_path, path) #path就是template ?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播放到的位置
? ? ?? ?# 下次播放的位置?
? ? ? ? first_byte, last_byte = range_match.group('START'), range_match.group('END')
? ? ? ? first_byte = int(first_byte) if first_byte else 0
? ? ? ? # 從播放的位置往后讀取10M的數據
? ? ? ? last_byte = first_byte + 1024 * 1024 * 10
? ? ? ? if last_byte >= size: ?# 如果想讀取的位置大于文件大小
? ? ? ? ? ? last_byte = size - 1 ?# 最后將圖片全部讀完
? ? ? ? length = last_byte - first_byte + 1 ?# 此次讀取的長度(字節(jié))
? ? ? ? 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
?
前端
<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <title>Title</title>
? ? <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>
原文鏈接:https://blog.csdn.net/weixin_45111060/article/details/123447975
相關推薦
- 2022-11-20 spring?boot集成redis基礎入門實例詳解_Redis
- 2023-02-10 Redis如何正確關閉和開啟持久化_Redis
- 2022-04-16 Django中外鍵ForeignKey介紹使用_python
- 2022-03-24 .Net?Core服務治理Consul使用服務發(fā)現(xiàn)_自學過程
- 2022-09-29 關于react?ant?組件?Select下拉框?值回顯的問題_React
- 2022-08-12 Python3.8安裝tensorflow的簡單方法步驟_python
- 2023-10-17 css標簽畫圓形進度條
- 2023-07-02 python中編寫config文件并及時更新的方法_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支