亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Use the Element Inside the Tag
Understand the Attributes
Format Your WebVTT File Correctly
Browser Support and Fallbacks
Home Web Front-end HTML Tutorial How to add captions or subtitles to an HTML5 video?

How to add captions or subtitles to an HTML5 video?

Jul 27, 2025 am 12:06 AM
subtitle HTML5 video

Use the <track> element inside the <video> tag to add captions or subtitles. 2. Set the src attribute to point to the WebVTT file. 3. Specify the kind attribute as "subtitles", "captions", "descriptions", "chapters", or "metadata". 4. Include srclang for language identification when kind is "subtitles". 5. Add a label for the display name in the player menu. 6. Optionally use the default attribute to enable the track by default. 7. Ensure the WebVTT file starts with "WEBVTT", uses correct time formatting (HH:MM:SS.mmm), and has blank lines between cues. 8. Save the file in UTF-8 encoding. 9. Provide a fallback message inside the <video> tag for unsupported browsers. 10. Test browser compatibility and consider using libraries like video.js for enhanced support. 11. Style captions using the ::cue CSS pseudo-element. Captions enhance accessibility and user experience and are straightforward to implement when these steps are followed correctly.

How to add captions or subtitles to an HTML5 video?

Adding captions or subtitles to an HTML5 video is straightforward using the <track></track> element. This feature enhances accessibility and improves user experience, especially for viewers who are deaf or hard of hearing, or those watching in noisy environments.

How to add captions or subtitles to an HTML5 video?

Use the <track></track> Element Inside the <video></video> Tag

The <track></track> element allows you to specify external text tracks (like subtitles or captions) in supported formats—most commonly WebVTT (.vtt files).

Here’s a basic example:

How to add captions or subtitles to an HTML5 video?
<video controls>
  <source src="movie.mp4" type="video/mp4">
  <track src="subtitles.en.vtt" kind="subtitles" srclang="en" label="English" default>
  <track src="subtitles.es.vtt" kind="subtitles" srclang="es" label="Spanish">
  Your browser does not support the video tag or text tracks.
</video>

Understand the <track> Attributes

Each <track> tag supports several important attributes:

  • src: Path to the WebVTT file (e.g., subtitles.en.vtt)
  • kind: Specifies the type of text track:
    • subtitles – Translations of the dialogue (useful for non-native speakers)
    • captions – Includes dialogue and sound descriptions (ideal for accessibility)
    • descriptions – Text descriptions of the video for screen readers
    • chapters – Chapter titles for navigation
    • metadata – Used for scripting, not visible to users
  • srclang: Language of the track (e.g., en, es, fr). Required when kind="subtitles".
  • label: Human-readable name shown in the player’s menu (e.g., "English").
  • default: (Optional) Tells the browser to enable this track by default. Only one track should have this attribute.

Format Your WebVTT File Correctly

WebVTT files must start with WEBVTT and use a specific time format. Example (subtitles.en.vtt):

How to add captions or subtitles to an HTML5 video?
WEBVTT

1
00:00:05.000 --> 00:00:08.000
Hello, welcome to the video.

2
00:00:10.000 --> 00:00:13.000
This is an example of subtitles.

Make sure:

  • The file is saved with UTF-8 encoding
  • Time format is HH:MM:SS.mmm
  • There’s a blank line between cues

Browser Support and Fallbacks

Most modern browsers support <track> and WebVTT, but:

  • Always include a fallback message inside the <video> tag
  • Test on target browsers (especially older versions of Safari or mobile browsers)
  • Consider using JavaScript libraries like video.js for broader compatibility and better UI controls

Also, note that:

  • Captions won’t display unless the user enables them (except the default track)
  • Styling captions is limited, but you can use ::cue CSS pseudo-elements:
video::cue {
  background-color: black;
  color: white;
  font-size: 1.2em;
}

Basically, just link your .vtt file with the right attributes and ensure the format is correct. It’s simple but easy to miss small details like the srclang or kind values.

The above is the detailed content of How to add captions or subtitles to an HTML5 video?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
Are nfo files subtitles? Are nfo files subtitles? Jan 30, 2023 am 10:49 AM

nfo files are not subtitles, and subtitle files are not in nfo format; common subtitle file formats include Srt format, Webvtt format, STL format, Sbv format, Ass format, Dfxp format, Ttml format, etc.; and nfo records the video producer’s production process A file of various data information for the film.

How to turn on live captions instantly in Windows 11 How to turn on live captions instantly in Windows 11 Jun 27, 2023 am 08:33 AM

How to turn on live subtitles instantly in Windows 11 1. Press Ctrl+L on your keyboard 2. Click Agree 3. A popup will appear saying Ready to add subtitles in English (US) (depending on your preferred language) 4. Additionally, you can filter profanity by clicking the gear button? Preference? Filtering Swear Words Related Articles How to Fix Activation Error Code 0xc004f069 in Windows Server The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Pass these preliminary checks and if these checks do not

How to adjust the subtitles of National K-song How to adjust the subtitles of National K-song Feb 23, 2024 pm 07:16 PM

How to adjust the subtitles of karaoke? The subtitles of lyrics can be adjusted in the National Karaoke APP, but many users do not know how to adjust the subtitles of Karaoke moments. The latest solutions to the problems will be continuously sorted out below. Next is the National Karaoke that the editor brings to users. Here is a graphic tutorial on how to adjust subtitles. Interested users can take a look together! Tutorial on the use of National Karaoke. How to adjust the subtitles of National Karaoke. 1. First open the Universal Karaoke APP, switch to the [Voice Sing] section on the main page, select your favorite song and click [K Song]; 2. Then switch to the Karaoke page, Click the red button [Start Singing] at the bottom; 3. Then on the latest page, click the [three dots] function in the upper right corner; 4. Finally, expand the function bar at the bottom and select [Font Size] to adjust

How to add subtitles and speaking sounds to cut videos How to add subtitles and speaking sounds to cut videos Mar 26, 2024 pm 05:14 PM

[Dubbing] 1. Open the clipping app and import the video file to which dubbing needs to be added. 2. Click the &quot;Audio&quot; button at the bottom of the video editing interface and select the &quot;Recording&quot; option. 3. Click the red recording button below to start recording the desired dubbing content. 4. After the recording is completed, click the &quot;Export&quot; button in the upper right corner and save it. [With subtitles] 1. Open the clipping app and import the video file to which subtitles need to be added. 2. Click the &quot;Text&quot; button at the bottom of the video editing interface and select the &quot;New Text&quot; option. 3. Enter the subtitles you want to add in the text box, and then set

How to identify subtitles in the computer version of movie editing - How to identify subtitles in the computer version of movie editing How to identify subtitles in the computer version of movie editing - How to identify subtitles in the computer version of movie editing Mar 04, 2024 am 11:43 AM

Many people are using the computer version of the computer version in the office, but do you know how to recognize the subtitles in the computer version of the computer version? Next, the editor will bring you the method of identifying the subtitles in the computer version of the computer version. Interested users should quickly Let’s take a look below. Open the computer version of the clip and enter the homepage. Select text in the menu bar above the cutout. Just select Start Recognition under Smart Subtitles.

How to add subtitles and dubbing to movie clipping - video editing video tutorial for beginners How to add subtitles and dubbing to movie clipping - video editing video tutorial for beginners Mar 18, 2024 pm 01:04 PM

Add subtitles: 1. Enter the software and click [Start Creating]. 2. Click [Text] below. 3. Select [New Text]. 4. Enter text, adjust the style and save. 5. The addition is completed. Add dubbing: 6. Select [Audio]. 7. Click [Recording]. 8. Start recording. Introductory Tutorial on Screen Cutting 1. Video Segmentation If you want to learn video editing, you must learn video segmentation. For users who have short video production needs, learning video segmentation can allow us to learn video in a very short time. For the basic operations of editing, next we use the Windows 10 version of Silhouette (Shadow) to demonstrate how to split a video. As shown below: The material of the video is a TV series image. New users can use this type of video first.

Douyin automatic subtitle setting operation explanation Douyin automatic subtitle setting operation explanation Mar 26, 2024 pm 08:20 PM

1. Now if you want to set up automatic subtitles, the first step is to shoot a video through Douyin, or import a local video file on your phone, enter the video editing page, and find the drop-down button in the upper right corner (as shown below). 2. Through the drop-down list, we can find the new [Automatic Subtitles] function. Click to identify the voice in the current video and add subtitles. 3. Moreover, the color, format and other effects of subtitles can now be automatically set by you, allowing more users to enjoy the convenience. If you are interested, start now.

How to play a video file with subtitles using PHP? How to play a video file with subtitles using PHP? Aug 07, 2023 pm 12:13 PM

How to play a video file with subtitles using PHP? With the development of Internet technology, video has become one of the most popular forms of online content. Now, many video files are equipped with subtitles, which makes it easier for users to understand and digest video content. So, how to use PHP to play video files with subtitles on a web page? This article will introduce it to you in detail. First, we need to prepare the following environment: a server with a PHP running environment (such as Apache or Nginx); a video file (support

See all articles