Skip to content

Commit

Permalink
fix: merge subtitles by 20 group
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyLv committed Mar 8, 2023
1 parent ccf23b6 commit 0d674d9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/fetchSubtitle.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -8,7 +8,7 @@ export async function fetchSubtitle(
shouldShowTimestamp?: boolean
): Promise<{
title: string;
subtitlesArray?: null | Array<{ index: number; text: string }>;
subtitlesArray?: null | Array<CommonSubtitleItem>;
descriptionText?: string;
}> {
if (service === VideoService.Youtube) {
Expand Down
2 changes: 2 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ export enum VideoService {
LocalVideo = "local-video",
LocalAudio = "local-audio",
}

export type CommonSubtitleItem = { text: string; index: number };
11 changes: 3 additions & 8 deletions lib/youtube/fetchYoutubeSubtitle.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 };
}

Expand Down
2 changes: 1 addition & 1 deletion middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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} ========`
Expand Down
42 changes: 42 additions & 0 deletions utils/reduceSubtitleTimestamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { CommonSubtitleItem } from "~/lib/types";

export type YoutubeSubtitleItem = { start: number; lines: string[] };

export function reduceSubtitleTimestamp(
subtitles: Array<YoutubeSubtitleItem>
): Array<CommonSubtitleItem> {
// 把字幕数组分成 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;
},
[]
);
}

1 comment on commit 0d674d9

@vercel
Copy link

@vercel vercel bot commented on 0d674d9 Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.