Chapter 33 - Sources and video captioning
In this chapter we conclude our look at the video
and audio
elements with a look at the use of the source
element as well as the track
element.
source
As with the picture
element, the source
element is used to specify various sources from which the user agent will choose the first it supports. Unlike the picture
element, the available attributes are more limited. When used with either the video
or audio
element, the only valid attributes are:
src
type
Even with this limitation, the source
element is very useful, and the most common way the video
and audio
elements are used. Let’s look at an example.
|
|
Live Codepen - Using the source
element with video
A quick side note about the formats above. You will notice that I only use two formats above but, yesterday I mentioned more “formats” than that. Here we need to distinguish between containers and codecs(see further reading in the related reading section). MP4 and WebM are containers for video and audio but, both the video and audio channels can be encoded using a variety of supported codecs. Which codec is supported, depends on the container. So, while you can use VP8, VP9, Theora, or AV1 to encode you video for example, you will most likely stick with either WebM or MP4 as the container you use to deliver your video files.
|
|
Live Codepen - Using the source
element with audio
Above is how you would use source
with the audio
element. Again, please see the related reading section for various links to codecs and containers. As mentioned, a user agent will pick the first version it supports, starting from the first entry and moving on. The Opus format is a great general use format for audio but, there are support caveats with regards to Safari that you should be aware off. But then, that is exactly what the source
element is for 😀 We start by specifying Opus, followed by MP3 and lastly, an m4a
file. The last one uses the Advanced Audio Coding(AAC) codec inside a MPEG-4 container which is widely supported across browsers.
In terms of file size, though, the above three breaks down as follows:
- Opus: 549kb
- MP3: 1.4MB
- AAC: 2MB
track
When adding video or audio to your website, there is an important point to remember. Without captions, subtitles, or a transcript, there is a large audience that is completely excluded from accessing the media. The track
element is one of the pieces that help solve this. The track
element has a couple of attributes to take note of.
kind
This attribute has five possible values: subtitles, captions, descriptions, chapters, and metadata. the default value, if omitted is subtitles. It describes the intended purpose of the content of the text track. You can find additional details on each kind here.
|
|
src
The src
attribute as with other elements points to the location of the associated text track. One of the common formats used on the web is the Web Video Text Tracks(WebVTT) format. This is a specification all on its own and so, covering the details is beyond the scope of the 150 days of HTML but, we will look at a small example so you can get an idea of one of the ways it can be used.
|
|
srclang
Often captions or subtitles are not only used to provide a means of access for deaf or hard of hearing persons but, to provide a translation of the language used in the video. In these instances, you would specify the language of the text track using the srclang
attribute.
|
|
label
The label
attribute is used to provide a descriptive name for the text track. This should be in a user-readable format as it will be exposed by user agents when listing the available subtitles, captions, or audio descriptions.
|
|
When the value of the label
attribute is in a different language from the default language of the page, it is also advisable to set the lang
attribute to identify the language used.
|
|
default
The final attribute of the track
element is the default
attribute. It is a Boolean attribute and when specified, indicates that the track should be used by default if the user did not specify a preference.
|
|
That covers all of the attributes and their use with the track
element. To conclude today’s post, let’s take a look at an example of putting all of this together.
|
|
Live Codesandbox - Using the video
element with source
and track
elements
You can also see an example of the English VTT file being used here below.
|
|
As mentioned before, below is a BIG list of tools and related reading on all things video, audio, subtitling, captioning, etc. Enjoy! 😀 🙃
Related reading
- Web audio codec guide
- Converting MP4 to WebM
- Container formats
- HandBrake: The open source video transcoder
- boram - Cross platform, open source video encoder with support for AV1
- FFMpeg
- Audacity
- Encoding AV1
- What are video encoding formats?
- Audio and video types
- Discrete types
- Web Video Text Tracks Format