简体   繁体   English

Python flask 视频流无法处理多个客户端

[英]Python flask video streaming can't handle multiple clients

I am trying to display a video in a HTML file with the python flask library.我正在尝试使用 python flask 库在 HTML 文件中显示视频。 Online I found pleanty of code very similar to each other.在网上我发现很多代码彼此非常相似。 I ended up with the code below我最终得到了下面的代码

Code:代码:

from flask import Flask, render_template, Response
import cv2

app = Flask(__name__)
camera = cv2.VideoCapture('path to file')
@app.route('/')
def index():
    return render_template('index.html')

def gen(camera):
    while True:
        success, frame = camera.read()
        ret, buffer = cv2.imencode('.jpg', frame)
        frame = buffer.tobytes()
        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(debug=True)

HTML: HTML:

<!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
          integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

    <title>Live Streaming Demonstration</title>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-lg-8  offset-lg-2">
            <h3 class="mt-5">Live Streaming</h3>
            <img src="{{ url_for('video_feed') }}" width="100%">
        </div>
    </div>
</div>
</body>
</html>

When I try to run my code with just one client it works fine, but when I open it in 2 different browsers at the same time the frames freeze and I receive the following error:当我尝试仅使用一个客户端运行我的代码时,它工作正常,但是当我同时在 2 个不同的浏览器中打开它时,帧冻结并且我收到以下错误:

Assertion avci->compat_decode_consumed == 0 failed at libavcodec/decode.c:822断言 avci->compat_decode_consumed == 0 在 libavcodec/decode.c:822 失败

Is their some way I can fix this?他们有办法解决这个问题吗?

Flask is synchronous and single threaded, that can simultaneously serve as many connections, however it is not suitable for remote video, no matter what protocol you use. Flask 是同步单线程的,可以同时服务多个连接,但是无论你使用什么协议,它都不适合远程视频。 It may work with local web-cam, but definitely not rtsp.它可能适用于本地网络摄像头,但绝对不适用于 rtsp。

Problem defined here: https://blog.miguelgrinberg.com/post/video-streaming-with-flask/page/10此处定义的问题: https://blog.miguelgrinberg.com/post/video-streaming-with-flask/page/10

A workaround is using a very complicated set of threads as discussed here: https://blog.miguelgrinberg.com/post/flask-video-streaming-revisited一种解决方法是使用一组非常复杂的线程,如下所述: https://blog.miguelgrinberg.com/post/flask-video-streaming-revisited

You should try asynchronous frameworks, eg Sanic, this code will give a you a good starting point, very buggy but it works on 3 devices simultaneously for few seconds before it crashed: https://github.com/kxxoling/sanic_video_streaming您应该尝试异步框架,例如 Sanic,此代码将为您提供一个很好的起点,非常有问题,但它在崩溃前可同时在 3 个设备上运行几秒钟: https://github.com/kxxoling/sanic_video_streaming

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM