In a previous post Prepare videos for adaptive bitrate streaming with ffmpeg, I wrote about how to prepare a video for adaptive bitrate streaming using ffmpeg and MP4Box.
This time, it will be about serving that video file on the internet and playing it on a web page.
Overview
Prerequisites:
- A transcoded video file (see previous post)
- Docker and docker-compose
- Python
To keep things simple, we will do everything locally on a single machine. The video file will be served by a nginx docker container, and the web page will be served by a python http server. In a production environment, the nginx server corresponds to a CDN, and the python server corresponds to a web application server.
Directory structure
|
|
Nginx configuration
|
|
Notice that there’s nothing special about this nginx configuration, nothing related to mpeg-dash such as plugins. This is because mpeg-dash is just some divided video files plus a manifest file, and the frontend player will take care of fetching the individual files and playing them. The video server just needs to serve the files.
Docker compose file
|
|
This will launch a nginx server on port 8084 serving the video files in the data
directory.
Sample webpage
To test the playout, we can just use a simple html webpage that uses dash.js. It is a javascript library that can play mpeg-dash videos in a web browser, and it is what YouTube uses under the hood.
Conveniently dash.js provides a sample html page, so we just have to modify it a little bit to point to our video file.
|
|
Spin up everything
|
|
Now go to http://localhost:8000 and the video should be there.
Conclusion
The playout part of mpeg-dash was easier than I thought. As a web developer, I just had to serve the video files and use a javascript library to play them. Feels like the most difficult part is transcoding the video files, and fine-tuning the settings/options for smooth playback in different network conditions.
One concern with this approach is that there is no protection for the video file, so anyone with the URL can view it from anywhere on the internet. Combining with technique like Securely hosting static files with Nginx, we can add a layer of protection. I have just recently implemented that with a SvelteKit backend, so that might be a good topic for another post.