Guides

Add Subtitles to an hls.js or Video.js Player with Wyzie Subs#

This guide adds subtitles to a web player built on hls.js or Video.js: search by IMDb or TMDB id, take a result, get it as WebVTT and add it as a track. The code runs in the page, so it uses a browser-safe key.

Use a Browser-safe Key#

Never put a normal key in page JavaScript: anyone can read it in DevTools. A browser-safe key (wyzie-pub-, Pro) works only from the sites you list and only on /search, /c/ downloads, /quote and /sources. Replace wyzie-pub-YOUR_KEY below with yours.

Search by IMDb or TMDB ID#

Send the id and a language. IMDb ids keep the tt; for a TV episode, pass season and episode too. format=srt,vtt keeps only text files. Results come best first, and each one has url, language, display and isHearingImpaired. No match is a 404.

const KEY = 'wyzie-pub-YOUR_KEY';
 
// One subtitle for a movie (tt0111161) or, with season and episode, a TV episode.
async function findSubtitle(id, language, extra = {}) {
  const params = new URLSearchParams({
    id,
    language,
    format: 'srt,vtt',
    key: KEY,
    ...extra,
  });
  const res = await fetch(`https://sub.wyzie.io/search?${params}`);
  if (!res.ok) return null; // 404: no subtitles found
  const results = await res.json();
  return results[0] ?? null;
}
 
// The same link, served as WebVTT.
function vttUrl(sub) {
  const url = new URL(sub.url);
  url.searchParams.set('to', 'vtt');
  return url.href;
}

Get WebVTT#

Add to=vtt to a result's url and the file comes back as WebVTT (text/vtt), the format browsers play in a <track>. It works on every plan, so the page needs no SRT parser. Leave format, encoding, id, season and episode on the link as they are. Each download costs 1 request.

hls.js#

hls.js plays the stream in a normal <video>, so the browser shows a <track> you add yourself. The hls.js subtitleTracks list only holds subtitles inside the HLS playlist, so the Wyzie track is not in it. The track comes from another origin, so the <video> needs crossorigin="anonymous".

<video id="player" controls crossorigin="anonymous"></video>
 
<script src="https://cdn.jsdelivr.net/npm/hls.js@1"></script>
<script type="module">
  const video = document.getElementById('player');
  const src = '/media/movie.m3u8';
 
  if (Hls.isSupported()) {
    const hls = new Hls();
    hls.loadSource(src);
    hls.attachMedia(video);
  } else {
    video.src = src; // Safari plays HLS natively
  }
 
  // findSubtitle and vttUrl: see Search by IMDb or TMDB ID above.
  const sub = await findSubtitle('tt0111161', 'en');
  if (sub) {
    const track = document.createElement('track');
    track.kind = 'subtitles';
    track.srclang = sub.language;
    track.label = sub.display;
    track.src = vttUrl(sub);
    track.default = true;
    video.appendChild(track);
  }
</script>

Video.js#

Video.js plays .m3u8 sources itself. Add the track with player.addRemoteTextTrack(); with false as the second argument, Video.js removes the track when the source changes. Set crossorigin="anonymous" on the <video> here too.

<video id="player" class="video-js" controls crossorigin="anonymous"></video>
 
<link
  href="https://cdn.jsdelivr.net/npm/video.js@8/dist/video-js.min.css"
  rel="stylesheet"
/>
<script src="https://cdn.jsdelivr.net/npm/video.js@8/dist/video.min.js"></script>
<script type="module">
  const player = videojs('player');
  player.src({ src: '/media/movie.m3u8', type: 'application/x-mpegURL' });
 
  // findSubtitle and vttUrl: see Search by IMDb or TMDB ID above.
  const sub = await findSubtitle('tt0111161', 'en');
  if (sub) {
    player.addRemoteTextTrack(
      {
        kind: 'subtitles',
        srclang: sub.language,
        label: sub.display,
        src: vttUrl(sub),
        default: true,
      },
      false,
    );
  }
</script>

Caching#

Each download link works for 60 days, so store the search answer per title and episode instead of searching on every play. Downloading the same subtitle again with the same key within 6 hours, with options you have not used yet in that window, is free (Repeat Downloads). Serve each file from its Wyzie link: the Terms do not allow re-hosting subtitles where others can reach them.

Out-of-sync files, clean subtitles for family sites and batch search are in Subtitles for Your Streaming Site or Player.