Skip to content

Commit

Permalink
Fix subtitle download (fix #4319)
Browse files Browse the repository at this point in the history
  • Loading branch information
the1812 committed Jul 19, 2023
1 parent 2ff60c9 commit 136894b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
1 change: 1 addition & 0 deletions registry/lib/components/video/subtitle/download/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
启用下载字幕支持, 在视频页面中可从功能面板里下载字幕. (AI 生成的不可下载)
3 changes: 0 additions & 3 deletions registry/lib/components/video/subtitle/download/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import { getBlobByType, SubtitleDownloadType } from './utils'
export const component = defineComponentMetadata({
name: 'downloadSubtitle',
displayName: '下载字幕',
description: {
'zh-CN': '启用下载字幕支持, 在视频页面中可从功能面板里下载字幕.',
},
tags: [componentsTags.video],
entry: none,
urlInclude: videoAndBangumiUrls,
Expand Down
28 changes: 19 additions & 9 deletions registry/lib/components/video/subtitle/download/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { getJson } from '@/core/ajax'
import { bilibiliApi, getJson, getJsonWithCredentials } from '@/core/ajax'
import { Toast } from '@/core/toast'
import { getFriendlyTitle } from '@/core/utils/title'
import { SubtitleConverterConfig } from '../subtitle-converter'

export interface SubtitleInfo {
id: number
id_str: string
lan: string
lan_doc: string
is_lock: boolean
subtitle_url: string
type: number
ai_type: number
ai_status: number
}
export type SubtitleDownloadType = 'json' | 'ass'
export const getSubtitleConfig = async (): Promise<[SubtitleConverterConfig, string]> => {
const { SubtitleConverter, SubtitleSize, SubtitleLocation } = await import(
Expand Down Expand Up @@ -64,11 +75,10 @@ export const getSubtitleConfig = async (): Promise<[SubtitleConverterConfig, str
return [config, language]
}
export const getSubtitleList = async (aid: string, cid: string | number) => {
const { VideoInfo } = await import('@/components/video/video-info')
const info = new VideoInfo(aid)
info.cid = typeof cid === 'string' ? parseInt(cid) : cid
await info.fetchInfo()
return info.subtitles
const data = await bilibiliApi(
getJsonWithCredentials(`https://api.bilibili.com/x/player/wbi/v2?aid=${aid}&cid=${cid}`),
)
return lodash.get(data, 'subtitle.subtitles', []) as SubtitleInfo[]
}
export const getBlobByType = async (
type: SubtitleDownloadType,
Expand All @@ -88,8 +98,8 @@ export const getBlobByType = async (
return null
}
const [config, language] = await getSubtitleConfig()
const subtitle = subtitles.find(s => s.languageCode === language) || subtitles[0]
const json = await getJson(subtitle.url)
const subtitle = subtitles.find(s => s.lan === language) || subtitles[0]
const json = await getJson(subtitle.subtitle_url)
const rawData = json.body
switch (type) {
case 'ass': {
Expand All @@ -102,7 +112,7 @@ export const getBlobByType = async (
}
default:
case 'json': {
return new Blob([JSON.stringify(rawData)], {
return new Blob([JSON.stringify(rawData, undefined, 2)], {
type: 'text/json',
})
}
Expand Down
25 changes: 12 additions & 13 deletions src/components/video/video-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ export class VideoInfo {
title: string
pageNumber: number
}[]
subtitles: {
id: number
languageCode: string
language: string
url: string
}[]

constructor(id: string, bvid = false) {
if (bvid) {
Expand Down Expand Up @@ -70,15 +64,20 @@ export class VideoInfo {
title: it.part,
pageNumber: it.page,
}))
this.subtitles =
data.subtitle?.list?.map((it: any) => ({
id: it.id,
languageCode: it.lan,
language: it.lan_doc,
url: it.subtitle_url.replace('http:', 'https:'),
})) ?? []
return this
}

/** @deprecated */
// eslint-disable-next-line class-methods-use-this
get subtitles(): {
id: number
languageCode: string
language: string
url: string
}[] {
console.warn('VideoInfo.subtitles is deprecated')
return []
}
}
export class BangumiInfo {
ep: number
Expand Down

0 comments on commit 136894b

Please sign in to comment.