Nane
Nane

Reputation: 403

How to calculate how much bandwidth a video used with javascript?

I am trying to calculate how much bandwidth a user is consuming in a video with javascript, for example, the user will not see the entire video so I want to calculate how bandwidth for the viewing time but I have no idea how to start this

Thanks in advance.

Upvotes: 3

Views: 1353

Answers (1)

Mosè Raguzzini
Mosè Raguzzini

Reputation: 15821

the first thing that comes in my mind is to approximate the traffic (it depends by sampling rate of the video, that can be dynamic and not fixed).

Approximate network traffic

function loaded()
{
    var size = 4096; // video size
    var v = document.getElementById('videoID');
    var r = v.buffered;
    var total = v.duration;

    var start = r.start(0);
    var end = r.end(0);
    var percentDownloaded = (end/total)*100;
    var percentOfSize = (size/100)*percentDownloaded;

    console.log(percentOfSize);
}   

$('#videoID').bind('progress', function() 
{
    loaded();
}
);

Tracking real network traffic If you want to retrieve consistent data some browsers implements Resource Timing API. You can find more info about that API here and here

Upvotes: 5

Related Questions