Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

try m4s

Rory Clear864b365757ab
diff --git a/clearcam.py b/clearcam.py
index 3891014..a0a3b98 100644
--- a/clearcam.py
+++ b/clearcam.py
@@ -338,8 +338,11 @@ class VideoCapture:
"-hls_time", "2",
"-hls_list_size", "0",
"-hls_playlist_type", "event",
+ "-hls_segment_type", "fmp4",
+ "-hls_fmp4_init_filename", "init.mp4",
"-hls_flags", "append_list+independent_segments+temp_file",
- "-hls_segment_filename", str(path / "stream_%06d.ts"),
+ "-hls_segment_filename", str(path / "stream_%06d.m4s"),
+ "-start_number", "0",
str(path / "stream.m3u8")
]
hls_proc = subprocess.Popen(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
@@ -1009,6 +1012,8 @@ class HLSRequestHandler(BaseHTTPRequestHandler):
self.send_header('Cache-Control', 'no-cache')
elif file_path.suffix == '.ts':
self.send_header('Content-Type', 'video/MP2T')
+ elif file_path.suffix == '.m4s':
+ self.send_header('Content-Type', 'video/iso.segment')
elif file_path.suffix == '.png':
self.send_header('Content-Type', 'image/jpeg')
self.end_headers()
diff --git a/utils/helpers.py b/utils/helpers.py
index 73402a1..a999afc 100644
--- a/utils/helpers.py
+++ b/utils/helpers.py
@@ -131,44 +131,49 @@ def resize(img, new_size):
return img
def export_clip(stream_dir, output_path: Path, live=False, length=5, end=0, start=None):
- segments = sorted(stream_dir.glob("*.ts"), key=os.path.getmtime)
+ segments = sorted(stream_dir.glob("*.m4s"), key=os.path.getmtime)
recent_segments = deque()
- cutoff = os.path.getmtime(segments[0]) + start if start is not None else time.time() - length if live else time.time() - length
- end = os.path.getmtime(segments[0]) + start + length if start is not None else time.time() - end
- recent_raw = [f for f in segments if os.path.getmtime(f) >= cutoff and os.path.getmtime(f) <= end]
+ first_segment_time = os.path.getmtime(segments[0])
+ if start is not None:
+ cutoff = first_segment_time + start
+ end_time = cutoff + length
+ else:
+ cutoff = time.time() - length
+ end_time = time.time() - end
+ recent_raw = [f for f in segments if cutoff <= os.path.getmtime(f) <= end_time]
recent_segments.extend(recent_raw)
- concat_list_path = stream_dir / "concat_list.txt"
- with open(concat_list_path, "w") as f: f.writelines(f"file '{segment.resolve()}'\n" for segment in recent_segments)
+ if live and recent_segments and recent_segments[-1] == segments[-1]: recent_segments.pop()
output_path.parent.mkdir(parents=True, exist_ok=True)
ffmpeg_path = find_ffmpeg()
- with open(stream_dir / "concat_list.txt", "r") as f: print(" ".join(line.strip() for line in f)) # todo
- crf = 32
- while True:
- command = [
+ init_path = stream_dir / "init.mp4"
+ combined_path = stream_dir / ".export_combined.mp4"
+
+ try:
+ with open(combined_path, "wb") as out:
+ with open(init_path, "rb") as f: out.write(f.read())
+ for segment in recent_segments:
+ with open(segment, "rb") as f: out.write(f.read())
+ crf = 32
+ while True:
+ command = [
ffmpeg_path,
"-y",
- "-f", "concat",
- "-safe", "0",
- "-i", str(concat_list_path),
+ "-i", str(combined_path),
*(["-vf", "scale=-2:240,fps=24,format=yuv420p", "-preset", "veryslow",] if live else []),
- "-c:v", "libx264",
+ "-c:v", "libx264",
"-pix_fmt", "yuv420p",
"-preset", "veryslow",
"-crf", str(crf),
"-an",
str(output_path)
- ]
- subprocess.run(command, check=True)
- crf += 5
- file_size = 10*1024*1024
- with open(output_path, "rb") as f:
- file_data = f.read()
- file_size = len(file_data)
- if file_size <= 9*1024*1024:
- break # max size 10MB, # todo, calculate time from ts files
- else:
+ ]
+ subprocess.run(command, check=True)
+ if output_path.stat().st_size < 9*1024*1024: break
crf += 5
+ finally:
+ if combined_path.exists(): combined_path.unlink()
+
def export_and_upload(cam_name, thumbnail, userID, key, start=None, end=0, length=20, wait=False):
if wait: time.sleep(10) # todo, objects can be too far ahead
os.makedirs(BASE_DIR / "cameras" / cam_name / "event_clips", exist_ok=True)