Skip to content

Commit

Permalink
fix: fetch youtube subtitle with timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyLv committed Mar 7, 2023
1 parent d968eaf commit 244b77c
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/bilibili.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,21 @@ export async function fetchSubtitle(
find(subtitleList, { quality: "English (auto" }) ||
find(subtitleList, { quality: "zh-CN" }) ||
subtitleList[0];
const format = shouldShowTimestamp ? "" : `?ext=txt`; // ?ext=json
const subtitleUrl = `${SUBTITLE_DOWNLOADER_URL}${betterSubtitle.url}${format}`;
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);
const transcripts = subtitles?.map(
(item: { start: number; lines: string[] }, index: number) => ({
text: `${item.start}: ${item.lines.join(" ")}`,
index,
})
);
return { title, subtitles: transcripts };
}

const subtitleUrl = `${SUBTITLE_DOWNLOADER_URL}${betterSubtitle.url}?ext=txt`;
const response = await fetch(subtitleUrl);
const subtitles = await response.text();
const transcripts = subtitles
Expand Down

1 comment on commit 244b77c

@vercel
Copy link

@vercel vercel bot commented on 244b77c Mar 7, 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.