fix: fix reviewed issues in PR #4521

Co-authored-by: lainio24 <lainio24@outlook.com>
This commit is contained in:
WakelessSloth56 2023-11-23 19:59:27 +08:00
parent 5f75019286
commit a407963f0d
No known key found for this signature in database
GPG Key ID: 3AFBE2AD7700CA19
6 changed files with 29 additions and 20 deletions

View File

@ -7,8 +7,6 @@ Modified 2023 WakelessSloth56
/* eslint-disable @typescript-eslint/naming-convention */ /* eslint-disable @typescript-eslint/naming-convention */
export const VERSION = '0.12.4'
const messageId = (() => { const messageId = (() => {
let messageID = 0 let messageID = 0
return () => messageID++ return () => messageID++
@ -46,7 +44,8 @@ export class FFmpeg {
this.#rejects[id](data) this.#rejects[id](data)
break break
default: default:
throw new Error('Unknown FFmpeg message') // https://github.com/the1812/Bilibili-Evolved/pull/4521#discussion_r1402041877
break
} }
delete this.#resolves[id] delete this.#resolves[id]
delete this.#rejects[id] delete this.#rejects[id]

View File

@ -1,32 +1,25 @@
import { CdnTypes } from '@/core/cdn-types'
import { DownloadPackage } from '@/core/download' import { DownloadPackage } from '@/core/download'
import { meta } from '@/core/meta' import { meta } from '@/core/meta'
import { Toast } from '@/core/toast' import { Toast } from '@/core/toast'
import { FFmpeg, VERSION } from './ffmpeg' import { FFmpeg } from './ffmpeg'
import { httpget, toBlobUrl, toastProgress } from './utils' import { httpGet, toBlobUrl, toastProgress } from './utils'
const ffmpeg = new FFmpeg() 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) { async function load(toast: Toast) {
await ffmpeg.load({ await ffmpeg.load({
workerLoadURL: await toBlobUrl( workerLoadURL: await toBlobUrl(
cdnUrl('ffmpeg', '814.ffmpeg.js'), meta.compilationInfo.altCdn.library.ffmpeg.worker,
'text/javascript', 'text/javascript',
toastProgress(toast, '正在加载 FFmpeg Worker'), toastProgress(toast, '正在加载 FFmpeg Worker'),
), ),
coreURL: await toBlobUrl( coreURL: await toBlobUrl(
cdnUrl('core', 'ffmpeg-core.js'), meta.compilationInfo.altCdn.library.ffmpeg.core,
'text/javascript', 'text/javascript',
toastProgress(toast, '正在加载 FFmpeg Core'), toastProgress(toast, '正在加载 FFmpeg Core'),
), ),
wasmURL: await toBlobUrl( wasmURL: await toBlobUrl(
cdnUrl('core', 'ffmpeg-core.wasm'), meta.compilationInfo.altCdn.library.ffmpeg.wasm,
'application/wasm', 'application/wasm',
toastProgress(toast, '正在加载 FFmpeg WASM'), toastProgress(toast, '正在加载 FFmpeg WASM'),
), ),
@ -37,8 +30,8 @@ export async function run(name: string, videoUrl: string, audioUrl: string, toas
await load(toast) await load(toast)
} }
ffmpeg.writeFile('video', await httpget(videoUrl, toastProgress(toast, '正在下载视频流'))) ffmpeg.writeFile('video', await httpGet(videoUrl, toastProgress(toast, '正在下载视频流')))
ffmpeg.writeFile('audio', await httpget(audioUrl, toastProgress(toast, '正在下载音频流'))) ffmpeg.writeFile('audio', await httpGet(audioUrl, toastProgress(toast, '正在下载音频流')))
toast.message = '混流中……' toast.message = '混流中……'

View File

@ -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) const response = await fetch(url)
if (!response.ok) { if (!response.ok) {
throw new Error(`${response.status} ${response.statusText}`) throw new Error(`${response.status} ${response.statusText}`)
} }
const reader = response.body.getReader() 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 let received = 0
const chunks = [] const chunks = []
@ -44,7 +48,7 @@ export async function httpget(url: string, onprogress: OnProgress) {
} }
export async function toBlobUrl(url: string, mimeType: 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 }) const blob = new Blob([buffer], { type: mimeType })
return URL.createObjectURL(blob) return URL.createObjectURL(blob)
} }

View File

@ -1,3 +1,4 @@
import { jsDelivr } from './jsdelivr'
import { CdnConfig } from './types' import { CdnConfig } from './types'
const owner = 'the1812' const owner = 'the1812'
@ -15,6 +16,8 @@ export const github: CdnConfig = {
sortable: `https://${host}/SortableJS/Sortable/1.14.0/Sortable.min.js`, sortable: `https://${host}/SortableJS/Sortable/1.14.0/Sortable.min.js`,
mdi: `https://${owner}.github.io/Bilibili-Evolved/static/mdi/mdi.css`, mdi: `https://${owner}.github.io/Bilibili-Evolved/static/mdi/mdi.css`,
streamsaver: `https://${host}/jimmywarting/StreamSaver.js/2.0.6/StreamSaver.js`, 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`, smallLogo: `https://${host}/${owner}/Bilibili-Evolved/preview/images/logo-small.png`,
logo: `https://${host}/${owner}/Bilibili-Evolved/preview/images/logo.png`, logo: `https://${host}/${owner}/Bilibili-Evolved/preview/images/logo.png`,

View File

@ -15,6 +15,11 @@ export const jsDelivr: CdnConfig = {
sortable: `https://${host}/npm/sortablejs@1.14.0/Sortable.min.js`, sortable: `https://${host}/npm/sortablejs@1.14.0/Sortable.min.js`,
mdi: `https://${host}/gh/${owner}/Bilibili-Evolved@master/docs/static/mdi/mdi.css`, mdi: `https://${host}/gh/${owner}/Bilibili-Evolved@master/docs/static/mdi/mdi.css`,
streamsaver: `https://${host}/npm/streamsaver@2.0.6/StreamSaver.min.js`, streamsaver: `https://${host}/npm/streamsaver@2.0.6/StreamSaver.min.js`,
ffmpeg: {
worker: `https://${host}/npm/@ffmpeg/ffmpeg@0.12.4/dist/umd/814.ffmpeg.js`,
core: `https://${host}/npm/@ffmpeg/core@0.12.4/dist/umd/ffmpeg-core.js`,
wasm: `https://${host}/npm/@ffmpeg/core@0.12.4/dist/umd/ffmpeg-core.wasm`,
},
}, },
smallLogo: `https://${host}/gh/${owner}/Bilibili-Evolved@preview/images/logo-small.png`, smallLogo: `https://${host}/gh/${owner}/Bilibili-Evolved@preview/images/logo-small.png`,
logo: `https://${host}/gh/${owner}/Bilibili-Evolved@preview/images/logo.png`, logo: `https://${host}/gh/${owner}/Bilibili-Evolved@preview/images/logo.png`,

View File

@ -11,6 +11,11 @@ export interface CdnConfig {
sortable: string sortable: string
mdi: string mdi: string
streamsaver: string streamsaver: string
ffmpeg: {
worker: string
core: string
wasm: string
}
} }
smallLogo: string smallLogo: string
logo: string logo: string