--- allow_origins: - '*' allow_xss: false auto-reload: false auto-reload-interval: 5.0 mimetype: text/html no_header: false no_navheader: false require_method: - GET - POST require_permissions: - view_nvr resource-timestamp: 1646332981520065 resource-type: page streaming-response: true template_engine: mako --- <%! #Code Here runs once when page is first rendered. Good place for import statements. import subprocess import os from kaithem.api.devices import all_devices from kaithem.api.web import has_permission %> <% device = all_devices[path[1]] for i in all_devices[path[1]].config.get('kaithem.read_perms','').strip().split(","): if not has_permission(i): raise RuntimeError("Missing permission for that camera: "+repr(i)) dir = os.path.expanduser(device.config['device.storage_dir']) for i in path: if i.startswith("/"): raise RuntimeError("security") clip = os.path.join(dir,*path[1:]) if (device.segmentDir and device.segmentDir in clip) or (device.activeSegmentDir and device.activeSegmentDir in clip): raise RuntimeError("Cannot fetch clip that is still recording") with open(clip) as f: f = f.read() if not "#EXT-X-ENDLIST" in f: with open(os.path.join(clip), "a+") as f: f.write("\r\n#EXT-X-ENDLIST\r\n") env = {} env.update(os.environ) p = subprocess.Popen(["ffmpeg", "-i", clip, "-bsf:a", "aac_adtstoasc", "-vcodec", "copy", "-c", "copy", "-crf", "50", '-movflags', 'frag_keyframe+empty_moov', '-f', 'mp4', "pipe:1"], stdout=subprocess.PIPE, env=env) serve_file(p.stdout, "video/mp4", path[1]+".mp4" ) %>