HTML Video element
(
<video>
) embeds a media player which supports video playback into the document. You can use
<video>
for audio content as well, but the
<audio>
element may provide a more appropriate user experience.
The above example shows simple usage of the
<video>
element. In a similar manner to the
<img>
element, we include a path to the media we want to display inside the
src
attribute; we can include other attributes to specify information such as video width and height, whether we want it to autoplay and loop, whether we want to show the browser's default video controls, etc.
The content inside the opening and closing
<video></video>
tags is shown as a fallback in browsers that don't support the element.
Like all other HTML elements, this element supports the 全局属性 .
autoplay
To disable video autoplay,
autoplay="false"
will not work; the video will autoplay if the attribute is there in the
<video>
tag at all. To remove autoplay, the attribute needs to be removed altogether.
In some browsers (e.g. Chrome 70.0) autoplay doesn't work if no
muted
attribute is present.
autoPictureInPicture
true
indicates that the element should automatically toggle picture-in-picture mode when the user switches back and forth between this document and another document or application.
buffered
TimeRanges
对象。
controls
If this attribute is present, the browser will offer controls to allow the user to control video playback, including volume, seeking, and pause/resume playback.
controlslist
controlslist
attribute, when specified, helps the browser select what controls to show on the media element whenever the browser shows its own set of controls (e.g. when the
controls
attribute is specified).
The allowed values are
nodownload
,
nofullscreen
and
noremoteplayback
.
使用
disablePictureInPicture
attribute if you want to disable the Picture-In-Picture mode (and the control).
crossorigin
<canvas>
element without being
tainted
. The allowed values are:
anonymous
Origin:
HTTP header without a cookie, X.509 certificate, or performing HTTP Basic authentication. If the server does not give credentials to the origin site (by not setting the
Access-Control-Allow-Origin:
HTTP header), the image will be
tainted
, and its usage restricted.
use-credentials
Origin:
HTTP header with a cookie, a certificate, or performing HTTP Basic authentication. If the server does not give credentials to the origin site (through
Access-Control-Allow-Credentials:
HTTP header), the image will be
tainted
and its usage restricted.
Origin:
HTTP header), preventing its non-tainted used in
<canvas>
elements. If invalid, it is handled as if the enumerated keyword
anonymous
was used. See
CORS 设置属性
for additional information.
currentTime
Reading
currentTime
returns a double-precision floating-point value indicating the current playback position of the media specified in seconds. If the media has not started playing yet, the time offset at which it will begin is returned. Setting
currentTime
sets the current playback position to the given time and seeks the media to that position if the media is currently loaded.
If the media is being streamed, it's possible that the
用户代理
may not be able to obtain some parts of the resource if that data has expired from the media buffer. Other media may have a media timeline that doesn't start at 0 seconds, so setting
currentTime
to a time before that would fail. The
getStartDate()
method can be used to determine the beginning point of the media timeline's reference frame.
disablePictureInPicture
Prevents the browser from suggesting a Picture-in-Picture context menu or to request Picture-in-Picture automatically in some cases.
disableRemotePlayback
In Safari, you can use
x-webkit-airplay="deny"
as a fallback.
duration
只读
NaN
. If the media has no known end (such as for live streams of unknown duration, web radio, media incoming from
WebRTC
, and so forth), this value is
+Infinity
.
height
intrinsicsize
naturalWidth
/
naturalHeight
on images would return the values specified in this attribute.
Explainer
,
examples
loop
A Boolean attribute; if specified, the browser will automatically seek back to the start upon reaching the end of the video.
muted
false
, meaning that the audio will be played when the video is played.
playsinline
poster
A URL for an image to be shown while the video is downloading. If this attribute isn't specified, nothing is displayed until the first frame is available, then the first frame is shown as the poster frame.
preload
none
: Indicates that the video should not be preloaded.
metadata
: Indicates that only video metadata (e.g. length) is fetched.
auto
: Indicates that the whole video file can be downloaded, even if the user is not expected to use it.
auto
值。
The default value is different for each browser. The spec advises it to be set to
metadata
.
autoplay
attribute has precedence over
preload
。若
autoplay
is specified, the browser would obviously need to start downloading the video for playback.
src
<source>
element within the video block to specify the video to embed.
width
| Event Name | Fired When |
|---|---|
audioprocess
|
The input buffer of a
ScriptProcessorNode
is ready to be processed.
|
canplay
|
The browser can play the media, but estimates that not enough data has been loaded to play the media up to its end without having to stop for further buffering of content. |
canplaythrough
|
The browser estimates it can play the media up to its end without stopping for content buffering. |
complete
|
The rendering of an
OfflineAudioContext
is terminated.
|
durationchange
|
duration
attribute has been updated.
|
emptied
|
The media has become empty; for example, this event is sent if the media has already been loaded (or partially loaded), and the
load()
method is called to reload it.
|
ended
|
Playback has stopped because the end of the media was reached. |
loadeddata
|
The first frame of the media has finished loading. |
loadedmetadata
|
The metadata has been loaded. |
pause
|
Playback has been paused. |
play
|
Playback has begun. |
playing
|
Playback is ready to start after having been paused or delayed due to lack of data. |
progress
|
Fired periodically as the browser loads a resource. |
ratechange
|
The playback rate has changed. |
seeked
|
A seek operation completed. |
seeking
|
A seek operation began. |
stalled
|
The user agent is trying to fetch media data, but data is unexpectedly not forthcoming. |
suspend
|
Media data loading has been suspended. |
timeupdate
|
The time indicated by the
currentTime
attribute has been updated.
|
volumechange
|
The volume has changed. |
waiting
|
Playback has stopped because of a temporary lack of data |
Browsers don't all support the same video formats; you can provide multiple sources inside nested
<source>
elements, and the browser will then use the first one it understands.
<video controls>
<source src="myVideo.mp4" type="video/mp4">
<source src="myVideo.webm" type="video/webm">
<p>Your browser doesn't support HTML5 video. Here is
a <a href="myVideo.mp4">link to the video</a> instead.</p>
</video>
We offer a substantive and thorough guide to media file types 和 guide to the codecs supported for video . Also available is a guide to audio codecs that can be used with them .
Other usage notes:
controls
attribute, the video won't include the browser's default controls; you can create your own custom controls using JavaScript and the
HTMLMediaElement
API. See
Creating a cross-browser video player
了解更多细节。
HTMLMediaElement
s fire many different
events
. In addition to providing controllability, these events let you monitor the progress of both download and playback of the media, as well as the playback state and position.
object-position
property to adjust the positioning of the video within the element's frame, and the
object-fit
property to control how the video's size is adjusted to fit within the frame.
<track>
element and the
WebVTT
format. See
Adding captions and subtitles to HTML5 video
了解更多信息。
<video>
element. This can be useful if, for example, you need to perform audio with a
WebVTT
transcript, since the
<audio>
element doesn't allow captions using WebVTT.
<video>
with a non-existing element like
<notavideo>
.
A good general source of information on using HTML
<video>
是
Video and audio content
beginner's tutorial.
<video>
element is a replaced element — its
display
value is
inline
by default, but its default width and height in the viewport is defined by the video being embedded.
There are no special considerations for styling
<video>
; a common strategy is to give it a
display
value of
block
to make it easier to position, size, etc., and then provide styling and layout information as required.
Video player styling basics
provides some useful styling techniques.
You can detect when tracks are added to and removed from a
<video>
element using the
addtrack
and
removetrack
events. However, these events aren't sent directly to the
<video>
element itself. Instead, they're sent to the track list object within the
<video>
元素的
HTMLMediaElement
that corresponds to the type of track that was added to the element:
HTMLMediaElement.audioTracks
AudioTrackList
containing all of the media element's audio tracks. You can add a listener for
addtrack
to this object to be alerted when new audio tracks are added to the element.
HTMLMediaElement.videoTracks
addtrack
listener to this
VideoTrackList
object to be informed when video tracks are added to the element.
HTMLMediaElement.textTracks
addtrack
event listener to this
TextTrackList
to be notified when new text tracks are added to the element.
For example, to detect when audio tracks are added to or removed from a
<video>
element, you can use code like this:
var elem = document.querySelector("video");
elem.audioTrackList.onaddtrack = function(event) {
trackEditor.addTrack(event.track);
};
elem.audioTrackList.onremovetrack = function(event) {
trackEditor.removeTrack(event.track);
};
This code watches for audio tracks to be added to and removed from the element, and calls a hypothetical function on a track editor to register and remove the track from the editor's list of available tracks.
还可以使用
addEventListener()
to listen for the
addtrack
and
removetrack
事件。
This example plays a video when activated, providing the user with the browser's default video controls to control playback.
<!-- Simple video example -->
<!-- 'Big Buck Bunny' licensed under CC 3.0 by the Blender foundation. Hosted by archive.org -->
<!-- Poster from peach.blender.org -->
<video controls
src="https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4"
poster="https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217"
width="620">
Sorry, your browser doesn't support embedded videos,
but don't worry, you can <a href="https://archive.org/details/BigBuckBunny_124">download it</a>
and watch it with your favorite video player!
</video>
Until the video starts playing, the image provided in the
poster
attribute is displayed in its place. If the browser doesn't support video playback, the fallback text is displayed.
This example builds on the last one, offering three different sources for the media; this allows the video to be watched regardless of which video codecs are supported by the browser.
<!-- Using multiple sources as fallbacks for a video tag -->
<!-- 'Elephants Dream' by Orange Open Movie Project Studio, licensed under CC-3.0, hosted by archive.org -->
<!-- Poster hosted by Wikimedia -->
<video width="620" controls
poster="https://upload.wikimedia.org/wikipedia/commons/e/e8/Elephants_Dream_s5_both.jpg" >
<source
src="https://archive.org/download/ElephantsDream/ed_1024_512kb.mp4"
type="video/mp4">
<source
src="https://archive.org/download/ElephantsDream/ed_hd.ogv"
type="video/ogg">
<source
src="https://archive.org/download/ElephantsDream/ed_hd.avi"
type="video/avi">
Your browser doesn't support HTML5 video tag.
</video>
First WebM is tried. If that can't be played, then MP4 is tried. Finally, Ogg is tried. A fallback message is displayed if the video element isn't supported, but not if all sources fail.
Some media file types let you provide more specific information using the
codecs
parameter as part of the file's type string. A relatively simple example is
video/webm; codecs="vp8, vorbis"
, which says that the file is a
WebM
video using
VP8
for its video and
Vorbis
for audio.
If the MIME type for the video is not set correctly on the server, the video may not show or show a gray box containing an X (if JavaScript is enabled).
If you use Apache Web Server to serve Ogg Theora videos, you can fix this problem by adding the video file type extensions to "video/ogg" MIME type. The most common video file type extensions are ".ogm", ".ogv", or ".ogg". To do this, edit the "mime.types" file in "/etc/apache" or use the
"AddType"
configuration directive in
httpd.conf
.
AddType video/ogg .ogm AddType video/ogg .ogv AddType video/ogg .ogg
If you serve your videos as WebM, you can fix this problem for the Apache Web Server by adding the extension used by your video files (".webm" is the most common one) to the MIME type "video/webm" via the "mime.types" file in "/etc/apache" or via the "AddType" configuration directive in
httpd.conf
.
AddType video/webm .webm
Your web host may provide an easy interface to MIME type configuration changes for new technologies until a global update naturally occurs.
Videos should provide both captions and transcripts that accurately describe its content (see Adding captions and subtitles to HTML5 video for more information on how to implement these). Captions allow people who are experiencing hearing loss to understand a video's audio content as the video is being played, while transcripts allow people who need additional time to be able to review audio content at a pace and format that is comfortable for them.
It's worth noting that while you can caption audio-only media, you can only do so when playing audio in a
<video>
element, since the video region of the element is used to present the captions. This is one of the special scenarios in which it's useful to play audio in a video element.
If automatic captioning services are used, it is important to review the generated content to ensure it accurately represents the source video.
In addition to spoken dialog, subtitles and transcripts should also identify music and sound effects that communicate important information. This includes emotion and tone:
14 00:03:14 --> 00:03:18 [Dramatic rock music] 15 00:03:19 --> 00:03:21 [whispering] What's that off in the distance? 16 00:03:22 --> 00:03:24 It's… it's a… 16 00:03:25 --> 00:03:32 [Loud thumping] [Dishes clattering]
Captions should not obstruct the main subject of the video. They can be positioned using
the
align
VTT cue setting
.
| 内容类别 |
流内容
, phrasing content, embedded content. If it has a
controls
attribute: interactive content and palpable content.
|
|---|---|
| 准许内容 |
If the element has a
src
attribute: zero or more
<track>
elements, followed by transparent content that contains no media elements–that is no
<audio>
or
<video>
Else: zero or more
|
| Tag omission | None, both the starting and ending tag are mandatory. |
| Permitted parents | Any element that accepts embedded content. |
| Implicit ARIA role | 无对应角色 |
| Permitted ARIA roles |
application
|
| DOM 接口 |
HTMLVideoElement
|
| 规范 | Feedback |
|---|---|
|
HTML 实时标准
The definition of '<video>' in that specification. |
WHATWG HTML GitHub issues |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
视频
|
Chrome 完整支持 3 | Edge 完整支持 12 | Firefox 完整支持 3.5 | IE 完整支持 9 | Opera 完整支持 10.5 | Safari 完整支持 3.1 | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 4 | Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
autoplay
|
Chrome 完整支持 3 | Edge 完整支持 12 | Firefox 完整支持 3.5 | IE 完整支持 9 | Opera 完整支持 10.5 | Safari 完整支持 3.1 | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 4 | Opera Android 完整支持 Yes |
Safari iOS
完整支持
10
注意事项
|
Samsung Internet Android 完整支持 Yes |
buffered
|
Chrome ? | Edge 完整支持 ≤18 | Firefox 完整支持 4 | IE ? | Opera 完整支持 Yes | Safari ? | WebView Android ? | Chrome Android ? | Firefox Android 完整支持 4 | Opera Android 完整支持 Yes | Safari iOS ? | Samsung Internet Android ? |
controls
|
Chrome 完整支持 3 | Edge 完整支持 12 | Firefox 完整支持 3.5 | IE 完整支持 9 | Opera 完整支持 10.5 | Safari 完整支持 3.1 | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 4 | Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
crossorigin
|
Chrome ? | Edge 完整支持 ≤18 | Firefox 完整支持 12 | IE ? | Opera ? | Safari ? | WebView Android ? | Chrome Android ? | Firefox Android 完整支持 14 | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
height
|
Chrome 完整支持 3 | Edge 完整支持 12 | Firefox 完整支持 3.5 | IE 完整支持 9 | Opera 完整支持 10.5 | Safari 完整支持 3.1 | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 4 | Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
intrinsicsize
弃用
非标
|
Chrome
不支持
71 — 78
Disabled
|
Edge 不支持 No | Firefox 不支持 No | IE 不支持 No |
Opera
不支持
58 — 65
Disabled
|
Safari 不支持 No | WebView Android 不支持 No |
Chrome Android
不支持
71 — 78
Disabled
|
Firefox Android 不支持 No |
Opera Android
不支持
50 — 56
Disabled
|
Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
loop
|
Chrome 完整支持 3 | Edge 完整支持 12 | Firefox 完整支持 11 | IE 完整支持 9 | Opera 完整支持 10.5 | Safari 完整支持 3.1 | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 14 | Opera Android 完整支持 Yes | Safari iOS 完整支持 6 | Samsung Internet Android 完整支持 Yes |
muted
|
Chrome 完整支持 30 | Edge 完整支持 12 | Firefox 完整支持 11 | IE 完整支持 10 | Opera 完整支持 Yes | Safari 完整支持 5 | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 14 | Opera Android 完整支持 Yes | Safari iOS ? | Samsung Internet Android 完整支持 Yes |
played
|
Chrome ? | Edge 完整支持 ≤18 | Firefox 完整支持 15 | IE ? | Opera 完整支持 Yes | Safari ? | WebView Android ? | Chrome Android ? | Firefox Android 完整支持 15 | Opera Android 完整支持 Yes | Safari iOS ? | Samsung Internet Android ? |
poster
|
Chrome 完整支持 3 | Edge 完整支持 12 | Firefox 完整支持 3.6 | IE 完整支持 9 | Opera 完整支持 10.5 | Safari 完整支持 3.1 | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 4 | Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
preload
|
Chrome
完整支持
3
注意事项
|
Edge 完整支持 12 | Firefox 完整支持 4 | IE 完整支持 9 |
Opera
完整支持
Yes
注意事项
|
Safari 完整支持 3.1 |
WebView Android
完整支持
Yes
注意事项
|
Chrome Android
完整支持
Yes
注意事项
|
Firefox Android 完整支持 4 |
Opera Android
完整支持
Yes
注意事项
|
Safari iOS 完整支持 Yes |
Samsung Internet Android
完整支持
Yes
注意事项
|
src
|
Chrome 完整支持 3 | Edge 完整支持 12 | Firefox 完整支持 3.5 | IE 完整支持 9 | Opera 完整支持 10.5 | Safari 完整支持 3.1 | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 4 | Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
width
|
Chrome 完整支持 3 | Edge 完整支持 12 | Firefox 完整支持 3.5 | IE 完整支持 9 | Opera 完整支持 10.5 | Safari 完整支持 3.1 | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 4 | Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
完整支持
不支持
兼容性未知
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
见实现注意事项。
用户必须明确启用此特征。
object-position
and
object-fit
<audio>