【问题标题】:Stream Video from memory using Flask in Python3在 Python3 中使用 Flask 从内存中流式传输视频
【发布时间】:2019-06-21 13:10:39
【问题描述】:

我一直在研究一个生成视频 (30fps) 并将其存储在内存中的项目。 我希望它使用 Flask 流式传输到浏览器,但我不希望视频暂时存储在驱动器上。 我尝试使用 send_file, response 方法,但没有得到任何结果。 这是 app.py :-

from flask import Flask, render_template, Response, send_file

app = Flask(__name__)

def generate_video():
    return b'video data in bytes'

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

@app.route('/static/a.mp4')
def stream_video():
    return Response(generate_video(), mimetype='video/mp4')

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=5000, debug=True)

我已经使用“PLYR”脚本制作了 HTML 视频播放器。

<html>
  <head>
    <link
      rel="stylesheet"
      href="{{ url_for('static', filename='plyr.css') }}"
    />
  </head>
  <body>
    <video id="plyr-video" controls>
      <source src="/static/a.mp4" />
      <!--<source src="{{ url_for('static',filename='a.mp4') }}" />-->
    </video>
    <script src="{{ url_for('static', filename='plyr.js') }}"></script>
    <script>
      plyr.setup("#plyr-video");
    </script>
  </body>
</html>

【问题讨论】:

    标签: python html python-3.x flask


    【解决方案1】:

    flask 中的网络服务器主脚本将允许您通过 /video_feed 路径流式传输每一帧。您不能以字节为单位发送所有视频:

    from flask import Flask, render_template, Response, jsonify
    from camera import VideoCamera
    import cv2
    
    app = Flask(__name__)
    
    video_stream = VideoCamera()
    
    @app.route('/')
    def index():
        return render_template('index.html')
    
    def generate_video(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\r\n')
    
    @app.route('/video_feed')
       def video_feed():
            return Response(generate_video(video_stream),
                        mimetype='multipart/x-mixed-replace; boundary=frame')
    
    if __name__ == '__main__':
        app.run(host='127.0.0.1', port="5000", debug=True)
    

    然后您需要 VideoCamera 类,您将处理每个帧,并且您可以在其中对帧进行您想要的每个预测或处理。我在这里使用了 OpenCv 和网络摄像头流,但你可以用你的视频来改变它。 camera.py 文件:

    class VideoCamera(object):
        def __init__(self):
            self.video = cv2.VideoCapture(0)
    
        def __del__(self):
            self.video.release()        
    
        def get_frame(self):
            ret, frame = self.video.read()
    
            # DO WHAT YOU WANT WITH TENSORFLOW / KERAS AND OPENCV
    
            ret, jpeg = cv2.imencode('.jpg', frame)
    
            return jpeg.tobytes()
    

    还有 index.html 文件:

    <html>
      <head>
        <link
          rel="stylesheet"
          href="{{ url_for('static', filename='plyr.css') }}"
        />
      </head>
      <body>
          <img src="{{ url_for('video_feed') }}" />
      </body>
    </html>
    
    猜你喜欢
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 2019-11-04
    • 2018-04-04
    • 2013-11-08
    • 2019-09-10
    相关资源
    最近更新 更多