Move filterDuplicateCaptionCues to a different file

This commit is contained in:
ssssobek 2024-03-14 20:34:32 +01:00
parent 810a12a097
commit 9f025bd12b
2 changed files with 18 additions and 15 deletions

View File

@ -1,26 +1,15 @@
import { useCallback, useMemo } from "react"; import { useCallback, useMemo } from "react";
import subsrt from "subsrt-ts"; import subsrt from "subsrt-ts";
import { ContentCaption } from "subsrt-ts/dist/types/handler";
import { downloadCaption, downloadWebVTT } from "@/backend/helpers/subs"; import { downloadCaption, downloadWebVTT } from "@/backend/helpers/subs";
import { Caption } from "@/stores/player/slices/source"; import { Caption } from "@/stores/player/slices/source";
import { usePlayerStore } from "@/stores/player/store"; import { usePlayerStore } from "@/stores/player/store";
import { useSubtitleStore } from "@/stores/subtitles"; import { useSubtitleStore } from "@/stores/subtitles";
import { parseVttSubtitles } from "../utils/captions"; import {
filterDuplicateCaptionCues,
const filterDuplicateCaptionCues = (cues: ContentCaption[]) => parseVttSubtitles,
cues.reduce((acc: ContentCaption[], cap: ContentCaption) => { } from "../utils/captions";
const lastCap = acc[acc.length - 1];
const isSameAsLast =
lastCap?.start === cap.start &&
lastCap?.end === cap.end &&
lastCap?.content === cap.content;
if (lastCap === undefined || !isSameAsLast) {
acc.push(cap);
}
return acc;
}, []);
export function useCaptions() { export function useCaptions() {
const setLanguage = useSubtitleStore((s) => s.setLanguage); const setLanguage = useSubtitleStore((s) => s.setLanguage);

View File

@ -50,6 +50,20 @@ export function convertSubtitlesToSrt(text: string): string {
return srt; return srt;
} }
export function filterDuplicateCaptionCues(cues: ContentCaption[]) {
return cues.reduce((acc: ContentCaption[], cap: ContentCaption) => {
const lastCap = acc[acc.length - 1];
const isSameAsLast =
lastCap?.start === cap.start &&
lastCap?.end === cap.end &&
lastCap?.content === cap.content;
if (lastCap === undefined || !isSameAsLast) {
acc.push(cap);
}
return acc;
}, []);
}
export function parseVttSubtitles(vtt: string) { export function parseVttSubtitles(vtt: string) {
return parse(vtt).filter((cue) => cue.type === "caption") as CaptionCueType[]; return parse(vtt).filter((cue) => cue.type === "caption") as CaptionCueType[];
} }