Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Flask播放多个视频?_Python_Flask - Fatal编程技术网

Python 使用Flask播放多个视频?

Python 使用Flask播放多个视频?,python,flask,Python,Flask,使用flask进行视频流,两者都建议使用生成器进行流式处理。下面是我的一个视频流的工作示例但我的问题是:如何同时流式传输多个视频? 我的文件夹结构如下所示: root_folder/ templates/ index.html server.py video1.mp4 video2.mp4 server.py Camera类读取视频文件并转换为.jpeg格式。路由“/video_feed/”指向具有指定索引的视频馈送 import cv2 fr

使用flask进行视频流,两者都建议使用
生成器进行流式处理。下面是我的一个视频流的工作示例但我的问题是:如何同时流式传输多个视频?

我的文件夹结构如下所示:

root_folder/
    templates/
        index.html
    server.py
    video1.mp4
    video2.mp4

server.py
Camera
类读取视频文件并转换为
.jpeg
格式。路由
“/video_feed/”
指向具有指定
索引的视频馈送

import cv2
from flask import Flask, render_template, Response

app = Flask(__name__)


class Camera:
    def __init__(self, index):
        self.path = f'video{index}.mp4'
        self.cap = cv2.VideoCapture(self.path)

    def get_frame(self):
        while 1:
            success, image = self.cap.read()
            # to replay video infinitely
            if not success:
                print('replay')
                self.cap = cv2.VideoCapture(self.path)
                success, image = self.cap.read()

            _, encoded = cv2.imencode(".jpg", image)

            yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
                  bytearray(encoded) + b'\r\n')


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/video_feed/<index>')
def video_feed(index):
    return Response(Camera(index).get_frame(),
                    mimetype='multipart/x-mixed-replace; boundary=frame')


if __name__ == '__main__':
    app.run(debug=True)
导入cv2
从烧瓶导入烧瓶,呈现_模板,响应
app=烧瓶(名称)
类摄像机:
定义初始化(自,索引):
self.path=f'video{index}.mp4'
self.cap=cv2.VideoCapture(self.path)
def get_帧(自身):
而1:
成功,image=self.cap.read()
#无限重放视频
如果不成功:
打印('重播')
self.cap=cv2.VideoCapture(self.path)
成功,image=self.cap.read()
_,encoded=cv2.imencode(“.jpg”,图像)
产量(b'--帧\r\n'b'内容类型:图像/jpeg\r\n\r\n'+
字节数组(编码)+b'\r\n')
@应用程序路径(“/”)
def index():
返回渲染模板('index.html')
@app.route(“/video_feed/”)
def视频_提要(索引):
返回响应(摄像机(索引).get_frame(),
mimetype='multipart/x-mixed-replace;boundary=frame')
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run(debug=True)
index.html

当只有一个
img
标签请求视频馈送时,这非常有效。但是如何同时流多个视频?当我取消对另一个
img
标签的注释时,前端卡住了。数据流没有播放,CPU使用率飙升

我想我完全搞砸了。我的代码怎么了

在普通的python程序中(不是使用flask),我会使用多进程或线程处理多个视频。然而,我对flask中的线程概念感到困惑,不知道如何实现


如有任何建议,我们将不胜感激。谢谢大家!

我不是法尔斯克大师,但在与同样的问题斗争之后,我唯一的想法是:

  • 将标识符添加到流中,如:
  • 无论哪种方式,我都看不出如何避免全局变量从字符串映射到实例的需要

  • 此外,我不知道如何轻松地对视频流进行垃圾收集

    • 一种方法是让JS在窗口关闭之前发送消息,这似乎不是一种可靠的方法
    • 另一种是存储最后发送帧的时间戳,并手动终止
  • 话虽如此,我仍在努力让Flask继续向JS提供图像,同时我也停止了img刷新计时器(因此浏览器中的视频被停止,但Flask仍热情地继续向任何人发送图像)
  • <img src="{{ url_for('video_feed', index='1') }}">
    <!--<img src="{{ url_for('video_feed', index='2') }}">-->
    
        @app.route('/video/<vid_id>.html')
        def render_video(vid_id):
            return render_template('pages/video_stream.html', name=vid_id)
    
        @app.route('/video_feed/<vid_id>')
        def video_feed(vid_id):
            print("Feeding video %s" % vid_id)
            # Do whatever