-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c25efae
commit 4b689b4
Showing
11 changed files
with
209 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
/// <reference types="vite/client" /> | ||
|
||
declare module "libass-wasm"; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type Artplayer from "artplayer" | ||
import type SubtitlesOctopus from "libass-wasm" | ||
import { type Options } from "libass-wasm" | ||
|
||
export = artplayerPluginAss | ||
export as namespace artplayerPluginAss | ||
type Ass = { | ||
name: "artplayerPluginAss" | ||
instance: SubtitlesOctopus | ||
} | ||
|
||
declare const artplayerPluginAss: (options: Options) => (art: Artplayer) => Ass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* ref: https://github.com/alist-org/alist-web/blob/main/src/components/artplayer-plugin-ass/index.js | ||
*/ | ||
import SubtitlesOctopus from "libass-wasm" | ||
import workerUrl from "libass-wasm/dist/js/subtitles-octopus-worker.js?url" | ||
import wasmUrl from "libass-wasm/dist/js/subtitles-octopus-worker.wasm?url" | ||
|
||
import TimesNewRomanFont from "./fonts/TimesNewRoman.ttf?url" | ||
import fallbackFont from "./fonts/SourceHanSansCN-Bold.woff2?url" | ||
|
||
let instance = null | ||
|
||
function isAbsoluteUrl(url) { | ||
return /^https?:\/\//.test(url) | ||
} | ||
|
||
function toAbsoluteUrl(url) { | ||
if (isAbsoluteUrl(url)) return url | ||
|
||
// handle absolute URL when the `Worker` of `BLOB` type loading network resources | ||
return new URL(url, document.baseURI).toString() | ||
} | ||
|
||
function loadWorker({ workerUrl, wasmUrl }) { | ||
return new Promise((resolve) => { | ||
fetch(workerUrl) | ||
.then((res) => res.text()) | ||
.then((text) => { | ||
let workerScriptContent = text | ||
|
||
workerScriptContent = workerScriptContent.replace( | ||
/wasmBinaryFile\s*=\s*"(subtitles-octopus-worker\.wasm)"/g, | ||
(_match, wasm) => { | ||
if (!wasmUrl) { | ||
wasmUrl = new URL(wasm, toAbsoluteUrl(workerUrl)).toString() | ||
} else { | ||
wasmUrl = toAbsoluteUrl(wasmUrl) | ||
} | ||
|
||
return `wasmBinaryFile = "${wasmUrl}"` | ||
}, | ||
) | ||
|
||
const workerBlob = new Blob([workerScriptContent], { | ||
type: "text/javascript", | ||
}) | ||
resolve(URL.createObjectURL(workerBlob)) | ||
}) | ||
}) | ||
} | ||
|
||
function setVisible(visible) { | ||
if (instance.canvasParent) | ||
instance.canvasParent.style.display = visible ? "block" : "none" | ||
} | ||
|
||
function artplayerPluginAss(options) { | ||
return async (art) => { | ||
instance = new SubtitlesOctopus({ | ||
// TODO: load available fonts from manage panel | ||
availableFonts: { | ||
"times new roman": toAbsoluteUrl(TimesNewRomanFont), | ||
}, | ||
workerUrl: await loadWorker({ workerUrl, wasmUrl }), | ||
fallbackFont: toAbsoluteUrl(fallbackFont), | ||
video: art.template.$video, | ||
...options, | ||
}) | ||
|
||
instance.canvasParent.className = "artplayer-plugin-ass" | ||
instance.canvasParent.style.cssText = ` | ||
position: absolute; | ||
width: 100%; | ||
height: 100%; | ||
user-select: none; | ||
pointer-events: none; | ||
z-index: 20; | ||
` | ||
// switch subtitle track | ||
art.on("artplayer-plugin-ass:switch", (subtitle) => { | ||
let newSubAddr | ||
if (subtitle.startsWith('/api/movie/proxy/')) { | ||
newSubAddr = window.location.origin + subtitle | ||
} else { | ||
newSubAddr = subtitle | ||
} | ||
instance.freeTrack() | ||
instance.setTrackByUrl(newSubAddr) | ||
console.log("plugin->切换字幕:", newSubAddr); | ||
setVisible(true) | ||
}) | ||
|
||
// set subtitle visible | ||
art.on("subtitle", (visible) => setVisible(visible)) | ||
art.on("artplayer-plugin-ass:visible", (visible) => setVisible(visible)) | ||
|
||
// set subtitle offset | ||
art.on("subtitleOffset", (offset) => (instance.timeOffset = offset)) | ||
|
||
// when player destory | ||
art.on("destroy", () => instance.dispose()) | ||
|
||
return { | ||
name: "artplayerPluginAss", | ||
instance: instance, | ||
} | ||
} | ||
} | ||
|
||
export default artplayerPluginAss |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters