我有3种类型的视频:
如何根据用户的网络吞吐量自动选择视频质量?
答案 0 :(得分:7)
第1步
在服务器上的某处上传大小为1Mb的图像。您必须知道图像的确切大小(以字节为单位)和图像的URL。
第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
}
}