You can use the flash framework for this.
This is not just for mjpeg.
I adapted the code from here: https://blog.miguelgrinberg.com/post/video-streaming-with-flask
APP.py
#!/usr/bin/env python from importlib import import_module import os from flask import Flask, render_template, Response # import camera driver if os.environ.get('CAMERA'): Camera = import_module('camera_' + os.environ['CAMERA']).Camera else: from camera import Camera # Raspberry Pi camera module (requires picamera package) # from camera_pi import Camera app = Flask(__name__) @app.route('/') def index(): """Video streaming home page.""" return render_template('index.html') def gen(camera): """Video streaming generator function.""" 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(): """Video streaming route. Put this in the src attribute of an img tag.""" return Response(gen(Camera()), mimetype='multipart/x-mixed-replace; boundary=frame') if __name__ == '__main__': app.run(host='0.0.0.0', threaded=True)
base_camera.py
import time import threading try: from greenlet import getcurrent as get_ident except ImportError: try: from thread import get_ident except ImportError: from _thread import get_ident class CameraEvent(object): """An Event-like class that signals all active clients when a new frame is available. """ def __init__(self): self.events = {} def wait(self): """Invoked from each client thread to wait for the next frame.""" ident = get_ident() if ident not in self.events:
camera.py
#D:\gstreamer\1.0\x86\bin>gst-launch-1.0.exe multifilesrc loop=true start-index=0 stop-index=0 location=d:/python/temp.png ! decodebin ! identity sleep-time=1000000 ! videoconvert ! autovideosink import shutil import time import os,sys from PIL import Image, ImageFont, ImageDraw, ImageFile from io import BytesIO from base_camera import BaseCamera im = Image.new("RGB", (300, 30), (220, 180, 180)) #im.format'JPEG' dr = ImageDraw.Draw(im) font = ImageFont.truetype(os.path.join("fonts", "msyh.ttf"), 16) text =time.strftime("%m/%d %H:%M:%S") +u"这是一段测试文本。" dr.text((10, 5), text, font=font, fill="#000000") im.save("d://python/temp.jpg") dr.rectangle((0,0,300,500),fill="#FFFFFF") text =time.strftime("%m/%d %H:%M:%S") +u"这是一段测试文本。" dr.text((10, 5),text, font=font, fill="#000000") f = BytesIO() f.name="sdf.jpg" im.save(f,"JPEG") f.seek(0) f.close() class Camera(BaseCamera): """An emulated camera implementation that streams a repeated sequence of files 1.jpg, 2.jpg and 3.jpg at a rate of one frame penr second.""" imgs = [open(f + '.jpg', 'rb').read() for f in ['1', '2', '3']] @staticmethod def frames(): while True: text =time.strftime("%m/%d %H:%M:%S") +u"这是一段测试文本。" dr.rectangle((0,0,300,500),fill="#FFFFFF") dr.text((10, 5), text, font=font, fill="#000000") f = BytesIO() im.save(f,'JPEG') try : im.save("d:/python/temp.jpg") except : print("Unexpected error:", sys.exc_info()[0]) pass # shutil.copy("d:/python/temp2.png","d:/python/temp.png") f.seek(0) time.sleep(1) yield f.read() #Camera.imgs[int(time.time()) % 3]