Home

Awesome

ASS.js

GitHub Action Codecov License File size

<span></span> <a href="https://ass.js.org/">Online Demo</a> <span></span> <a href="https://github.com/weizhenye/ASS/wiki/ASS-%E5%AD%97%E5%B9%95%E6%A0%BC%E5%BC%8F%E8%A7%84%E8%8C%83">ASS specs (zh-Hans)</a> <span></span> <a href="https://github.com/weizhenye/ass-compiler">ass-compiler</a> <span></span>

ASS.js renders ASS subtitles on HTML5 video, with almost full ASS features.

It's lightweight and suitable for web, 60x smaller than WebAssembly solutions:

SolutionSize
ASS.jsDOM
JavascriptSubtitlesOctopusWebAssembly
JASSUBWebAssembly

WebAssembly solutions also requires to set fallback font to avoid CJK characters turning into tofu, it's a huge cost for web. In ASS.js font fallback is handled by browser, it just works.

However compared to WebAssembly solutions, it's almost impossible for DOM to render exactly same result in every pixels as VSFilter or libass, ASS.js will provide best efforts to accurate rendering.

Installation

NPM Version jsDelivr

npm install assjs

ASS.js can be used as a JavaScript module:

<script type="module">
import ASS from '/path/to/assjs/dist/ass.min.js';
</script>

or a classic script:

<script src="/path/to/assjs/dist/ass.global.min.js"></script>
<script>
console.log(window.ASS);
</script>

Usage

<div id="player">
  <video id="video" src="./example.mp4"></video>
  <div id="ass-container"></div>
</div>
import ASS from 'assjs';

const content = await fetch('/path/to/example.ass').then((res) => res.text());
const ass = new ASS(content, document.querySelector('#video'), {
  container: document.querySelector('#ass-container'),
});

new ASS() will append several elements to the container, and sync the render area's size with the video element. You should set styles yourself to make sure the container is overlap on the video and match the position. For example:

<div id="player" style="position: relative;">
  <video id="video" src="./example.mp4" style="position: absolute; top: 0; left: 0;"></video>
  <div id="ass-container" style="position: absolute; top: 0; left: 0;"></div>
</div>

If you click the native fullscreen button in video element, only <video> will be fullscreened, so ASS will not show. You should use a custom button and call document.querySelector('#player').requestFullscreen() to ensure ASS is contained.

API

Initialization

const ass = new ASS(content, video, {
  // Subtitles will display in the container.
  container: document.getElementById('my-container'),

  // see resampling API below
  resampling: 'video_width',
});

Show

ass.show();

Hide

ass.hide();

Destroy

ass.destroy();

Delay

// Subtitles will be 5s later
ass.delay = 5;
// Subtitles will be 3s earlier
ass.delay = -3;

Resampling

When script resolution(PlayResX and PlayResY) don't match the video resolution, this API defines how it behaves. However, drawings and clips will be always depending on script origin resolution.

There are four valid values, we suppose video resolution is 1280x720 and script resolution is 640x480 in following situations:

ass.resampling = 'video_width';

Browser Compatibility

ASS.js uses many Web APIs to render subtitles, some features will be disabled if you use a old browser.

FeatureWeb APIChromeFirefoxSafari
Auto resizeResizeObserver646913.1
\[i]clipclip-path and path()889713.1
Animations (\t)registerProperty()7812816.4
accel in \tlinear()11311217.2
\q0text-wrap: balance11412117.5
BorderStyle=3 with \bord0@container111-18.0
\blur with \bord0round()12511815.4