流媒体电影 Python 烧瓶

Streaming MOVIE Python flask

我有一个关于流媒体电影 (720p) 的小项目。在 Python Flask 中,谁能给我一个示例,说明如何在 python Flask 中从本地磁盘流式传输视频。所以它在主页上播放。为此可以使用哪些依赖项。

谢谢

有一篇关于这个主题的优秀文章,Video Streaming with Flask,由 Miguel Grinberg 在他的博客上撰写。

正如他所说和解释得很好,使用 Flask 进行流式处理就是这么简单:

app.py

#!/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)

index.html:

<html>
  <head>
    <title>Video Streaming Demonstration</title>
  </head>
  <body>
    <h1>Video Streaming Demonstration</h1>
    <img src="{{ url_for('video_feed') }}">
  </body>
</html>

所有细节都在文章中。

从那里...

在你的情况下,工作大大简化了:

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's Blog

您可能遇到过手动解决方案,但 Flask 已经具有帮助您轻松流式传输媒体文件的功能。

您需要使用 helpers.py https://flask.palletsprojects.com/en/1.1.x/api/#flask.send_from_directory

中的 send_from_directory 方法

你可以这样使用它:

@app.route("/movies", methods=["GET"])
def get_movie():
    return send_from_directory(
                app.config["UPLOAD_FOLDER"],
                "your_movie.png",
                conditional=True,
            )