use clipFrom to in vod module endpoint to start at the correct time

This commit is contained in:
Josh Hawkins 2025-05-14 15:43:45 -05:00
parent 468778aa8f
commit 1a461b1cc5

View File

@ -645,7 +645,12 @@ def recording_clip(
@router.get("/vod/{camera_name}/start/{start_ts}/end/{end_ts}")
def vod_ts(camera_name: str, start_ts: float, end_ts: float):
recordings = (
Recordings.select(Recordings.path, Recordings.duration, Recordings.end_time)
Recordings.select(
Recordings.path,
Recordings.duration,
Recordings.end_time,
Recordings.start_time,
)
.where(
Recordings.start_time.between(start_ts, end_ts)
| Recordings.end_time.between(start_ts, end_ts)
@ -665,17 +670,25 @@ def vod_ts(camera_name: str, start_ts: float, end_ts: float):
clip = {"type": "source", "path": recording.path}
duration = int(recording.duration * 1000)
# Determine if we need to end the last clip early
# adjust start offset if start_ts is after recording.start_time
clip_from = 0
if start_ts > recording.start_time:
clip_from = int((start_ts - recording.start_time) * 1000)
# adjust duration to account for the trimmed start
duration -= clip_from
# adjust end if recording.end_time is after end_ts
if recording.end_time > end_ts:
duration -= int((recording.end_time - end_ts) * 1000)
if duration == 0:
# this means the segment starts right at the end of the requested time range
# and it does not need to be included
continue
if duration <= 0:
# skip if the clip has no valid duration
continue
if 0 < duration < max_duration_ms:
clip["keyFrameDurations"] = [duration]
if clip_from > 0:
clip["clipFrom"] = clip_from
clips.append(clip)
durations.append(duration)
else: