Django實現(xiàn)視頻播放的具體示例
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: ?# 沒有數(shù)據(jù)了 退出 ? ? ? ? ? ? ? ? break ? ? ? ? ? ? if remaining: ? ? ? ? ? ? ? ? remaining -= len(data) ? ? ? ? ? ? yield data def stream_video(request): ? ? """將視頻文件以流媒體的方式響應(yīng)""" ? ? 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') ? #這里根據(jù)實際情況改變,我的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=后面的參數(shù)的值 ? ? 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的數(shù)據(jù) ? ? ? ? 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標(biāo)簽 </video> </video> </body> <script> ? ? $(function () { ? ? ? ? ? ? $("#media").attr('src', '/test_resp/?path=/media/video.mp4'); ? ? ?}) </script> </html>
到此這篇關(guān)于Django實現(xiàn)視頻播放的具體示例的文章就介紹到這了,更多相關(guān)Django 視頻播放 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python+Splinter實現(xiàn)12306搶票功能
這篇文章主要為大家詳細(xì)介紹了python+Splinter實現(xiàn)12306搶票功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09python如何每天在指定時間段運行程序及關(guān)閉程序
這篇文章主要介紹了python如何每天在指定時間段運行程序及關(guān)閉程序問題,具有很好的參考價值,希望對大家有所幫助。2023-04-04PyQt5的PyQtGraph實踐系列3之實時數(shù)據(jù)更新繪制圖形
這篇文章主要介紹了PyQt5的PyQtGraph實踐系列3之實時數(shù)據(jù)更新繪制圖形,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05python一行代碼就能實現(xiàn)數(shù)據(jù)分析的pandas-profiling庫
這篇文章主要為大家介紹了python一行代碼就能實現(xiàn)數(shù)據(jù)分析的pandas-profiling庫,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01一文搞懂Python中pandas透視表pivot_table功能
透視表是一種可以對數(shù)據(jù)動態(tài)排布并且分類匯總的表格格式?;蛟S大多數(shù)人都在Excel使用過數(shù)據(jù)透視表,也體會到它的強大功能,而在pandas中它被稱作pivot_table,今天通過本文給大家介紹Python中pandas透視表pivot_table功能,感興趣的朋友一起看看吧2021-11-11Pandas?計算相關(guān)性系數(shù)corr()方式
這篇文章主要介紹了Pandas?計算相關(guān)性系數(shù)corr()方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07python實現(xiàn)AHP算法的方法實例(層次分析法)
這篇文章主要給大家介紹了關(guān)于python實現(xiàn)AHP算法(層次分析法)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09