Skip to content

Commit 30b8627

Browse files
move clip.mp4 backend to clips folder (#11834)
* move clip.mp4 backend to clips folder * improve caching * fix check
1 parent 5f3c352 commit 30b8627

File tree

6 files changed

+23
-12
lines changed

6 files changed

+23
-12
lines changed

docker/main/rootfs/usr/local/nginx/conf/nginx.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ http {
131131
image/jpeg jpg;
132132
}
133133

134+
expires 7d;
135+
add_header Cache-Control "public";
134136
autoindex on;
135137
root /media/frigate;
136138
}

frigate/api/media.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ def recording_clip(camera_name, start_ts, end_ts):
459459
)
460460

461461
file_name = secure_filename(file_name)
462-
path = os.path.join(CACHE_DIR, file_name)
462+
path = os.path.join(CLIPS_DIR, f"cache/{file_name}")
463463

464464
if not os.path.exists(path):
465465
ffmpeg_cmd = [
@@ -511,7 +511,7 @@ def recording_clip(camera_name, start_ts, end_ts):
511511
response.headers["Content-Disposition"] = "attachment; filename=%s" % file_name
512512
response.headers["Content-Length"] = os.path.getsize(path)
513513
response.headers["X-Accel-Redirect"] = (
514-
f"/cache/{file_name}" # nginx: https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ignore_headers
514+
f"/clips/cache/{file_name}" # nginx: https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ignore_headers
515515
)
516516

517517
return response
@@ -1232,8 +1232,8 @@ def preview_gif(camera_name: str, start_ts, end_ts, max_cache_age=2592000):
12321232

12331233
@MediaBp.route("/<camera_name>/start/<int:start_ts>/end/<int:end_ts>/preview.mp4")
12341234
@MediaBp.route("/<camera_name>/start/<float:start_ts>/end/<float:end_ts>/preview.mp4")
1235-
def preview_mp4(camera_name: str, start_ts, end_ts, max_cache_age=2592000):
1236-
file_name = f"clip_{camera_name}_{start_ts}-{end_ts}.mp4"
1235+
def preview_mp4(camera_name: str, start_ts, end_ts, max_cache_age=604800):
1236+
file_name = f"preview_{camera_name}_{start_ts}-{end_ts}.mp4"
12371237

12381238
if len(file_name) > 1000:
12391239
return make_response(

frigate/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def ensure_dirs(self) -> None:
100100
for d in [
101101
CONFIG_DIR,
102102
RECORD_DIR,
103-
CLIPS_DIR,
103+
f"{CLIPS_DIR}/cache",
104104
CACHE_DIR,
105105
MODEL_CACHE_DIR,
106106
EXPORT_DIR,

frigate/record/cleanup.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from playhouse.sqlite_ext import SqliteExtDatabase
1212

1313
from frigate.config import CameraConfig, FrigateConfig, RetainModeEnum
14-
from frigate.const import CACHE_DIR, MAX_WAL_SIZE, RECORD_DIR
14+
from frigate.const import CACHE_DIR, CLIPS_DIR, MAX_WAL_SIZE, RECORD_DIR
1515
from frigate.models import Event, Previews, Recordings, ReviewSegment
1616
from frigate.record.util import remove_empty_directories, sync_recordings
1717
from frigate.util.builtin import clear_and_unlink, get_tomorrow_at_time
@@ -28,11 +28,19 @@ def __init__(self, config: FrigateConfig, stop_event: MpEvent) -> None:
2828
self.config = config
2929
self.stop_event = stop_event
3030

31+
def clean_tmp_previews(self) -> None:
32+
"""delete any previews in the cache that are more than 1 hour old."""
33+
for p in Path(CACHE_DIR).rglob("preview_*.mp4"):
34+
logger.debug(f"Checking preview {p}.")
35+
if p.stat().st_mtime < (datetime.datetime.now().timestamp() - 60 * 60):
36+
logger.debug("Deleting preview.")
37+
clear_and_unlink(p)
38+
3139
def clean_tmp_clips(self) -> None:
32-
"""delete any clips in the cache that are more than 5 minutes old."""
33-
for p in Path(CACHE_DIR).rglob("clip_*.mp4"):
40+
"""delete any clips in the cache that are more than 1 hour old."""
41+
for p in Path(os.path.join(CLIPS_DIR, "cache")).rglob("clip_*.mp4"):
3442
logger.debug(f"Checking tmp clip {p}.")
35-
if p.stat().st_mtime < (datetime.datetime.now().timestamp() - 60 * 1):
43+
if p.stat().st_mtime < (datetime.datetime.now().timestamp() - 60 * 60):
3644
logger.debug("Deleting tmp clip.")
3745
clear_and_unlink(p)
3846

@@ -335,7 +343,7 @@ def run(self) -> None:
335343
logger.info("Exiting recording cleanup...")
336344
break
337345

338-
self.clean_tmp_clips()
346+
self.clean_tmp_previews()
339347

340348
if (
341349
self.config.record.sync_recordings
@@ -346,6 +354,7 @@ def run(self) -> None:
346354
next_sync = get_tomorrow_at_time(3)
347355

348356
if counter == 0:
357+
self.clean_tmp_clips()
349358
self.expire_recordings()
350359
remove_empty_directories(RECORD_DIR)
351360
self.truncate_wal()

frigate/record/maintainer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ async def move_files(self) -> None:
8282
for d in os.listdir(CACHE_DIR)
8383
if os.path.isfile(os.path.join(CACHE_DIR, d))
8484
and d.endswith(".mp4")
85-
and not d.startswith("clip_")
85+
and not d.startswith("preview_")
8686
]
8787

8888
files_in_use = []

frigate/video.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def get_latest_segment_datetime(self, latest_segment: datetime.datetime) -> int:
300300
for d in os.listdir(CACHE_DIR)
301301
if os.path.isfile(os.path.join(CACHE_DIR, d))
302302
and d.endswith(".mp4")
303-
and not d.startswith("clip_")
303+
and not d.startswith("preview_")
304304
]
305305
)
306306
newest_segment_time = latest_segment

0 commit comments

Comments
 (0)