title: Understanding video files http range requests in Firefox Gecko
body: <p>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.</p>
  
  <p>I use the <a href="http://docs.python-requests.org/en/latest/" rel="nofollow noreferrer">requests library</a> 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.</p>
  
  <p>When I use a player in the browser, it <strong>somehow</strong> (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.</p>
  
  <p>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:</p>
  
  <p>A dummy using a header (aborted immediately):</p>
  
  <p><img src="https://i.stack.imgur.com/s8JmU.jpg" alt="First Request"></p>
  
  <p>Now the response contains <code>content-length</code> in bytes (and nothing more that seems informative other than <code>content-type: video/webm</code>).</p>
  
  <p>Now I assume it <strong>somehow</strong> knows:</p>
  
  <pre><code>total length (bytes) - 1000
  total_duration (seconds) - 60
  start_offset (seconds) - 30
  end_offset`(seconds) - 40
  </code></pre>
  
  <p>What I think it does next is something like:</p>
  
  <pre><code>((start-offset / total_duration) * total_length)-((end_offset / total_duration) * total_length) = ... = 160
  </code></pre>
  
  <p>And now it has the range needed to stream the specific part of the file:</p>
  
  <p><img src="https://i.stack.imgur.com/bdejk.png" alt="Second request"></p>
  
  <p>My question is: How an Firefox tell the <strong>total</strong> duration of the file only by knowing it's size in bytes? When I try and figure out the <code>bitrate</code> avconv gives N/A.</p>
  
  <p><code>avconv</code> output on the remote file:</p>
  
  <pre><code>  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)
  </code></pre>
  
  <p>After inspecting multiple requests made from the browser I am almost certain the video bit-rate is not constant.</p>
  
  <p>I base my understanding around <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Configuring_servers_for_Ogg_media" rel="nofollow noreferrer">this post</a>. It may be outdated / I may have overlooked something.</p>
  
  <p>I've also looked at StackOverflow posts like <a href="https://stackoverflow.com/questions/3303029/http-range-header">this one</a>, sadly they weren't in-depth enough for me to understand how to do this.</p>
  
  <ul>
  <li><p>If there is another way to do this, and I'm going the wrong way, please let me know.</p></li>
  <li><p>The website uses <a href="http://www.videojs.com/" rel="nofollow noreferrer">videojs</a> but firefox knows the video duration when accessing the video file directly, and not using the embedded player, so that's not the issue.</p></li>
  </ul>