Does Auto choose the right video quality based on network bandwidth?

I have 3 types of videos:

  • 720x720,
  • 480x480 and
  • 320x320.

How to choose automatic video quality depending on the user's network bandwidth?

+7
javascript jquery html5 ruby-on-rails ruby-on-rails-4
source share
1 answer

Step 1

Upload a 1 MB image somewhere on your server. You need to know the exact size of the image in bytes and the image URL.

Step 2

var imageAddr = "IMAGE_URL_HERE"; imageAddr += "?n="+Math.random(); var startTime, endTime; var downloadSize = SIZE_OF_IMAGE_IN_BYTES; var download = new Image(); download.onload = function () { endTime = (new Date()).getTime(); showResults(); } startTime = (new Date()).getTime(); download.src = imageAddr; function showResults() { var duration = (endTime - startTime) / 1000; //Math.round() var bitsLoaded = downloadSize * 8; var speedBps = (bitsLoaded / duration).toFixed(2); var speedKbps = (speedBps / 1024).toFixed(2); var speedMbps = (speedKbps / 1024).toFixed(2); if(speedMbps<1){ //LOAD_SMALL_VIDEO } else if(speedMbps<2){ //LOAD_MEDIUM_VIDEO } else { //LOAD_LARGE_VIDEO } } 
+7
source share

All Articles