This tutorial explains how to encode videos with HLS and encrypt them using ClearKeys and set up a ClearKey DRM Server to provide licenses for the video player.
This tutorial is based around the go-clearkey-server implementation.
- Shaka Player - Video player
- Shaka Packager - Used in HLS encoding and encrypting the videos
- Go - For building the ClearKey server
- ClearKey - DRM server
- Docker - For setting up the PostgreSQL database
- Docker Compose - For setting up the database container
- Psycopg Library - For querying the database from Python
-
Clone the Shaka Player from the official repository and compile it according to the documentation.
-
Download the binary file and rename it to packager and add it to PATH.
-
Install the Go language according to the documentation.
-
Clone the forked repository of ClearKey Server and compile it.
cd ~/go-clearkey-server go run .
-
Follow the documentation to install the Docker Engine.
-
Download the appropriate release of Docker Compose, rename it to docker-compose and move it to /usr/local/bin (for Linux users).
-
pip install psycopg2
After installing all the required packages let's start by creating the PostgreSQL database by running docker-compose in the ClearKey server directory:
cd ~/go-clearkey-server
sudo docker-compose up -d
Now our PSQL server is running and ready to recieve connections.
To run the ClearKey Server we change to the servers directory and run this command:
cd ~/go-clearkey-server
go run .
Now our DRM Server is running and connected to our database. We can check that by going to http://localhost:8080/license, we should start seeing the licenses if there's data in the database.
To HLS encode videos and encrypt them we can use encode.py that is provided in this repository. This script automates the encoding process and sends the encryption keys to the database. It's used as below:
python3 encode.py input_video.mp4 out_name
It should output the keyId and key values that it encoded the video with.
Now the ClearKey Server should be able to see the keyId and key pair and should provide the video player with the appropriate license.
This repository provides a website and its javascript code to test the video player. Make sure to provide the compiled shaka-player and the encoded video playlist in the same directory as the website.
As you can see here, the response is according to the W3C specifications.
Here the server is selecting the corresponding key for the requested key id
- A special thanks to the original owner of the ClearKey Server implementation!