简体   繁体   中英

How to determine HTTP range request start & end bytes (nodejs + mongodb)

I wonder if it is possible to determine the start & end bytes in HTTP range requests or let the browser somehow know where to start and let it use some user defined chunk size or so. I have a file in my database and it is split into multiple chunks, each chunk is 2 MB.

eg. 20 MB file => 10 chunks

When the browser starts downloading the file (video file), I have studied Chrome, it firsts checks the 'range=bytes 0-' byte range and if the server sucessfully responds with the 'right' bytes and 206 headers back, then it sends another request for the end bytes of the file eg 'range=bytes 1900000-' ,

It just checkes if your server responds well for the partial response

On the serverside I have coded my app so that it will send 2 MB partials if you ask it nicely to it :)

What I want the browser to do

range=bytes 0-'

range=bytes 2000000-4000000 bytes'

range=bytes 4000000-6000000 bytes'

But if you ask a partial which doesnt fit in a 2mb chunk it will give an error. Or it will just not play from the right position for a audio/video file.

range=bytes 2500000-4000000 bytes'

range=bytes 0-1000000 bytes'

this will give an error because I cannot start to send from a part of a chunk. Otherwise I have to slice my chunks and do some buffer operations. But I want to keep it clean. If this is possible please let me know.

I am assuming that you are streaming an mp4 file? Different parts (boxes) of the mp4 have different purposes, its not possible to jump to a random position in the file and start playing without out first identifying the location of each frame by preloading the index (moov). The moov can be at the beginning, or end of a file, so the browser MAY need the end of the of file first. It can determine this by starting from the beginning, and looking for the moov, if it is not at the start, there will be a pointer to the location of the next box. It can leap frog through the file until it finds the index. Once the moov header is downloaded, The browser will know the EXACT byte offset and size of every single frame in the video, and can jump around the file as you seek. This is all possible because the browser knows how to parse mp4 natively. TLDR. No, your solution will not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM