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

Add API and WebUI to export recordings #6550

Merged
merged 20 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add http endpoint
  • Loading branch information
NickM-27 committed Jun 7, 2023
commit 3649dd3d59b658c36480aab55cfb22221dadcdd6
3 changes: 2 additions & 1 deletion frigate/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
CLIPS_DIR,
CONFIG_DIR,
DEFAULT_DB_PATH,
EXPORT_DIR,
MODEL_CACHE_DIR,
RECORD_DIR,
)
Expand Down Expand Up @@ -68,7 +69,7 @@ def set_environment_vars(self) -> None:
os.environ[key] = value

def ensure_dirs(self) -> None:
for d in [CONFIG_DIR, RECORD_DIR, CLIPS_DIR, CACHE_DIR, MODEL_CACHE_DIR]:
for d in [CONFIG_DIR, RECORD_DIR, CLIPS_DIR, CACHE_DIR, MODEL_CACHE_DIR, EXPORT_DIR]:
if not os.path.exists(d) and not os.path.islink(d):
logger.info(f"Creating directory: {d}")
os.makedirs(d)
Expand Down
16 changes: 16 additions & 0 deletions frigate/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from frigate.object_processing import TrackedObject
from frigate.plus import PlusApi
from frigate.ptz import OnvifController
from frigate.record.export import PlaybackFactorEnum, RecordingExporter
from frigate.stats import stats_snapshot
from frigate.storage import StorageMaintainer
from frigate.util import (
Expand Down Expand Up @@ -1504,6 +1505,21 @@ def vod_event(id):
)


@bp.route("/export/<camera_name>/start/<start_time>/end/<end_time>")
def export_recording(camera_name: str, start_time: int, end_time: int):
playback_factor = request.args.get("playback", type=str, default="realtime")
exporter = RecordingExporter(
camera_name,
int(start_time),
int(end_time),
PlaybackFactorEnum[playback_factor]
if playback_factor in PlaybackFactorEnum.__members__.values()
else PlaybackFactorEnum.real_time,
)
exporter.start()
return "Starting export of recording", 200


def imagestream(detected_frames_processor, camera_name, fps, height, draw_options):
while True:
# max out at specified FPS
Expand Down
23 changes: 15 additions & 8 deletions frigate/record/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,50 @@ def __init__(
end_time: int,
playback_factor: PlaybackFactorEnum,
) -> None:
threading.Thread.__init__(self)
self.camera = camera
self.start_time = start_time
self.end_time = end_time
self.playback_factor = playback_factor

def get_datetime_from_timestamp(self, timestamp: int) -> str:
"""Convenience fun to get a simple date time from timestamp."""
return datetime.datetime.fromtimestamp(timestamp).strftime("%Y/%m/%d %I:%M")
return datetime.datetime.fromtimestamp(timestamp).strftime("%Y_%m_%d_%I:%M")

def run(self) -> None:
logger.debug(
f"Beginning export for {self.camera} from {self.start_time} to {self.end_time}"
)
file_name = f"{EXPORT_DIR}/in_progress.{self.camera}_{self.get_datetime_from_timestamp(self.start_time)}-{self.get_datetime_from_timestamp(self.end_time)}.mp4"
final_file_name = f"{EXPORT_DIR}/{self.camera}_{self.get_datetime_from_timestamp(self.start_time)}-{self.get_datetime_from_timestamp(self.end_time)}.mp4"
file_name = f"{EXPORT_DIR}/in_progress.{self.camera}@{self.get_datetime_from_timestamp(self.start_time)}__{self.get_datetime_from_timestamp(self.end_time)}.mp4"
final_file_name = f"{EXPORT_DIR}/{self.camera}_{self.get_datetime_from_timestamp(self.start_time)}__{self.get_datetime_from_timestamp(self.end_time)}.mp4"

if (self.end_time - self.start_time) <= MAX_PLAYLIST_SECONDS:
playlist_lines = f"http:https://127.0.0.1:5000/vod/start/{self.start_time}/end/{self.end_time}/index.m3u8"
playlist_lines = f"http:https://127.0.0.1:5000/vod/{self.camera}/start/{self.start_time}/end/{self.end_time}/index.m3u8"
ffmpeg_cmd = [
"ffmpeg",
"-hide_banner",
"-y",
"-protocol_whitelist",
"pipe,file,http,tcp",
"-i",
"/dev/stdin",
playlist_lines,
]
else:
playlist_lines = []
playlist_start = self.start_time

while playlist_start < self.end_time:
playlist_lines.append(
f"file http:https://127.0.0.1:5000/vod/start/{playlist_start}/end/{min(playlist_start + MAX_PLAYLIST_SECONDS, self.end_time)}/index.m3u8"
f"file http:https://127.0.0.1:5000/vod/{self.camera}/start/{playlist_start}/end/{min(playlist_start + MAX_PLAYLIST_SECONDS, self.end_time)}/index.m3u8"
)
playlist_start += MAX_PLAYLIST_SECONDS

ffmpeg_cmd = [
"ffmpeg",
"-hide_banner",
"-y",
"-protocol_whitelist",
"pipe,file",
"pipe,file,http,tcp",
"-f",
"concat",
"-safe",
Expand All @@ -92,5 +98,6 @@ def run(self) -> None:
logger.error(p.stderr)
return

logger.error(f"Updating finalized export {file_name}")
NickM-27 marked this conversation as resolved.
Show resolved Hide resolved
os.rename(file_name, final_file_name)
logger.debug("Finished exporting")
logger.error(f"Finished exporting {file_name}")