Table of contents
Open Table of contents
What is HLS?
Benefits of Using HLS
- Wide Compatibility: HLS is supported on iOS, Android, smart TVs, and most web browsers.
- Adaptive Bitrate Streaming: Ensures viewers get the best possible video quality based on their internet connection. Hls loads faster than other types of video formats.
- Scalability: HLS can be easily scaled to accommodate a large number of viewers.
Step by Step to Setup VoD with HLS
To have a video distribution system using hls, you need 2 things:
- A server so the client can get the ts file(Nginx)
- A server that help you upload video and spilt them to ts files
Prerequisites and usage
- Ubuntu server 20.04
Step 1: Install nginx and ffmpeg
-
Update the system:
sudo apt update sudo apt upgrade -y
-
Install the necessary dependencies:
sudo apt install build-essential
-
Install Nginx:
sudo apt install nginx
-
Install ffmpeg
sudo apt install ffmpeg
Step 2: Configure Nginx web server for hls files
-
Create file
your_domain.conf
in nginxetc/nginx/conf.d
with follow content:nano /etc/nginx/conf.d/your_domain.conf
server { listen 80; server_name your_domain; location /hls/ { alias /path/file/hls; types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Allow-Headers' 'Range' always; add_header 'Cache-Control' 'no-cache' always; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always; try_files $uri $uri/index.m3u8; } }
-
Restart nginx:
nano systemctl restart nginx
Step 3: Convert video to hls
-
Get video to test:
curl -o BigBuckBunny.mp4 http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4
-
Use ffmpeg to convert file mp4 to hls
ffmpeg -i BigBuckBunny.mp4 -codec: copy -hls_time 1 -hls_list_size 0 -f hls /path/file/hls/filename.m3u8
Wait done, you can see file ts in your path folder.
-
Create an HTML5 Video Player:
To play the HLS video, you need an HTML5 video player that supports HLS. One of the most popular options is the Video.js player with the videojs-contrib-hls plugin.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="robots" content="noindex"> <title>Live CDN</title> <link href="https://cdnjs.cloudflare.com/ajax/libs/video.js/8.3.0/video-js.min.css" rel="stylesheet"> </head> <body style="background:#000;color:#fff;"> <script src="https://cdnjs.cloudflare.com/ajax/libs/video.js/8.3.0/video.min.js"></script> <div class="video-container"> <video id="my-player" class="video-js" muted controls preload="auto" poster="" data-setup='{}' style="position:fixed;right:0;bottom:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:fill;" > <source id="video-source" type="application/x-mpegURL" src="https://your_domain/hls/4892603c8a/index.m3u8"> </source> <p class="vjs-no-js"> To view this video please enable JavaScript, and consider upgrading to a web browser that <a href="https://videojs.com/html5-video-support/" target="_blank"> supports HTML5 video </a> </p> </video> </div> <script> var player = videojs('my-player'); </script> </body> </html>
Step 4: Test Your Setup
Once everything is set up, test your HLS stream on different devices and network conditions to ensure it works seamlessly.