HTML video loop re-downloads video file HTML video loop re-downloads video file google-chrome google-chrome

HTML video loop re-downloads video file


I don't know for sure what's the real issue you are facing.

It could be that Chrome has a max-size limit to what they'd cache, and if it the case, then not using Range-Requests wouldn't solve anything.

An other possible explanation is that caching media is not really a simple task.
Without seeing your file it's hard to tell for sure in which case you are, but you have to understand that to play a media, the browser doesn't need to fetch the whole file.
For instance, you can very well play a video file in an <audio> element, since the video stream won't be used, a browser could very well omit it completely and download only the audio stream. Not sure if any does, but they could. Most media formats do physically separate audio and video streams in the file and their byte positions are marked in the metadata.
They could certainly cache the Range-Requests they perform, but I think it's still quite rare they do.

But as tempting it might be to disable Range-Requests, you've to know that some browsers (Safari) will not play your media if your server doesn't allow Range-Requests.
So even then, it's probably not what you want.


The first thing you may want to try is to optimize your video for web usage. Instead of mp4, serve a webm file. These will generally take less space for the same quality and maybe you'll avoid the max-size limitation.

If the resulting file is still too big, then a dirty solution would be to use a MediaSource so that the file is kept in memory and you need to fetch it only once.

In the following example, the file will be fetched entirely only once, by chunks of 1MB, streamed by the MediaSource as it's being fetched and then only the data in memory will be used for looping plays:

document.getElementById('streamVid').onclick = e => (async () => {const url = 'https://upload.wikimedia.org/wikipedia/commons/transcoded/2/22/Volcano_Lava_Sample.webm/Volcano_Lava_Sample.webm.360p.webm';// you must know the mimeType of your video before hand.const type = 'video/webm; codecs="vp8, vorbis"';if( !MediaSource.isTypeSupported( type ) ) {  throw 'Unsupported';}const source = new MediaSource();source.addEventListener('sourceopen', sourceOpen);document.getElementById('out').src = URL.createObjectURL( source );// async generator Range-Fetcherasync function* fetchRanges( url, chunk_size = 1024 * 1024 ) {  let chunk = new ArrayBuffer(1);  let cursor = 0;  while( chunk.byteLength ) {    const resp = await fetch( url, {      method: "get",        headers: { "Range": "bytes=" + cursor + "-" + ( cursor += chunk_size ) }      }    )    chunk = resp.ok && await resp.arrayBuffer();    cursor++; // add one byte for next iteration, Ranges are inclusive    yield chunk;  }}// set up our MediaSourceasync function sourceOpen() {  const buffer = source.addSourceBuffer( type );  buffer.mode = "sequence";  // waiting forward to appendAsync...  const appendBuffer = ( chunk ) => {    return new Promise( resolve => {      buffer.addEventListener( 'update', resolve, { once: true } );      buffer.appendBuffer( chunk );    } );  }  // while our RangeFetcher is running  for await ( const chunk of fetchRanges(url) ) {    if( chunk ) { // append to our MediaSource      await appendBuffer( chunk );    }    else { // when done      source.endOfStream();    }  }}})().catch( console.error );
<button id="streamVid">stream video</button><video id="out" controls muted autoplay loop></video>