Files
AItst/AIMonitor/static_server.py
2026-02-08 14:33:45 +08:00

39 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
from pathlib import Path
from flask import Flask, send_from_directory, abort
# 静态视频根目录,应与 rtsp_service_ws.py 中的 VIDEO_OUTPUT_DIR 保持一致
BASE_DIR = Path(__file__).resolve().parent
VIDEO_ROOT = BASE_DIR / "videos"
app = Flask(__name__)
@app.route("/<int:camera_id>/<path:filename>")
def serve_video(camera_id: int, filename: str):
"""按 /<id>/<videofile>.mp4 形式访问视频文件。
这里简单地从 VIDEO_ROOT 下按文件名查找并返回文件。
如果你希望严格校验 id 与文件名中的 cam{id} 一致,可以在这里加一层判断。
"""
# 可选的安全检查:不允许跳出目录
if ".." in filename or filename.startswith("/"):
abort(400, "invalid filename")
if not VIDEO_ROOT.exists():
abort(404, "video root not found")
# 这里没有强制校验 camera_id 与文件名对应关系,只按文件名返回
# 例如http://host:5000/1/20251209_101010_cam1.mp4
# 实际文件路径为 ./videos/20251209_101010_cam1.mp4
if not (VIDEO_ROOT / filename).exists():
abort(404)
return send_from_directory(VIDEO_ROOT, filename)
if __name__ == "__main__":
# 默认监听 5000 端口,你可以按需修改 host/port
app.run(host="0.0.0.0", port=5000, debug=False)