2024-04-20 00:11:41 +02:00
|
|
|
"""Export apis."""
|
|
|
|
|
|
|
|
import logging
|
2024-09-30 23:55:44 +02:00
|
|
|
import random
|
|
|
|
import string
|
2024-04-20 00:11:41 +02:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Optional
|
|
|
|
|
2024-06-04 17:10:19 +02:00
|
|
|
import psutil
|
2024-09-24 15:05:30 +02:00
|
|
|
from fastapi import APIRouter, Request
|
|
|
|
from fastapi.responses import JSONResponse
|
2024-04-20 00:11:41 +02:00
|
|
|
from peewee import DoesNotExist
|
|
|
|
|
2024-09-24 15:05:30 +02:00
|
|
|
from frigate.api.defs.tags import Tags
|
2024-06-04 17:10:19 +02:00
|
|
|
from frigate.const import EXPORT_DIR
|
2024-04-20 00:11:41 +02:00
|
|
|
from frigate.models import Export, Recordings
|
|
|
|
from frigate.record.export import PlaybackFactorEnum, RecordingExporter
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2024-09-24 15:05:30 +02:00
|
|
|
router = APIRouter(tags=[Tags.export])
|
2024-04-20 00:11:41 +02:00
|
|
|
|
|
|
|
|
2024-09-24 15:05:30 +02:00
|
|
|
@router.get("/exports")
|
2024-04-20 00:11:41 +02:00
|
|
|
def get_exports():
|
|
|
|
exports = Export.select().order_by(Export.date.desc()).dicts().iterator()
|
2024-09-24 15:05:30 +02:00
|
|
|
return JSONResponse(content=[e for e in exports])
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/export/{camera_name}/start/{start_time}/end/{end_time}")
|
|
|
|
def export_recording(
|
|
|
|
request: Request,
|
|
|
|
camera_name: str,
|
|
|
|
start_time: float,
|
|
|
|
end_time: float,
|
|
|
|
body: dict = None,
|
|
|
|
):
|
|
|
|
if not camera_name or not request.app.frigate_config.cameras.get(camera_name):
|
|
|
|
return JSONResponse(
|
|
|
|
content=(
|
2024-04-20 00:11:41 +02:00
|
|
|
{"success": False, "message": f"{camera_name} is not a valid camera."}
|
|
|
|
),
|
2024-09-24 15:05:30 +02:00
|
|
|
status_code=404,
|
2024-04-20 00:11:41 +02:00
|
|
|
)
|
|
|
|
|
2024-09-24 15:05:30 +02:00
|
|
|
json: dict[str, any] = body or {}
|
2024-04-20 00:11:41 +02:00
|
|
|
playback_factor = json.get("playback", "realtime")
|
2024-07-17 15:39:37 +02:00
|
|
|
friendly_name: Optional[str] = json.get("name")
|
2024-04-20 00:11:41 +02:00
|
|
|
|
2024-07-17 15:39:37 +02:00
|
|
|
if len(friendly_name or "") > 256:
|
2024-09-24 15:05:30 +02:00
|
|
|
return JSONResponse(
|
|
|
|
content=({"success": False, "message": "File name is too long."}),
|
|
|
|
status_code=401,
|
2024-04-20 00:11:41 +02:00
|
|
|
)
|
|
|
|
|
2024-08-12 16:26:54 +02:00
|
|
|
existing_image = json.get("image_path")
|
|
|
|
|
2024-04-20 00:11:41 +02:00
|
|
|
recordings_count = (
|
|
|
|
Recordings.select()
|
|
|
|
.where(
|
|
|
|
Recordings.start_time.between(start_time, end_time)
|
|
|
|
| Recordings.end_time.between(start_time, end_time)
|
|
|
|
| ((start_time > Recordings.start_time) & (end_time < Recordings.end_time))
|
|
|
|
)
|
|
|
|
.where(Recordings.camera == camera_name)
|
|
|
|
.count()
|
|
|
|
)
|
|
|
|
|
|
|
|
if recordings_count <= 0:
|
2024-09-24 15:05:30 +02:00
|
|
|
return JSONResponse(
|
|
|
|
content=(
|
2024-04-20 00:11:41 +02:00
|
|
|
{"success": False, "message": "No recordings found for time range"}
|
|
|
|
),
|
2024-09-24 15:05:30 +02:00
|
|
|
status_code=400,
|
2024-04-20 00:11:41 +02:00
|
|
|
)
|
|
|
|
|
2024-09-30 23:55:44 +02:00
|
|
|
export_id = f"{camera_name}_{''.join(random.choices(string.ascii_lowercase + string.digits, k=6))}"
|
2024-04-20 00:11:41 +02:00
|
|
|
exporter = RecordingExporter(
|
2024-09-24 15:05:30 +02:00
|
|
|
request.app.frigate_config,
|
2024-09-30 23:55:44 +02:00
|
|
|
export_id,
|
2024-04-20 00:11:41 +02:00
|
|
|
camera_name,
|
2024-07-17 15:39:37 +02:00
|
|
|
friendly_name,
|
2024-08-12 16:26:54 +02:00
|
|
|
existing_image,
|
2024-04-20 00:11:41 +02:00
|
|
|
int(start_time),
|
|
|
|
int(end_time),
|
|
|
|
(
|
|
|
|
PlaybackFactorEnum[playback_factor]
|
|
|
|
if playback_factor in PlaybackFactorEnum.__members__.values()
|
|
|
|
else PlaybackFactorEnum.realtime
|
|
|
|
),
|
|
|
|
)
|
|
|
|
exporter.start()
|
2024-09-24 15:05:30 +02:00
|
|
|
return JSONResponse(
|
|
|
|
content=(
|
2024-04-20 00:11:41 +02:00
|
|
|
{
|
|
|
|
"success": True,
|
|
|
|
"message": "Starting export of recording.",
|
2024-09-30 23:55:44 +02:00
|
|
|
"export_id": export_id,
|
2024-04-20 00:11:41 +02:00
|
|
|
}
|
|
|
|
),
|
2024-09-24 15:05:30 +02:00
|
|
|
status_code=200,
|
2024-04-20 00:11:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-09-24 15:05:30 +02:00
|
|
|
@router.patch("/export/{event_id}/{new_name}")
|
|
|
|
def export_rename(event_id: str, new_name: str):
|
2024-04-20 00:11:41 +02:00
|
|
|
try:
|
2024-09-24 15:05:30 +02:00
|
|
|
export: Export = Export.get(Export.id == event_id)
|
2024-04-20 00:11:41 +02:00
|
|
|
except DoesNotExist:
|
2024-09-24 15:05:30 +02:00
|
|
|
return JSONResponse(
|
|
|
|
content=(
|
2024-04-20 00:11:41 +02:00
|
|
|
{
|
|
|
|
"success": False,
|
|
|
|
"message": "Export not found.",
|
|
|
|
}
|
|
|
|
),
|
2024-09-24 15:05:30 +02:00
|
|
|
status_code=404,
|
2024-04-20 00:11:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
export.name = new_name
|
|
|
|
export.save()
|
2024-09-24 15:05:30 +02:00
|
|
|
return JSONResponse(
|
|
|
|
content=(
|
2024-04-20 00:11:41 +02:00
|
|
|
{
|
|
|
|
"success": True,
|
|
|
|
"message": "Successfully renamed export.",
|
|
|
|
}
|
|
|
|
),
|
2024-09-24 15:05:30 +02:00
|
|
|
status_code=200,
|
2024-04-20 00:11:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-09-24 15:05:30 +02:00
|
|
|
@router.delete("/export/{event_id}")
|
|
|
|
def export_delete(event_id: str):
|
2024-04-20 00:11:41 +02:00
|
|
|
try:
|
2024-09-24 15:05:30 +02:00
|
|
|
export: Export = Export.get(Export.id == event_id)
|
2024-04-20 00:11:41 +02:00
|
|
|
except DoesNotExist:
|
2024-09-24 15:05:30 +02:00
|
|
|
return JSONResponse(
|
|
|
|
content=(
|
2024-04-20 00:11:41 +02:00
|
|
|
{
|
|
|
|
"success": False,
|
|
|
|
"message": "Export not found.",
|
|
|
|
}
|
|
|
|
),
|
2024-09-24 15:05:30 +02:00
|
|
|
status_code=404,
|
2024-04-20 00:11:41 +02:00
|
|
|
)
|
|
|
|
|
2024-06-04 17:10:19 +02:00
|
|
|
files_in_use = []
|
|
|
|
for process in psutil.process_iter():
|
|
|
|
try:
|
|
|
|
if process.name() != "ffmpeg":
|
|
|
|
continue
|
2024-09-17 17:41:46 +02:00
|
|
|
file_list = process.open_files()
|
|
|
|
if file_list:
|
|
|
|
for nt in file_list:
|
2024-06-04 17:10:19 +02:00
|
|
|
if nt.path.startswith(EXPORT_DIR):
|
|
|
|
files_in_use.append(nt.path.split("/")[-1])
|
|
|
|
except psutil.Error:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if export.video_path.split("/")[-1] in files_in_use:
|
2024-09-24 15:05:30 +02:00
|
|
|
return JSONResponse(
|
|
|
|
content=(
|
2024-06-04 17:10:19 +02:00
|
|
|
{"success": False, "message": "Can not delete in progress export."}
|
|
|
|
),
|
2024-09-24 15:05:30 +02:00
|
|
|
status_code=400,
|
2024-06-04 17:10:19 +02:00
|
|
|
)
|
|
|
|
|
2024-04-20 00:11:41 +02:00
|
|
|
Path(export.video_path).unlink(missing_ok=True)
|
|
|
|
|
|
|
|
if export.thumb_path:
|
|
|
|
Path(export.thumb_path).unlink(missing_ok=True)
|
|
|
|
|
|
|
|
export.delete_instance()
|
2024-09-24 15:05:30 +02:00
|
|
|
return JSONResponse(
|
|
|
|
content=(
|
2024-04-20 00:11:41 +02:00
|
|
|
{
|
|
|
|
"success": True,
|
|
|
|
"message": "Successfully deleted export.",
|
|
|
|
}
|
|
|
|
),
|
2024-09-24 15:05:30 +02:00
|
|
|
status_code=200,
|
2024-04-20 00:11:41 +02:00
|
|
|
)
|