流媒体电影Python flas

2024-03-28 21:36:37 发布

您现在位置:Python中文网/ 问答频道 /正文

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

谢谢你


Tags: 项目flask视频电影关系流式主页磁盘
2条回答

米格尔·格林伯格(Miguel Grinberg)在他的博客上写了一篇关于这个主题的优秀文章Video Streaming with Flask。在

正如他所说和解释的那样,用烧瓶流就是这么简单:

应用程序副本

#!/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's Blog

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

您需要使用helpers.pyhttps://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,
            )

相关问题 更多 >