How to Download Video Subtitles with JavaScript

How to Download Video Subtitles with JavaScript

Introduction

If a video publishes subtitles as a separate HTML5 text track, you can save them from the browser console. This is useful when you want to read the captions offline or refer to them while studying.

What you need

  • A desktop browser such as Chrome or Firefox.
  • A video with an accessible subtitle or caption track, usually published as a WebVTT file.
  • Permission to download or reuse the captions.

1. Open the video and turn on captions

Open the video page and enable captions in the player. Some players add their subtitle tracks only after you choose a language.

2. Open the browser console

Open Developer Tools with Ctrl+Shift+I on Windows or Linux, or Cmd+Option+I on macOS. Select the Console tab.

3. Save the available tracks

The script looks for HTML <track> elements marked as subtitles or captions. Review it before running it in the console.

(async () => {
  const tracks = document.querySelectorAll(
    'video track[kind="subtitles"][src], video track[kind="captions"][src]'
  );

  if (tracks.length === 0) {
    console.info('No accessible HTML5 subtitle tracks were found.');
    return;
  }

  for (const track of tracks) {
    try {
      const response = await fetch(track.src);
      if (!response.ok) throw new Error(`HTTP ${response.status}`);

      const file = new Blob([await response.text()], { type: 'text/vtt' });
      const url = URL.createObjectURL(file);
      const link = document.createElement('a');
      const language = (track.srclang || track.label || 'captions')
        .replace(/[^a-z0-9_-]/gi, '-');

      link.href = url;
      link.download = `subtitles-${language}.vtt`;
      document.body.append(link);
      link.click();
      link.remove();
      setTimeout(() => URL.revokeObjectURL(url), 1000);
    } catch (error) {
      console.warn('Could not download this track:', error.message);
    }
  }
})();

How it works

The script reads each track URL, fetches the caption text and creates a temporary download link. The downloaded file retains the timing information from the original track. If the browser asks whether to allow multiple downloads, review the files you expect before continuing.

What about YouTube and other players?

The example works with standard HTML5 text tracks. YouTube and other custom players may expose captions through their own player or a transcript instead, so the script may find no tracks there. Use the player’s transcript or official download feature when available. The script cannot recover subtitles that are burned into the picture or access tracks that require permissions the page does not grant.