Sub topic 16
Serving media: MIME types and range requests
This chapter assumes you can write audio and video markup, read HTTP headers in browser developer tools, and edit a basic web server configuration. Markup can be perfect and a video will still fail if the server delivers the file badly. Two things matter most: the Content-Type header and support for range requests.
MIME types
A MIME type is a label that tells the browser what kind of data a response contains. The server sends it in the Content-Type header. Browsers use it to choose a decoder, so a wrong value can make a valid file fail to play.
| Extension | Container | Correct `Content-Type` |
|---|---|---|
| `.mp4` | MP4 | `video/mp4` |
| `.webm` | WebM | `video/webm` |
| `.mp3` | MP3 | `audio/mpeg` |
| `.ogg` | Ogg | `audio/ogg` or `video/ogg` |
| `.vtt` | WebVTT | `text/vtt` |
A common error is a server that returns application/octet-stream or text/html for media files. The type attribute on a source element only helps the browser skip formats it cannot play. It does not replace a correct header from the server.
Here is how to declare these types in nginx:
types {
video/mp4 mp4;
video/webm webm;
audio/mpeg mp3;
text/vtt vtt;
}Range requests
A range request asks for only part of a file. The browser sends a Range header such as Range: bytes=1000000-1999999. A server that supports ranges replies with status 206 Partial Content and a Content-Range header. It also advertises support with Accept-Ranges: bytes.
Ranges matter for three reasons:
- Seeking. When the user drags to the middle of a video, the browser asks for those bytes instead of downloading everything before them.
- Metadata. Some files keep their index at the end, so the browser may need to jump there first.
- Bandwidth. With
preload="metadata", the browser can fetch a small piece and stop.
Checking your server
You can test range support from the command line:
curl -I -H "Range: bytes=0-99" https://example.com/clip.mp4A correct response begins with HTTP/1.1 206 Partial Content (or HTTP/2 206) and includes Content-Type: video/mp4, Content-Range and Accept-Ranges: bytes. If you see 200 OK instead, the server ignored the range and sent the whole file.
Common errors
- Seeking restarts the video or does nothing. The server or a proxy is not supporting ranges. Static file servers usually do. Custom application code that streams files often does not.
- Safari refuses to play a file that works elsewhere. Safari requires range support, so a server that always returns
200can fail there. - A CDN or compression layer changes the response. Do not gzip already compressed media. It gains little and can interfere with byte offsets.
- Captions do not load. Check that
.vttfiles are served astext/vtt.
Exercise
A video plays from the start but dragging the progress bar to the middle makes it jump back to the beginning. A request with a Range header returns 200 OK. What is wrong and how do you confirm the fix?
Show answer
The server ignores range requests and returns the whole file, so the browser cannot seek by fetching only part of it. Enable range support, or fix the proxy or application code that streams the file. Then run curl -I -H "Range: bytes=0-99" against the file and confirm the response is 206 Partial Content with Content-Range and Accept-Ranges: bytes headers.