Reputation: 31349
I need to download a part of a video file (vp8/webm, can add more data if needed) based on the start and end time inside the file.
I use the requests library to make the requests, and I have an Apache server set-up that allows me to fetch parts of a file using http-range requests.
When I use a player in the browser, it somehow (and that's the part I'm trying to figure out) manages to estimate the entire video duration. This is important since it enables the player to know the offset in bytes from which to make a range request.
So, if there are 1000 bytes on a 1-minute file and I want seconds 30-40, it will make at least two range requests:
A dummy using a header (aborted immediately):
Now the response contains content-length
in bytes (and nothing more that seems informative other than content-type: video/webm
).
Now I assume it somehow knows:
total length (bytes) - 1000
total_duration (seconds) - 60
start_offset (seconds) - 30
end_offset`(seconds) - 40
What I think it does next is something like:
((start-offset / total_duration) * total_length)-((end_offset / total_duration) * total_length) = ... = 160
And now it has the range needed to stream the specific part of the file:
My question is: How an Firefox tell the total duration of the file only by knowing it's size in bytes? When I try and figure out the bitrate
avconv gives N/A.
avconv
output on the remote file:
Duration: 03:50:15.26, start: 0.000000, bitrate: N/A
Stream #0.0: Video: vp8, yuv420p, 352x240, PAR 10:11 DAR 4:3, 15 fps, 15 tbr, 1k tbn (default)
Stream #0.1(eng): Audio: vorbis, 48000 Hz, stereo, fltp (default)
After inspecting multiple requests made from the browser I am almost certain the video bit-rate is not constant.
I base my understanding around this post. It may be outdated / I may have overlooked something.
I've also looked at StackOverflow posts like this one, sadly they weren't in-depth enough for me to understand how to do this.
If there is another way to do this, and I'm going the wrong way, please let me know.
The website uses videojs but firefox knows the video duration when accessing the video file directly, and not using the embedded player, so that's not the issue.
Upvotes: 0
Views: 1096
Reputation: 31160
You assumption is wrong. The first request starts to download the file starting at byte 0. The browser aborts the download once the file header is received. This header contains the information needed to determine the file duration, as well as 'time to sample' and 'sample to offset' data. Using this information the browser knows exactly what offset to request for a specific time.
Upvotes: 1