通过 JavaScript 显示 .ass 格式的字幕。支持所有 SSA/ASS 功能,可轻松集成到 HTML5 视频中。
JASSUB
The Fastest JavaScript SSA/ASS Subtitle Renderer For Browsers.
JASSUB is a JS wrapper for libass, which renders SSA/ASS subtitles directly in your browser. It uses Emscripten to compile libass' C++ code to WASM, and WebGL for hardware acceleration, and outperforms even native implementations.
Demo
The
{
"Cross-Origin-Embedder-Policy": "require-corp",
"Cross-Origin-Opener-Policy": "same-origin"
}headers are recommended to use this library, as it uses SharedArrayBuffer for multi-threading, but if you can't set them, it will fallback automatically to work in single-threaded mode. Firefox doesn't support threading so they are not required there.
At minimum WASM + TextDecoder + OffscreenCanvas + Web Workers + Proxy + Fetch + Promise + getVideoPlaybackQuality/requestVideoFrameCallback are required for JASSUB to work.
JASSUB supports Chrome/Safari/Firefox 80/17/105, you bring the support down to 67/16.2/68 if you enable some flags/settings in your browser for these features. For other engines polyfills might be needed. Babel is also recommended if you need to support older JS engines as JASSUB ships as ES modules with modern syntax.
Install the library via:
[p]npm i jassubimport JASSUB from 'jassub'
const instance = new JASSUB({
video: document.querySelector('video'),
subUrl: './tracks/sub.ass'
})If you use a custom bundler, and need to override the worker and wasm URLs you can instead do:
import JASSUB from 'jassub'
import workerUrl from 'jassub/dist/jassub-worker.js?worker&url'
import wasmUrl from 'jassub/dist/jassub-worker.wasm?url' // non-SIMD fallback
import modernWasmUrl from 'jassub/dist/jassub-worker-modern.wasm?url' // SIMD
const instance = new JASSUB({
video: document.querySelector('video'),
subContent: subtitleString,
workerUrl, // you can also use: `new URL('jassub/dist/jassub-worker.js', import.meta.url)` instead of importing it as an url, or whatever solution suits you
wasmUrl,
modernWasmUrl
})However this shoud almost never be necessary.
You're also able to use it without any video. However, that requires you to set the time the subtitles should render at yourself:
import JASSUB from 'jassub'
const instance = new JASSUB({
canvas: document.querySelector('canvas'),
subUrl: './tracks/sub.ass'
})
await instance.ready
instance.manualRender({ expectedDisplayTime: performance.now(), width: 1920, height: 1080, mediaTime: 10.20 })The library is fully typed, so you can simply browse the types of instance or instance.renderer. "Private" fields are prefixed with _ such as _fontId or _findAvailableFonts, and shouldn't be used by developers, but can if the need arises.
instance.renderer calls are ALWAYS async as it's a remote worker, which means you should always await/then them for the IPC call to be serialized!!! For example:
const x = instance.renderer.useLocalFonts // does nothing, returns IPC proxy object
const y = await instance.renderer.useLocalFonts // returns true/false
instance.renderer.useLocalFonts = false // this is fine
await (instance.renderer.useLocalFonts = false) // or u can await it for safety
instance.renderer.setDefaultFont('Gandhi Sans') // this is fine, sets default font
await instance.renderer.setDefaultFont('Gandhi Sans') // or you can await if if you wantMake sure to always await instance.ready before running any methods!!!
Example usage can be found in the demo source here.
If you know for sure that your subtitles use specific fonts, you can pre-load them via the fonts option when creating the JASSUB instance:
const instance = new JASSUB({
video: document.querySelector('video'),
subUrl: './tracks/sub.ass',
fonts: [new URL('./fonts/GandhiSans-Regular.woff', import.meta.url).href, new Uint8Array(data)]
})This will load/fetch the fonts ASAP when the renderer and WASM is initiated, this process is non-blocking.
If you however have a very big database of fonts and/or you're unsure if your subtitles use, or you want to conserve memory, bandwidth etc you can define fonts via availableFonts, which is a case-insensitive, postscript-insensitive map of fonts and their sources. This means the keys can, but don't need to include the weight of the font, but it is preferred. For example:
const instance = new JASSUB({
video: document.querySelector('video'),
subUrl: './tracks/sub.ass',
availableFonts: {
'Gandhi Sans': new URL('./fonts/GandhiSans-Regular.ttf', import.meta.url).href,
'RoBoTO mEdiuM': new Uint8Array(data), // this is quite stupid if you want to conserve resources, since the data will be lingering in memory, but it is supported
'roboto': new URL('./fonts/Roboto-Medium.woff2', import.meta.url).href
}
})When JASSUB then needs one of these fonts for immediate rendering it will load the font from the given source, however this can cause a flash of unstyled text if the default font was previously loaded, as the font is being loaded asynchronously, which looks something like this:
With complex typesetting this might not just be text, but glyphs, icons etc. If the default font wasn't previously loaded and wasn't pre-loaded a FOUT won't happen!, and nothing will render for at most a few frames as the font is being downloaded from the given URL.
The above also applies to the default font, you can pre-load it via fonts[], or use availableFonts. If you use await instance.renderer.setDefaultFont('Gandhi Sans') and wish to preload it, you should do so manually via await instance.renderer.addFonts(['Gandhi Sans']), however this is not recommended as it can cause FOUTs as explained above. JASSUB defines and provides a default font so configuring one is not strictly necessary.
For the best user experience, which avoids FOUTs, while using as little memory/bandwidth as possible, you should use a config in the lines of:
…By default, JASSUB will only use embedded, constructor defined and local fonts. However, if you want to enable online font finding, you can do so by setting the queryFonts option to 'localandremote' when creating the JASSUB instance, note that this loads 50+ KB of code:
const instance = new JASSUB({
video: document.querySelector('video'),
subUrl: './tracks/sub.ass',
queryFonts: 'localandremote'
})This finds fonts from the free and public Google Fonts API if they aren't available locally or embedded, which has some privacy implications [in theory, not in practice]. Be mindful of the licensing. Note that Google Fonts doesn't include a lot of non-free fonts such as Arial, so this isn't a perfect solution.
If you want to support even older engines, then please check the v1.8.8 tag, or install it via:
[p]npm i [email protected]Support for older browsers (without OffscreenCanvas, WebAssembly threads, etc) has been dropped in v2.0.0 and later.
Run git clone --recursive https://github.com/ThaUnknown/jassub.git
暂无开放 Issues,或尚未同步最近议题。