#!/usr/bin/env python
from flask import Flask, render_template, Response
from camera import Camera
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
def gen(camera):
while True:
frame = camera.get_frame()
yield (b' frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/video_feed')
def video_feed():
return Response(gen(Camera()),mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
索引.html:
^{pr2}$
所有细节都在文章里。在
从那里…
在您的情况下,工作大大简化:
to stream pre-recorded video you can just serve the video file as a
regular file. You can encode it as mp4 with ffmpeg, for example, or if
you want something more sophisticated you can encode a
multi-resolution HLS stream. Either way you just need to serve the
static files, you don't need Flask for that.
米格尔·格林伯格(Miguel Grinberg)在他的博客上写了一篇关于这个主题的优秀文章Video Streaming with Flask。在
正如他所说和解释的那样,用烧瓶流就是这么简单:
应用程序副本
索引.html:
^{pr2}$所有细节都在文章里。在
从那里…
在您的情况下,工作大大简化:
来源:Miguel's Blog
您可能遇到过手动解决方案,但Flask已经有一个帮助功能,可以轻松地流式传输媒体文件。在
您需要使用
helpers.py
https://flask.palletsprojects.com/en/1.1.x/api/#flask.send_from_directory中的send_from_directory
方法你可以这样使用它:
相关问题 更多 >
编程相关推荐