Skip to content

Instantly share code, notes, and snippets.

@usr-ein
Created September 17, 2021 13:22
Show Gist options
  • Save usr-ein/b3bc6aa48a01d86a82beae24f821166b to your computer and use it in GitHub Desktop.
Save usr-ein/b3bc6aa48a01d86a82beae24f821166b to your computer and use it in GitHub Desktop.
Video stream from JPEG - Create an HTTP video stream from a file that periodically gets overwritten by another process
#!/usr/bin/env python3
import io
import os
from sys import argv
from time import sleep
from flask import Flask, Response, render_template
from PIL import Image
app = Flask(__name__)
def gen():
im_path = argv[1]
while True:
sleep(1)
if not os.path.isfile(im_path):
continue
bytestream = io.BytesIO()
Image.open(im_path).save(bytestream, "JPEG")
frame = bytestream.getvalue()
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(
gen(), mimetype="multipart/x-mixed-replace; boundary=frame"
)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment