Python Flask 流媒体传输
相关内容
示例
传输一个mp4流,使游览器播放:
from flask import Flask,Response
app = Flask(__name__)
def Video_stream(file):
with open(file,'rb') as f:
data = f.read(5120) #每次读取5120字节
while data:
print len(data),'-',
yield data
data = f.read(5120)
@app.route('/')
def index():
return Response(Video_stream('./test.mp4'),mimetype='video/mp4') #Content-Type:video/mp4
if __name__ == '__main__':
app.run(host='0.0.0.0',port='80',debug=True)
常用流媒体标头
mp3 = "audio/mpeg"
mp4 = "video/mp4"
mp4v = "video/mp4"
mpeg = "video/mpeg"
mpg = "video/mpeg"
wav = "audio/x-wav"
webm = "video/webm"
wma = "audio/x-ms-wma"
wmv = "video/x-ms-wmv"
wmx = "video/x-ms-wmx"
部署指南
为了适应高并发,https支持(比如gevent
就不支持)等条件时,往往会加入中间件,比如Nginx
,把这些流媒体交给Nginx处理,可以 减轻Flask应用应用的压力,提高效率。