Browse Source

switch to mpegts format for cache and create mp4 with faststart

Blake Blackshear 3 years ago
parent
commit
c809494c98
2 changed files with 27 additions and 8 deletions
  1. 2 2
      frigate/config.py
  2. 25 6
      frigate/record.py

+ 2 - 2
frigate/config.py

@@ -298,7 +298,7 @@ RECORD_FFMPEG_OUTPUT_ARGS_DEFAULT = [
     "-segment_time",
     "10",
     "-segment_format",
-    "mp4",
+    "ts",
     "-reset_timestamps",
     "1",
     "-strftime",
@@ -562,7 +562,7 @@ class CameraConfig(FrigateBaseModel):
             )
             ffmpeg_output_args = (
                 record_args
-                + [f"{os.path.join(CACHE_DIR, self.name)}-%Y%m%d%H%M%S.mp4"]
+                + [f"{os.path.join(CACHE_DIR, self.name)}-%Y%m%d%H%M%S.ts"]
                 + ffmpeg_output_args
             )
 

+ 25 - 6
frigate/record.py

@@ -48,9 +48,7 @@ class RecordingMaintainer(threading.Thread):
         recordings = [
             d
             for d in os.listdir(CACHE_DIR)
-            if os.path.isfile(os.path.join(CACHE_DIR, d))
-            and d.endswith(".mp4")
-            and not d.startswith("clip_")
+            if os.path.isfile(os.path.join(CACHE_DIR, d)) and d.endswith(".ts")
         ]
 
         files_in_use = []
@@ -113,9 +111,30 @@ class RecordingMaintainer(threading.Thread):
             file_name = f"{start_time.strftime('%M.%S.mp4')}"
             file_path = os.path.join(directory, file_name)
 
-            # copy then delete is required when recordings are stored on some network drives
-            shutil.copyfile(cache_path, file_path)
-            os.remove(cache_path)
+            ffmpeg_cmd = [
+                "ffmpeg",
+                "-y",
+                "-i",
+                cache_path,
+                "-c",
+                "copy",
+                "-movflags",
+                "+faststart",
+                file_path,
+            ]
+
+            p = sp.run(
+                ffmpeg_cmd,
+                encoding="ascii",
+                capture_output=True,
+            )
+
+            Path(cache_path).unlink(missing_ok=True)
+
+            if p.returncode != 0:
+                logger.error(f"Unable to convert {cache_path} to {file_path}")
+                logger.error(p.stderr)
+                continue
 
             rand_id = "".join(
                 random.choices(string.ascii_lowercase + string.digits, k=6)