I’m trying to display video processed by OpenCV in a web browser similar to how ShoutCast does it. I know that ShoutCast somehow does it over HTTP protocol, but I don’t know how to make it display both the video and have the page loaded, rather than constantly loading until the processing is complete. I also know that ShoutCast doesn’t send anything to the stream page because when I launch developer tools, nothing is sent periodically, yet time keeps increasing and the music keeps playing continuously. Does anyone know how I could do this?
My Testing Code:
<code>import os
from flask import Flask, Response
import cv2
from utils.app_root_dir import app_root_dir
app = Flask(__name__)
def generate_frames():
cap = cv2.VideoCapture(os.path.normpath(app_root_dir().joinpath("data/temp", "video-979257305707693982.mp4")))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield (b'--framern'
b'Content-Type: image/jpegrnrn' + frame + b'rn')
@app.route('/video-stream')
def video_feed():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame', status=None)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, threaded=True)
</code>
<code>import os
from flask import Flask, Response
import cv2
from utils.app_root_dir import app_root_dir
app = Flask(__name__)
def generate_frames():
cap = cv2.VideoCapture(os.path.normpath(app_root_dir().joinpath("data/temp", "video-979257305707693982.mp4")))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield (b'--framern'
b'Content-Type: image/jpegrnrn' + frame + b'rn')
@app.route('/video-stream')
def video_feed():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame', status=None)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, threaded=True)
</code>
import os
from flask import Flask, Response
import cv2
from utils.app_root_dir import app_root_dir
app = Flask(__name__)
def generate_frames():
cap = cv2.VideoCapture(os.path.normpath(app_root_dir().joinpath("data/temp", "video-979257305707693982.mp4")))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield (b'--framern'
b'Content-Type: image/jpegrnrn' + frame + b'rn')
@app.route('/video-stream')
def video_feed():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame', status=None)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, threaded=True)