Questions of HTML5 Videos
It’s months after last update. Recently, some questions of HTML5 videos apperars. This post is a simple list to introduce HTTP Range, HLS and MPEG-DASH.
HTTP Range header
The tag <video>
is magical, MP4(or WebM) videos can be played directly with a simple url to it as <video>
element’s src. But my first question is, what would happen if a video is very large? In fact, if you put a MP4 video on simple file server(localhost is ok, not file://) with a html file contains a video link to it, like this:
<!--
localhost/
|__index.html
|__video.mp4
-->
<video src="/video.mp4" type="video/mp4"></video>
It’s good on Chrome and Firefox, however it not works on Safari absolutely! The answer is simple, Safari needs your server to support Range
, see the following links to get more:
- https://stackoverflow.com/questions/27712778/video-plays-in-other-browsers-but-not-safari
- https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/CreatingVideoforSafarioniPhone/CreatingVideoforSafarioniPhone.html#//apple_ref/doc/uid/TP40006514-SW6
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range
Server tells browser size of the video, and browser take what it wants. Without this browsers must download all the video, when you clicking end of the seekbar.
HTTP Live Streaming (HLS)
One of many ordinary days, I found .m3u8
in a video site’s network requests. What’s that? It contains some plain text… like a list of video fragments’ urls? Wow, think I can download all of them and merge them into one video?
m3u8
is file format of HTTP Live Streaming:
P***Hub takes HLS, it loads master.m3u8 first, and the file contains urls of other m3u8 files, then video fragments are loaded.
PS: It’s easy to download a m3u8 video, in one sense, it’s a video file type, so you can use tools like ffmpeg to convert it into different types like MP4?
MediaSource
In fact, on desktop, HLS is supported by Edge and Safari only…some additional works are needed:
We know that, YouTube puts blob URL in <video>
, and that url is not able to be used to download the video. So, what blob is that? Obviously, it’s not arrayBuffer, because it doesn’t contain all data of the video, and blob is immutable.
It’s MediaSource.
- https://developer.mozilla.org/en-US/docs/Web/API/MediaSource
- https://developer.mozilla.org/en-US/docs/Web/API/Media_Source_Extensions_API
Ok, another question, what data are pushed into that media source? Actually, P***hub does same works like this, they put their HLS video data into it. And, YouTube takes another protocol to make video streaming.
Dynamic Adaptive Streaming over HTTP (DASH)
The answer is, Dynamic Adaptive Streaming over HTTP (DASH), which looks has more great features than HLS. For this, Bilibili has an article to Introduce that why they choose DASH: https://www.bilibili.com/read/cv855111/
There’s no browsers support DASH other than Microsft Edge, but caniuse.com said: