mirror of
https://github.com/blakeblackshear/frigate.git
synced 2025-09-26 19:41:29 +08:00

Explore videos are very small on Chrome specifically, this has something to do with how the latest version of Chrome loads video metadata. This change provides a default aspect ratio instead of a default height when the container ref is not defined yet
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import { useState, useMemo } from "react";
|
|
import { useResizeObserver } from "./resize-observer";
|
|
|
|
export type VideoResolutionType = {
|
|
width: number;
|
|
height: number;
|
|
};
|
|
|
|
export function useVideoDimensions(
|
|
containerRef: React.RefObject<HTMLDivElement>,
|
|
) {
|
|
const [{ width: containerWidth, height: containerHeight }] =
|
|
useResizeObserver(containerRef);
|
|
const [videoResolution, setVideoResolution] = useState<VideoResolutionType>({
|
|
width: 0,
|
|
height: 0,
|
|
});
|
|
|
|
const videoAspectRatio = useMemo(() => {
|
|
return videoResolution.width / videoResolution.height || 16 / 9;
|
|
}, [videoResolution]);
|
|
|
|
const containerAspectRatio = useMemo(() => {
|
|
return containerWidth / containerHeight || 16 / 9;
|
|
}, [containerWidth, containerHeight]);
|
|
|
|
const videoDimensions = useMemo(() => {
|
|
if (!containerWidth || !containerHeight)
|
|
return { aspectRatio: "16 / 9", width: "100%" };
|
|
if (containerAspectRatio > videoAspectRatio) {
|
|
const height = containerHeight;
|
|
const width = height * videoAspectRatio;
|
|
return { width: `${width}px`, height: `${height}px` };
|
|
} else {
|
|
const width = containerWidth;
|
|
const height = width / videoAspectRatio;
|
|
return { width: `${width}px`, height: `${height}px` };
|
|
}
|
|
}, [containerWidth, containerHeight, videoAspectRatio, containerAspectRatio]);
|
|
|
|
return {
|
|
videoDimensions,
|
|
setVideoResolution,
|
|
};
|
|
}
|