Skip to content

Commit

Permalink
fix: fix reviewed issues in PR #4521
Browse files Browse the repository at this point in the history
Co-authored-by: lainio24 <[email protected]>
  • Loading branch information
WakelessSloth56 and lainio24 committed Nov 23, 2023
1 parent 5f75019 commit a407963
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 20 deletions.
5 changes: 2 additions & 3 deletions registry/lib/plugins/video/download/wasm-output/ffmpeg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Modified 2023 WakelessSloth56

/* eslint-disable @typescript-eslint/naming-convention */

export const VERSION = '0.12.4'

const messageId = (() => {
let messageID = 0
return () => messageID++
Expand Down Expand Up @@ -46,7 +44,8 @@ export class FFmpeg {
this.#rejects[id](data)
break
default:
throw new Error('Unknown FFmpeg message')
// https://github.com/the1812/Bilibili-Evolved/pull/4521#discussion_r1402041877
break
}
delete this.#resolves[id]
delete this.#rejects[id]
Expand Down
21 changes: 7 additions & 14 deletions registry/lib/plugins/video/download/wasm-output/handler.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
import { CdnTypes } from '@/core/cdn-types'
import { DownloadPackage } from '@/core/download'
import { meta } from '@/core/meta'
import { Toast } from '@/core/toast'
import { FFmpeg, VERSION } from './ffmpeg'
import { httpget, toBlobUrl, toastProgress } from './utils'
import { FFmpeg } from './ffmpeg'
import { httpGet, toBlobUrl, toastProgress } from './utils'

const ffmpeg = new FFmpeg()

function cdnUrl(p: string, file: string) {
return `https://${
meta.compilationInfo.allCdns[CdnTypes.jsDelivr].host
}/npm/@ffmpeg/${p}@${VERSION}/dist/umd/${file}`
}

async function load(toast: Toast) {
await ffmpeg.load({
workerLoadURL: await toBlobUrl(
cdnUrl('ffmpeg', '814.ffmpeg.js'),
meta.compilationInfo.altCdn.library.ffmpeg.worker,
'text/javascript',
toastProgress(toast, '正在加载 FFmpeg Worker'),
),
coreURL: await toBlobUrl(
cdnUrl('core', 'ffmpeg-core.js'),
meta.compilationInfo.altCdn.library.ffmpeg.core,
'text/javascript',
toastProgress(toast, '正在加载 FFmpeg Core'),
),
wasmURL: await toBlobUrl(
cdnUrl('core', 'ffmpeg-core.wasm'),
meta.compilationInfo.altCdn.library.ffmpeg.wasm,
'application/wasm',
toastProgress(toast, '正在加载 FFmpeg WASM'),
),
Expand All @@ -37,8 +30,8 @@ export async function run(name: string, videoUrl: string, audioUrl: string, toas
await load(toast)
}

ffmpeg.writeFile('video', await httpget(videoUrl, toastProgress(toast, '正在下载视频流')))
ffmpeg.writeFile('audio', await httpget(audioUrl, toastProgress(toast, '正在下载音频流')))
ffmpeg.writeFile('video', await httpGet(videoUrl, toastProgress(toast, '正在下载视频流')))
ffmpeg.writeFile('audio', await httpGet(audioUrl, toastProgress(toast, '正在下载音频流')))

toast.message = '混流中……'

Expand Down
10 changes: 7 additions & 3 deletions registry/lib/plugins/video/download/wasm-output/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ export function toastProgress(toast: Toast, message: string): OnProgress {
}
}

export async function httpget(url: string, onprogress: OnProgress) {
export async function httpGet(url: string, onprogress: OnProgress) {
const response = await fetch(url)
if (!response.ok) {
throw new Error(`${response.status} ${response.statusText}`)
}

const reader = response.body.getReader()
const length = parseInt(response.headers.get('Content-Length') || '0')

// https://github.com/the1812/Bilibili-Evolved/pull/4521#discussion_r1402127375
const length = response.headers.get('Content-Encoding')
? -1
: parseInt(response.headers.get('Content-Length'))

let received = 0
const chunks = []
Expand All @@ -44,7 +48,7 @@ export async function httpget(url: string, onprogress: OnProgress) {
}

export async function toBlobUrl(url: string, mimeType: string, onprogress: OnProgress) {
const buffer = await httpget(url, onprogress)
const buffer = await httpGet(url, onprogress)
const blob = new Blob([buffer], { type: mimeType })
return URL.createObjectURL(blob)
}
3 changes: 3 additions & 0 deletions webpack/cdn/github.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { jsDelivr } from './jsdelivr'
import { CdnConfig } from './types'

const owner = 'the1812'
Expand All @@ -15,6 +16,8 @@ export const github: CdnConfig = {
sortable: `https://${host}/SortableJS/Sortable/1.14.0/Sortable.min.js`,
mdi: `https://${owner}.github.io/Bilibili-Evolved/static/mdi/mdi.css`,
streamsaver: `https://${host}/jimmywarting/StreamSaver.js/2.0.6/StreamSaver.js`,
// https://github.com/the1812/Bilibili-Evolved/pull/4521#discussion_r1402084486
ffmpeg: jsDelivr.library.ffmpeg,
},
smallLogo: `https://${host}/${owner}/Bilibili-Evolved/preview/images/logo-small.png`,
logo: `https://${host}/${owner}/Bilibili-Evolved/preview/images/logo.png`,
Expand Down
5 changes: 5 additions & 0 deletions webpack/cdn/jsdelivr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export const jsDelivr: CdnConfig = {
sortable: `https://${host}/npm/[email protected]/Sortable.min.js`,
mdi: `https://${host}/gh/${owner}/Bilibili-Evolved@master/docs/static/mdi/mdi.css`,
streamsaver: `https://${host}/npm/[email protected]/StreamSaver.min.js`,
ffmpeg: {
worker: `https://${host}/npm/@ffmpeg/[email protected]/dist/umd/814.ffmpeg.js`,
core: `https://${host}/npm/@ffmpeg/[email protected]/dist/umd/ffmpeg-core.js`,
wasm: `https://${host}/npm/@ffmpeg/[email protected]/dist/umd/ffmpeg-core.wasm`,
},
},
smallLogo: `https://${host}/gh/${owner}/Bilibili-Evolved@preview/images/logo-small.png`,
logo: `https://${host}/gh/${owner}/Bilibili-Evolved@preview/images/logo.png`,
Expand Down
5 changes: 5 additions & 0 deletions webpack/cdn/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export interface CdnConfig {
sortable: string
mdi: string
streamsaver: string
ffmpeg: {
worker: string
core: string
wasm: string
}
}
smallLogo: string
logo: string
Expand Down

0 comments on commit a407963

Please sign in to comment.