Modifying output to use H.264.
This commit is contained in:
42
src/app.py
42
src/app.py
@@ -1,16 +1,14 @@
|
||||
import logging
|
||||
from collections.abc import Generator
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from flask import Flask, Response, jsonify, render_template
|
||||
from flask import Flask, Response, abort, jsonify, render_template, send_from_directory
|
||||
|
||||
from src.camera import camera
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logging.basicConfig(level=logging.WARNING)
|
||||
|
||||
|
||||
@app.get("/heartbeat")
|
||||
@@ -43,17 +41,35 @@ def camera_stop() -> tuple[Response, int]:
|
||||
return jsonify({"status": "stopped"}), 200
|
||||
|
||||
|
||||
@app.get("/camera/stream")
|
||||
def camera_stream() -> Response:
|
||||
def generate() -> Generator[bytes, Any, Any]:
|
||||
for frame in camera.frames():
|
||||
yield (b"--frame\r\n" b"Content-Type: image/jpeg\r\n\r\n" + frame + b"\r\n")
|
||||
|
||||
return Response(
|
||||
generate(),
|
||||
mimetype="multipart/x-mixed-replace; boundary=frame",
|
||||
@app.get("/camera/status")
|
||||
def camera_status() -> tuple[Response, int]:
|
||||
return (
|
||||
jsonify(
|
||||
{
|
||||
"running": camera.running,
|
||||
"ready": camera.wait_until_ready(timeout=0),
|
||||
}
|
||||
),
|
||||
200,
|
||||
)
|
||||
|
||||
|
||||
@app.get("/camera/hls/<path:filename>")
|
||||
def hls_segment(filename: str) -> Response:
|
||||
hls_dir = camera.hls_dir
|
||||
if not hls_dir.exists():
|
||||
abort(404)
|
||||
|
||||
# set correct MIME types for HLS files
|
||||
if filename.endswith(".m3u8"):
|
||||
mimetype = "application/vnd.apple.mpegurl"
|
||||
elif filename.endswith(".ts"):
|
||||
mimetype = "video/mp2t"
|
||||
else:
|
||||
abort(404)
|
||||
|
||||
return send_from_directory(str(hls_dir), filename, mimetype=mimetype)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=5000, debug=True, threaded=True)
|
||||
|
||||
Reference in New Issue
Block a user