Switching between Japanese and English subtitles in videos | track

プログラミング・Web開発カテゴリを表すパンダのイラスト Programming / Web Development

About this article
This article was created using an automated generation workflow utilizing generative AI. It is based on the HTML Standard, MDN track and TextTrack documentation, the W3C WebVTT specification, and subtitle selection and cue timing from existing learning materials.

Verification status: 📘 Official information verified – Code statically verified – Browser execution unverified

Even when video subtitles are available in both Japanese and English, it can be challenging to figure out how to build a video player menu or toggle button. Using the HTML track element, time-synchronized subtitle data can be added to a video. You will learn how switching only the subtitles at the same moment in the same video eliminates the need to duplicate video assets for each language.

Live demo to try Japanese, English, and subtitle off(The page could not be opened from this environment, so actual display and operation remain unverified)

What this tutorial allows you to test

ItemContent
Target audienceBeginners and Web developers learning HTML video, subtitle tracks, and WebVTT
OperationAfter clicking Run, select the Japanese, English, or Subtitle Off buttons
ObservationOnly the subtitle text changes during the 1 to 3.2-second segment of the same video
Input dataA short video generated via canvas and fictional subtitles
Prerequisites and limitationsBrowsers supporting HTMLCanvasElement.captureStream() and MediaRecorder
CommunicationVideo and subtitles are generated within the page. Diagram rendering communicates with the Mermaid CDN.

When you click "Run", the demo records the canvas for about 3.3 seconds to create a video and prepares two sets of WebVTT data for subtitles. The video is silent, and the visual displays "Daily Code" along with the elapsed seconds. The subtitle cue runs from 1.0 seconds to 3.2 seconds.

Next, clicking "Japanese" resets the video position to 1.3 seconds, plays it, and advances to the time range where the subtitles appear. Clicking "English" displays "Sample caption" in the same section, and "Subtitles Off" hides both tracks. The "around 2 seconds" mentioned in the article is an estimated playback time and not a result verified through actual browser display checks.

The learning screen also includes a common input field, but demo ID 49 hides this field and does not load it as an experimental value. The operation to change conditions is to press the three subtitle buttons in order.

Subtitles are synchronized with the playback position and cues.

The track element adds timed text to video or audio. The HTML Standard defines types such as subtitles, captions, audio descriptions, chapters, and metadata. Subtitle sentences are displayed while the playback position is greater than or equal to the cue start time and less than the end time.

The following HTML is the basic structure of a subtitle track. It is an example of associating Japanese and English WebVTT files with an existing video, and the video file itself is required separately.

<video controls>
  <track
    kind="subtitles"
    src="subtitles-ja.vtt"
    srclang="ja"
    label="日本語"
  >
  <track
    kind="subtitles"
    src="subtitles-en.vtt"
    srclang="en"
    label="English"
  >
</video>
  • The kind attribute specifies the purpose of the track. Subtitles are used to display spoken content in another language. Captions are used to convey audio information in text, including speaker names and meaningful sounds.

  • The srclang attribute is a language tag indicating the language of the subtitle data. The demo specifies "ja" for Japanese and "en" for English.

  • The label attribute is the language name displayed to the user. This makes it easier to identify the language even when using the standard browser subtitle menu.

  • The video controls attribute displays the standard player operation UI. In addition to this, the demo includes custom buttons that make it easier to observe behavior.

WebVTT, used for track data, is a format where "WEBVTT" is written at the top, followed by time ranges and subtitle sentences.

WEBVTT

00:00:01.000 --> 00:00:03.200
字幕の見本

Cue times represent playback positions within the video. A cue from 1 second to 3.2 seconds is active while playing around the 2-second mark. It does not independently monitor the exact moment 2 seconds of real time have passed since the video started. If the loaded video's length and playback position do not match the cue times, the subtitles will not appear at the expected scenes.

Changing the Subtitle State with TextTrack.mode

You can retrieve the TextTrack from the HTMLTrackElement's track property. TextTrack.mode is a property used to switch the subtitle display state.

modeStateDisplay
showingenabledDisplay cues over the video
hiddenenabledProcess cues without displaying them
disableddisabledDo not use cues

In the demo, the selected track is set to showing and the rest are set to disabled. Selecting "Captions Off" sets all to disabled. Tracks with a disabled mode do not load caption data immediately, and loading may begin when the mode is changed to showing or hidden. Because the demo VTT uses a Blob URL, there is no need to prefetch an external file without using captions.

The processing flow is as follows.

flowchart LR
  A["動画の再生位置"] --> B["cueの開始・終了時刻"]
  B --> C{"選んだtrackはshowing?"}
  C -- "はい" --> D["動画上に字幕を表示"]
  C -- "いいえ" --> E["字幕を表示しない"]

The diagram simplifies the relationship between track mode and cue range affecting display. It does not show audio and video decoding or caption file parsing steps.

Why you can compare only captions with the same video

Japanese and English are associated with the video as separate tracks. Since the playback position is common to both, there is no need to duplicate the video when switching captions.

SelectJapanese trackEnglish trackResults viewed around the same 2-second mark
JapaneseshowingdisabledCaption Sample
EnglishdisabledshowingSample caption
Captions offdisableddisabledNo captions

In this demo, each button resets the playback position to the same point before playing, allowing for a comparison under identical conditions other than language. In a production interface, it may sometimes be more natural not to automatically reset the video position when a caption is selected. This demo is designed as an educational tool to make it easier to observe differences and is not a UI suited for every video player.

Do not confuse subtitles with captions

Subtitles are used to convey spoken content in a different language, such as translating English audio into Japanese. Captions are used to provide information to users who have difficulty hearing audio, by displaying speaker identification and important non-speech sounds such as door closing sounds in text.

The kind attribute in this demo is set to subtitles, and the cue consists of only a single short sentence. This does not evaluate the overall quality or accessibility of the subtitles. For public content, select the appropriate caption type based on users, platform requirements, laws, and internal standards.

Troubleshooting when it does not work

The execution button displays Video generation is not supported

The demo uses canvas.captureStream() and MediaRecorder. Browsers that lack either of these or block generation cannot produce the video. Since changing the input values will not resolve this, try using another supported browser or switch to a static track example that uses an existing video file.

The video plays, but captions do not appear

Click the Japanese or English button and verify that the playback position is within the range of 1.0 to 3.2 seconds. Compare the subtitle timestamps with the video duration. If playback does not start when clicking the button, also check the browser media controls and any errors displayed in the console.

Cannot load the actual WebVTT file

Check WEBVTT at the beginning of the file, UTF-8, time range, and video playback duration. 00:00:01.000 –>Place half-width spaces on both sides of the arrow, like 00:00:03.200. When delivering subtitles from a different domain, also check the HTTP response, MIME type (text/vtt), and CORS settings. External delivery settings do not apply to the Blob URL in the demo.

Things to check before using in production

When adding subtitles in multiple languages, attaching language-specific tracks to the same video asset allows managing subtitle text updates and language additions separately from video editing. On the other hand, if you only translate the text without checking the cue timing, subtitles may appear mid-sentence or remain through the next scene. Check subtitles at playback positions in the opening, middle, and end at a minimum to ensure they match the audio or on-screen explanations.

When distributing subtitle files or videos externally, check the publication scope, usage rights, and translation quality. Do not include confidential meeting subtitles, personally identifiable audio, or unreleased footage in public URL demos. In this tutorial material, users do not select files, and the short video and text generated from the canvas are handled as a Blob URL within the browser. Diagrams involve communication with the Mermaid CDN.

Official Documentation and Verification Scope

The official specifications were cross-referenced with the HTML, common JavaScript, and tutorial README on GitHub. GitHub Pages cannot be accessed from this environment, and video generation, subtitle rendering, native browser subtitle menus, and operations with assistive technologies have not been verified. Expected behaviors are explained based on static code verification and are not guaranteed as verified actual behavior.

Fixed commit tutorial README and implementation

Document information

Article title
Switching between Japanese and English subtitles in videos | track
Published
Updated
Source
https://papanda925.com/?p=19030&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL