Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

seems to buffer less now

roryclearfbcc8d8c4d94
diff --git a/player.html b/player.html
index 0fa0df6..a734ceb 100644
--- a/player.html
+++ b/player.html
@@ -74,197 +74,209 @@
<script>
let segments = [];
- let totalDuration = 0;
- let retryTimeout = 2000;
-
- const videoPlayer1 = document.getElementById('videoPlayer1');
- const videoPlayer2 = document.getElementById('videoPlayer2');
- const playPauseBtn = document.getElementById('playPauseBtn');
- const progressBar = document.getElementById('progress-bar');
- const progress = document.getElementById('progress');
- const timeDisplay = document.getElementById('timeDisplay');
-
- let currentVideo = videoPlayer1;
- let nextVideo = videoPlayer2;
- let currentSegment = 0;
- let isPlaying = false;
- let isScrubbing = false;
-
- async function fetchSegments() {
- try {
- // Dynamically append the ?start={segments.length} parameter
- const startParam = segments.length; // Start from the current number of segments
- const response = await fetch(`/get-segments?start=${startParam}`);
- if (response.ok) {
- const newSegments = await response.json();
- if (Array.isArray(newSegments)) {
- const oldTotalDuration = totalDuration;
-
- // Calculate cumulativeStart with lost frames adjustment
- let cumulativeStart = totalDuration;
- newSegments.forEach((segment, index) => {
- const timeDifference = segment.timeDifference || segment.duration;
- const lostFrames = timeDifference - segment.duration;
-
- // Store cumulativeStart including lost frames
- const currentCumulativeStart = cumulativeStart;
- cumulativeStart += timeDifference; // Add full time difference to cumulative start
-
- segments.push({
- ...segment,
- cumulativeStart: currentCumulativeStart,
- lostFrames: lostFrames > 0 ? lostFrames : 0,
- });
- });
-
- // Calculate total duration including lost frames
- totalDuration = segments.reduce((sum, seg) => sum + (seg.timeDifference || seg.duration), 0);
-
- // If total duration changed, update the timeline and preload next segment
- if (totalDuration !== oldTotalDuration) {
- updateTimeline();
- preloadNextSegment();
- if (!isPlaying && segments.length > 0) {
- playSegment();
- }
- }
+let totalDuration = 0;
+let retryTimeout = 2000;
+
+const videoPlayer1 = document.getElementById('videoPlayer1');
+const videoPlayer2 = document.getElementById('videoPlayer2');
+const playPauseBtn = document.getElementById('playPauseBtn');
+const progressBar = document.getElementById('progress-bar');
+const progress = document.getElementById('progress');
+const timeDisplay = document.getElementById('timeDisplay');
+
+let currentVideo = videoPlayer1;
+let nextVideo = videoPlayer2;
+let currentSegment = 0;
+let isPlaying = false;
+let isScrubbing = false;
+
+async function fetchSegments() {
+ try {
+ const startParam = segments.length;
+ const response = await fetch(`/get-segments?start=${startParam}`);
+ if (response.ok) {
+ const newSegments = await response.json();
+ if (Array.isArray(newSegments)) {
+ const oldTotalDuration = totalDuration;
+
+ let cumulativeStart = totalDuration;
+ newSegments.forEach((segment) => {
+ const timeDifference = segment.timeDifference || segment.duration;
+ const lostFrames = timeDifference - segment.duration;
+
+ const currentCumulativeStart = cumulativeStart;
+ cumulativeStart += timeDifference;
+
+ segments.push({
+ ...segment,
+ cumulativeStart: currentCumulativeStart,
+ lostFrames: lostFrames > 0 ? lostFrames : 0,
+ });
+ });
+
+ totalDuration = segments.reduce((sum, seg) => sum + (seg.timeDifference || seg.duration), 0);
+
+ if (totalDuration !== oldTotalDuration) {
+ updateTimeline();
+ preloadNextSegments();
+ if (!isPlaying && segments.length > 0) {
+ playSegment();
}
}
- } catch (error) {
- console.error('Failed to fetch segments:', error);
- }
- }
-
- function formatTime(seconds) {
- const minutes = Math.floor(seconds / 60);
- const secs = Math.floor(seconds % 60).toString().padStart(2, '0');
- return `${minutes}:${secs}`;
- }
-
- function updateTimeline() {
- // Calculate the current time including lost frames
- const currentTime = currentVideo.currentTime + segments[currentSegment].cumulativeStart;
- progressBar.style.width = `${(currentTime / totalDuration) * 100}%`;
- timeDisplay.innerText = `${formatTime(currentTime)} / ${formatTime(totalDuration)}`;
- }
-
- function scrub(e) {
- const scrubTime = ((e.offsetX || e.touches[0].clientX - progress.getBoundingClientRect().left) / progress.offsetWidth) * totalDuration;
-
- let targetSegment = segments.findIndex(segment => scrubTime < segment.cumulativeStart + segment.duration);
- targetSegment = targetSegment === -1 ? segments.length - 1 : targetSegment;
-
- const targetTime = scrubTime - segments[targetSegment].cumulativeStart;
-
- if (targetSegment >= 0 && targetSegment < segments.length) {
- currentSegment = targetSegment;
- currentVideo.src = segments[currentSegment].url;
- currentVideo.currentTime = targetTime;
- currentVideo.play();
- preloadNextSegment();
- }
-
- updateTimeline();
- }
-
- function startScrubbing(e) {
- isScrubbing = true;
- scrub(e);
- document.addEventListener("mousemove", scrubWhileDragging);
- document.addEventListener("mouseup", stopScrubbing);
- document.addEventListener("touchmove", scrubWhileDragging);
- document.addEventListener("touchend", stopScrubbing);
- }
-
- function scrubWhileDragging(e) {
- if (isScrubbing) {
- scrub(e);
- }
- }
-
- function stopScrubbing() {
- isScrubbing = false;
- document.removeEventListener("mousemove", scrubWhileDragging);
- document.removeEventListener("mouseup", stopScrubbing);
- document.removeEventListener("touchmove", scrubWhileDragging);
- document.removeEventListener("touchend", stopScrubbing);
- }
-
- function preloadNextSegment() {
- if (currentSegment + 1 < segments.length) {
- nextVideo.src = segments[currentSegment + 1].url;
- nextVideo.load();
- }
- }
-
- function playSegment() {
- if (segments.length > 0) {
- currentVideo.src = segments[currentSegment].url;
- currentVideo.currentTime = 0;
- currentVideo.style.display = "block";
- nextVideo.style.display = "none";
-
- currentVideo.play();
- isPlaying = true;
- playPauseBtn.innerText = "Pause";
-
- preloadNextSegment();
}
}
-
- function retrySegment(videoElement) {
- console.warn(`Retrying segment: ${segments[currentSegment].url}`);
- setTimeout(() => {
- videoElement.src = segments[currentSegment].url;
- videoElement.load();
- videoElement.play();
- }, retryTimeout);
- }
-
- function switchToNextSegment() {
- if (currentSegment + 1 < segments.length) {
- currentSegment++;
- const temp = currentVideo;
- currentVideo = nextVideo;
- nextVideo = temp;
-
- nextVideo.style.display = "none";
- currentVideo.style.display = "block";
-
- currentVideo.play();
- preloadNextSegment();
- } else {
- isPlaying = false;
- playPauseBtn.innerText = "Play";
- }
- }
-
- progress.addEventListener("mousedown", startScrubbing);
- progress.addEventListener("touchstart", startScrubbing);
-
- setInterval(fetchSegments, 1000);
-
- videoPlayer1.addEventListener("timeupdate", updateTimeline);
- videoPlayer2.addEventListener("timeupdate", updateTimeline);
-
- videoPlayer1.addEventListener("ended", switchToNextSegment);
- videoPlayer2.addEventListener("ended", switchToNextSegment);
-
- videoPlayer1.addEventListener("error", () => retrySegment(videoPlayer1));
- videoPlayer2.addEventListener("error", () => retrySegment(videoPlayer2));
-
- playPauseBtn.addEventListener("click", () => {
- if (isPlaying) {
- currentVideo.pause();
- playPauseBtn.innerText = "Play";
- } else {
- currentVideo.play();
- playPauseBtn.innerText = "Pause";
- }
- isPlaying = !isPlaying;
+ } catch (error) {
+ console.error('Failed to fetch segments:', error);
+ }
+}
+
+function formatTime(seconds) {
+ const minutes = Math.floor(seconds / 60);
+ const secs = Math.floor(seconds % 60).toString().padStart(2, '0');
+ return `${minutes}:${secs}`;
+}
+
+function updateTimeline() {
+ const currentTime = currentVideo.currentTime + segments[currentSegment].cumulativeStart;
+ progressBar.style.width = `${(currentTime / totalDuration) * 100}%`;
+ timeDisplay.innerText = `${formatTime(currentTime)} / ${formatTime(totalDuration)}`;
+}
+
+function scrub(e) {
+ const scrubTime = ((e.offsetX || e.touches[0].clientX - progress.getBoundingClientRect().left) / progress.offsetWidth) * totalDuration;
+
+ let targetSegment = segments.findIndex(segment => scrubTime < segment.cumulativeStart + segment.duration);
+ targetSegment = targetSegment === -1 ? segments.length - 1 : targetSegment;
+
+ const targetTime = scrubTime - segments[targetSegment].cumulativeStart;
+
+ if (targetSegment >= 0 && targetSegment < segments.length) {
+ currentSegment = targetSegment;
+ currentVideo.src = segments[currentSegment].url;
+ currentVideo.currentTime = targetTime;
+ currentVideo.play();
+ preloadNextSegments();
+ }
+
+ updateTimeline();
+}
+
+function startScrubbing(e) {
+ isScrubbing = true;
+ scrub(e);
+ document.addEventListener("mousemove", scrubWhileDragging);
+ document.addEventListener("mouseup", stopScrubbing);
+ document.addEventListener("touchmove", scrubWhileDragging);
+ document.addEventListener("touchend", stopScrubbing);
+}
+
+function scrubWhileDragging(e) {
+ if (isScrubbing) {
+ scrub(e);
+ }
+}
+
+function stopScrubbing() {
+ isScrubbing = false;
+ document.removeEventListener("mousemove", scrubWhileDragging);
+ document.removeEventListener("mouseup", stopScrubbing);
+ document.removeEventListener("touchmove", scrubWhileDragging);
+ document.removeEventListener("touchend", stopScrubbing);
+}
+
+function preloadNextSegments() {
+ if (currentSegment + 1 < segments.length) {
+ nextVideo.src = segments[currentSegment + 1].url;
+ nextVideo.load(); // Force load for mobile
+ nextVideo.preload = 'auto';
+ nextVideo.play().then(() => {
+ nextVideo.pause();
+ nextVideo.currentTime = 0; // Ensure it's ready to start instantly
+ }).catch(err => console.warn("Preload error:", err));
+ }
+}
+
+function playSegment() {
+ if (segments.length > 0) {
+ currentVideo.src = segments[currentSegment].url;
+ currentVideo.currentTime = 0;
+ currentVideo.style.display = "block";
+ nextVideo.style.display = "none";
+
+ currentVideo.play();
+ isPlaying = true;
+ playPauseBtn.innerText = "Pause";
+
+ preloadNextSegments();
+ }
+}
+
+function retrySegment(videoElement) {
+ console.warn(`Retrying segment: ${segments[currentSegment].url}`);
+ setTimeout(() => {
+ videoElement.src = segments[currentSegment].url;
+ videoElement.load();
+ videoElement.play();
+ }, retryTimeout);
+}
+
+function switchToNextSegment() {
+ if (currentSegment + 1 < segments.length) {
+ currentSegment++;
+ let temp = currentVideo;
+ currentVideo = nextVideo;
+ nextVideo = temp;
+
+ nextVideo.style.display = "none";
+ currentVideo.style.display = "block";
+
+ currentVideo.play().catch(() => {
+ retrySegment(currentVideo);
});
-
- fetchSegments();
+
+ setTimeout(() => preloadNextSegments(), 500); // Slightly earlier preloading for mobile
+ } else {
+ isPlaying = false;
+ playPauseBtn.innerText = "Play";
+ }
+}
+
+function handleTimeUpdate() {
+ updateTimeline();
+ if (!preloading && currentVideo.currentTime > currentVideo.duration - 5) {
+ preloading = true;
+ preloadNextSegments();
+ }
+}
+
+currentVideo.addEventListener("timeupdate", handleTimeUpdate);
+nextVideo.addEventListener("timeupdate", handleTimeUpdate);
+
+progress.addEventListener("mousedown", startScrubbing);
+progress.addEventListener("touchstart", startScrubbing);
+
+setInterval(fetchSegments, 1000);
+
+currentVideo.addEventListener("ended", switchToNextSegment);
+nextVideo.addEventListener("ended", switchToNextSegment);
+
+currentVideo.addEventListener("error", () => retrySegment(currentVideo));
+nextVideo.addEventListener("error", () => retrySegment(nextVideo));
+
+playPauseBtn.addEventListener("click", () => {
+ if (isPlaying) {
+ currentVideo.pause();
+ playPauseBtn.innerText = "Play";
+ isPlaying = false;
+ } else {
+ currentVideo.play();
+ playPauseBtn.innerText = "Pause";
+ isPlaying = true;
+ }
+});
+
+fetchSegments();
+
</script>
</body>