diff --git a/lib/fetchSubtitle.ts b/lib/fetchSubtitle.ts index 8bf21690..75d0d8f1 100644 --- a/lib/fetchSubtitle.ts +++ b/lib/fetchSubtitle.ts @@ -1,5 +1,5 @@ -import { VideoService } from "./types"; import { fetchBilibiliSubtitle } from "./bilibili/fetchBilibiliSubtitle"; +import { CommonSubtitleItem, VideoService } from "./types"; import { fetchYoutubeSubtitle } from "./youtube/fetchYoutubeSubtitle"; export async function fetchSubtitle( @@ -8,7 +8,7 @@ export async function fetchSubtitle( shouldShowTimestamp?: boolean ): Promise<{ title: string; - subtitlesArray?: null | Array<{ index: number; text: string }>; + subtitlesArray?: null | Array; descriptionText?: string; }> { if (service === VideoService.Youtube) { diff --git a/lib/types.ts b/lib/types.ts index 97b3a629..0aaa9197 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -21,3 +21,5 @@ export enum VideoService { LocalVideo = "local-video", LocalAudio = "local-audio", } + +export type CommonSubtitleItem = { text: string; index: number }; diff --git a/lib/youtube/fetchYoutubeSubtitle.ts b/lib/youtube/fetchYoutubeSubtitle.ts index b831255e..ce2fd01e 100644 --- a/lib/youtube/fetchYoutubeSubtitle.ts +++ b/lib/youtube/fetchYoutubeSubtitle.ts @@ -1,5 +1,6 @@ import { fetchYoutubeSubtitleUrls, SUBTITLE_DOWNLOADER_URL } from "~/lib/youtube/fetchYoutubeSubtitleUrls"; import { find } from "~/utils/fp"; +import { reduceSubtitleTimestamp } from "~/utils/reduceSubtitleTimestamp"; export async function fetchYoutubeSubtitle( videoId: string, @@ -10,22 +11,16 @@ export async function fetchYoutubeSubtitle( return { title, subtitlesArray: null }; } const betterSubtitle = + find(subtitleList, { quality: "zh-CN" }) || find(subtitleList, { quality: "English" }) || find(subtitleList, { quality: "English (auto" }) || - find(subtitleList, { quality: "zh-CN" }) || subtitleList[0]; if (shouldShowTimestamp) { const subtitleUrl = `${SUBTITLE_DOWNLOADER_URL}${betterSubtitle.url}?ext=json`; const response = await fetch(subtitleUrl); const subtitles = await response.json(); // console.log("========subtitles========", subtitles); - // TODO: merge timestamp by trunk - const transcripts = subtitles?.map( - (item: { start: number; lines: string[] }, index: number) => ({ - text: `${item.start}: ${item.lines.join(" ")}`, - index - }) - ); + const transcripts = reduceSubtitleTimestamp(subtitles); return { title, subtitlesArray: transcripts }; } diff --git a/middleware.ts b/middleware.ts index 566194cd..e3480995 100644 --- a/middleware.ts +++ b/middleware.ts @@ -40,7 +40,7 @@ export async function middleware(req: NextRequest, context: NextFetchEvent) { } if (!userKey) { - const identifier = req.ip ?? "127.0.0.8"; + const identifier = req.ip ?? "127.0.0.10"; const { success, remaining } = await ratelimitForIps.limit(identifier); console.log( `======== ip ${identifier}, remaining: ${remaining} ========` diff --git a/utils/reduceSubtitleTimestamp.ts b/utils/reduceSubtitleTimestamp.ts new file mode 100644 index 00000000..c95f372a --- /dev/null +++ b/utils/reduceSubtitleTimestamp.ts @@ -0,0 +1,42 @@ +import { CommonSubtitleItem } from "~/lib/types"; + +export type YoutubeSubtitleItem = { start: number; lines: string[] }; + +export function reduceSubtitleTimestamp( + subtitles: Array +): Array { + // 把字幕数组分成 10 组 + const groupCount = 20; + const eachGroupCount = + subtitles.length > groupCount + ? subtitles.length / groupCount + : subtitles.length; + + return subtitles.reduce( + ( + accumulator: CommonSubtitleItem[], + current: YoutubeSubtitleItem, + index: number + ) => { + // 计算当前元素在哪一组 + const groupIndex: number = Math.floor(index / eachGroupCount); + + // 如果是当前组的第一个元素,初始化这一组的字符串 + if (!accumulator[groupIndex]) { + accumulator[groupIndex] = { + // 5.88 -> 5.9 + // text: current.start.toFixed() + ": ", + text: current.start + ": ", + index: groupIndex, + }; + } + + // 将当前元素添加到当前组的字符串末尾 + accumulator[groupIndex].text = + accumulator[groupIndex].text + current.lines.join(" ") + " "; + + return accumulator; + }, + [] + ); +}