Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move clip.mp4 backend to clips folder #11834

Merged
merged 3 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docker/main/rootfs/usr/local/nginx/conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ http {
image/jpeg jpg;
}

expires 7d;
add_header Cache-Control "public";
autoindex on;
root /media/frigate;
}
Expand Down
8 changes: 4 additions & 4 deletions frigate/api/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def recording_clip(camera_name, start_ts, end_ts):
)

file_name = secure_filename(file_name)
path = os.path.join(CACHE_DIR, file_name)
path = os.path.join(CLIPS_DIR, f"cache/{file_name}")

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

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

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

if len(file_name) > 1000:
return make_response(
Expand Down
2 changes: 1 addition & 1 deletion frigate/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def ensure_dirs(self) -> None:
for d in [
CONFIG_DIR,
RECORD_DIR,
CLIPS_DIR,
f"{CLIPS_DIR}/cache",
CACHE_DIR,
MODEL_CACHE_DIR,
EXPORT_DIR,
Expand Down
19 changes: 14 additions & 5 deletions frigate/record/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from playhouse.sqlite_ext import SqliteExtDatabase

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

def clean_tmp_previews(self) -> None:
"""delete any previews in the cache that are more than 1 hour old."""
for p in Path(CACHE_DIR).rglob("preview_*.mp4"):
logger.debug(f"Checking preview {p}.")
if p.stat().st_mtime < (datetime.datetime.now().timestamp() - 60 * 60):
logger.debug("Deleting preview.")
clear_and_unlink(p)

def clean_tmp_clips(self) -> None:
"""delete any clips in the cache that are more than 5 minutes old."""
for p in Path(CACHE_DIR).rglob("clip_*.mp4"):
"""delete any clips in the cache that are more than 1 hour old."""
for p in Path(os.path.join(CLIPS_DIR, "cache")).rglob("clip_*.mp4"):
logger.debug(f"Checking tmp clip {p}.")
if p.stat().st_mtime < (datetime.datetime.now().timestamp() - 60 * 1):
if p.stat().st_mtime < (datetime.datetime.now().timestamp() - 60 * 60):
logger.debug("Deleting tmp clip.")
clear_and_unlink(p)

Expand Down Expand Up @@ -335,7 +343,7 @@ def run(self) -> None:
logger.info("Exiting recording cleanup...")
break

self.clean_tmp_clips()
self.clean_tmp_previews()

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

if counter == 0:
self.clean_tmp_clips()
self.expire_recordings()
remove_empty_directories(RECORD_DIR)
self.truncate_wal()
2 changes: 1 addition & 1 deletion frigate/record/maintainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async def move_files(self) -> None:
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_")
and not d.startswith("preview_")
]

files_in_use = []
Expand Down
2 changes: 1 addition & 1 deletion frigate/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def get_latest_segment_datetime(self, latest_segment: datetime.datetime) -> int:
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_")
and not d.startswith("preview_")
]
)
newest_segment_time = latest_segment
Expand Down
Loading