Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 video tag optimisation

I have Angular app , which is build and serve with node/Express server .

I use a video that user can watch

<video width="100%" height="344" controls controlsList="nodownload" preload="metadata" autoplay>
   <source src="/assets/video/digital.mp4" type="video/mp4">            
</video>

It works fine . but on slow internet it play very slowly. I want to know what can be done here so user wont get frustrated even on slow internet .

like image 721
Ashutosh Jha Avatar asked Feb 25 '26 12:02

Ashutosh Jha


1 Answers

Instead of preload="metadata" try preload:"auto"

auto: indicates that the whole video file could be downloaded, even if the user is not expected to use it.

But again as suggested the preload attribute is not always reliable..

There are 4 solutions mentioned in this blog & the most reliable being

Solution #4, ajax and bloburl (works)

var req = new XMLHttpRequest();
req.open('GET', 'video.mp4', true);
req.responseType = 'blob';

req.onload = function() {
   // Onload is triggered even on 404
   // so we need to check the status code
   if (this.status === 200) {
      var videoBlob = this.response;
      var vid = URL.createObjectURL(videoBlob); // IE10+
      // Video is now downloaded
      // and we can set it as source on the video element
      video.src = vid;
   }
}
req.onerror = function() {
   // Error
}

req.send();
like image 120
Rohit Kumar Avatar answered Feb 28 '26 01:02

Rohit Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!