From 0f178a502b0fc7e04f3d9dcca643e0a4d5fc0a31 Mon Sep 17 00:00:00 2001 From: Alex Kim Date: Mon, 28 Oct 2024 21:40:09 +1100 Subject: [PATCH] Fixed intro skipper for VLC --- components/video-player/Controls.tsx | 7 +++++-- hooks/useCreditSkipper.ts | 18 ++++++++++++++++-- hooks/useIntroSkipper.ts | 22 ++++++++++++++++++++-- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/components/video-player/Controls.tsx b/components/video-player/Controls.tsx index 5710559b..babc7f78 100644 --- a/components/video-player/Controls.tsx +++ b/components/video-player/Controls.tsx @@ -12,6 +12,7 @@ import { getDefaultPlaySettings } from "@/utils/jellyfin/getDefaultPlaySettings" import { writeToLog } from "@/utils/log"; import { formatTimeString, + msToSeconds, msToTicks, secondsToMs, ticksToMs, @@ -130,14 +131,16 @@ export const Controls: React.FC = ({ offline ? undefined : item.Id, currentTime, seek, - play + play, + isVlc ); const { showSkipCreditButton, skipCredit } = useCreditSkipper( offline ? undefined : item.Id, currentTime, seek, - play + play, + isVlc ); const goToPreviousItem = useCallback(() => { diff --git a/hooks/useCreditSkipper.ts b/hooks/useCreditSkipper.ts index 4b9345de..0ad52a56 100644 --- a/hooks/useCreditSkipper.ts +++ b/hooks/useCreditSkipper.ts @@ -4,6 +4,7 @@ import { useAtom } from "jotai"; import { apiAtom } from "@/providers/JellyfinProvider"; import { getAuthHeaders } from "@/utils/jellyfin/jellyfin"; import { writeToLog } from "@/utils/log"; +import { msToSeconds, secondsToMs } from "@/utils/time"; interface CreditTimestamps { Introduction: { @@ -22,11 +23,24 @@ export const useCreditSkipper = ( itemId: string | undefined, currentTime: number, seek: (time: number) => void, - play: () => void + play: () => void, + isVlc: boolean = false ) => { const [api] = useAtom(apiAtom); const [showSkipCreditButton, setShowSkipCreditButton] = useState(false); + if (isVlc) { + currentTime = msToSeconds(currentTime); + } + + const wrappedSeek = (seconds: number) => { + if (isVlc) { + seek(secondsToMs(seconds)); + return; + } + seek(seconds); + }; + const { data: creditTimestamps } = useQuery({ queryKey: ["creditTimestamps", itemId], queryFn: async () => { @@ -63,7 +77,7 @@ export const useCreditSkipper = ( const skipCredit = useCallback(() => { if (!creditTimestamps) return; try { - seek(creditTimestamps.Credits.End); + wrappedSeek(creditTimestamps.Credits.End); setTimeout(() => { play(); }, 200); diff --git a/hooks/useIntroSkipper.ts b/hooks/useIntroSkipper.ts index c092c3e2..0cf15818 100644 --- a/hooks/useIntroSkipper.ts +++ b/hooks/useIntroSkipper.ts @@ -4,6 +4,7 @@ import { useAtom } from "jotai"; import { apiAtom } from "@/providers/JellyfinProvider"; import { getAuthHeaders } from "@/utils/jellyfin/jellyfin"; import { writeToLog } from "@/utils/log"; +import { msToSeconds, secondsToMs } from "@/utils/time"; interface IntroTimestamps { EpisodeId: string; @@ -14,14 +15,31 @@ interface IntroTimestamps { Valid: boolean; } +/** + * Custom hook to handle skipping intros in a media player. + * + * @param {number} currentTime - The current playback time in seconds. + */ export const useIntroSkipper = ( itemId: string | undefined, currentTime: number, seek: (ticks: number) => void, - play: () => void + play: () => void, + isVlc: boolean = false ) => { const [api] = useAtom(apiAtom); const [showSkipButton, setShowSkipButton] = useState(false); + if (isVlc) { + currentTime = msToSeconds(currentTime); + } + + const wrappedSeek = (seconds: number) => { + if (isVlc) { + seek(secondsToMs(seconds)); + return; + } + seek(seconds); + }; const { data: introTimestamps } = useQuery({ queryKey: ["introTimestamps", itemId], @@ -60,7 +78,7 @@ export const useIntroSkipper = ( console.log("skipIntro"); if (!introTimestamps) return; try { - seek(introTimestamps.IntroEnd); + wrappedSeek(introTimestamps.IntroEnd); setTimeout(() => { play(); }, 200);