Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto select appropriate video quality depending upon network bandwidth?

I have 3 types of video:

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

How to select automatically quality of video depending on network throughput of the user?

like image 795
user3311412 Avatar asked Feb 17 '14 02:02

user3311412


1 Answers

Step 1

Upload an image around size of 1Mb somewhere on your server. You must know exact size of image in bytes and image's 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
    }

}
like image 64
XIMRX Avatar answered Oct 22 '22 01:10

XIMRX