HLS (HTTP Live Streaming) streaming is a widely used protocol for live streaming due to its compatibility with Content Delivery Networks (CDNs), making it suitable for streaming to large audiences. Traditionally, HLS streams have a latency of about 8-10 seconds, but with Low Latency HLS (LL-HLS), this can be reduced to around 3 seconds. Ant Media Server supports both HLS and LL-HLS.
However, content creators and broadcasters face challenges when it comes to merging individual streams with different resolutions and audio tracks in a single stream. Without such a solution, streamers must maintain separate streams for each resolution and audio option, which can be inefficient and difficult to manage. This fragmentation causes increased server load, higher bandwidth usage, and more complex workflows for both viewers and broadcasters.
In this blog post, we’ll introduce the new HLSMerger Plugin, which solves these challenges by:
1. Create an HLS stream with multiple resolutions – offering seamless quality switching for viewers with varying internet speeds.
2. Create an HLS stream with multiple audio tracks – enabling users to choose from different language or commentary tracks in a single stream.
Let us start by installing the plugin.
Build and Install the HLSMerger Plugin
Build the Plugin
1. Clone the Ant Media Plugins repository.
git clone https://github.com/ant-media/Plugins.git
2. Navigate to the HLSMergerPlugin
folder.
cd Plugins
cd HLSMergerPlugin
3. Run the following Maven command to build the plugin:
mvn clean install -Dmaven.javadoc.skip=true -Dmaven.test.skip=true -Dgpg.skip=true
4. The HLSMerger.jar file will be created in the target
folder.
Install the Plugin
1. Copy the HLSMerger.jar
file to the ant media server’s plugins directory.
sudo cp HLSMerger.jar /usr/local/antmedia/plugins/
2. Restart the Ant Media Server service.
sudo chown -R antmedia:antmedia /usr/local/antmedia
sudo service antmedia restart
Let’s proceed with the testing of both methods as mentioned above.
1. Create HLS Stream with Multiple Resolutions
Streaming with multiple resolutions is crucial for Adaptive Bitrate Streaming (ABR). ABR dynamically adjusts the video quality based on the viewer’s internet speed, providing a seamless viewing experience. However, ABR requires transcoding, which can be CPU-intensive. While Ant Media Server supports ABR, some users may prefer to handle this process on the client side.
The HLSMerger Plugin offers a solution by allowing users to create streams with different resolutions and then merge these streams into a single HLS master file. This eliminates the need for transcoding on the server side, reducing CPU usage.
- For example, to publish multiple RTMP streams with different resolutions and bit rates using FFmpeg, run the following commands:
ffmpeg -re -stream_loop -1 -i test.mp4 -bf 0 -s 1920x1080 -c:v libx264 -b:v 2000k -c:a copy -f flv rtmp://localhost/LiveApp/stream1
ffmpeg -re -stream_loop -1 -i test.mp4 -bf 0 -s 1280x720 -c:v libx264 -b:v 1000k -c:a copy -f flv rtmp://localhost/LiveApp/stream2
ffmpeg -re -stream_loop -1 -i test.mp4 -bf 0 -s 640x360 -c:v libx264 -b:v 500k -c:a copy -f flv rtmp://localhost/LiveApp/stream3
- Once these streams are published on the server, merge them using the following REST API call:
curl -X POST -H "Accept: Application/json" -H "Content-Type: application/json" http://localhost:5080/LiveApp/rest/v1/hls-merger/multi-resolution-stream/merged_stream -d '["stream1", "stream2", "stream3"]'
This creates a single master HLS file pointing to the different resolutions of the stream.
- Play the output stream
The output file will be created under the application’s (LiveApp) streams folder. The URL would be as follows:
https://domain:5443/LiveApp/streams/merged_stream.m3u8
The best part is that this merged stream can be played with the AMS player as well with the below URL format:
https://domain:5443/LiveApp/play.html?name=merged_stream&playOrder=hls
- To delete the merged stream, call the below API:
curl -X DELETE -H "Accept: Application/json" -H "Content-Type: application/json" http://localhost:5080/LiveApp/rest/v1/hls-merger/multi-resolution-stream/merged_stream
2. Create HLS Stream with Multiple Audio Tracks
Adding multiple audio tracks to a single video stream is ideal for multilingual streaming. The HLSMerger Plugin enables users to merge a video stream with multiple audio streams into a single HLS playlist (M3U8 file). HLS players can then present these audio options to viewers, allowing them to select their preferred language or audio track.
To achieve this, start by creating a video stream and separate audio streams using FFmpeg.
- Set the Segment Duration to 10 seconds in LiveApp Application Settings in Ant Media Server Dashdoard.
- Create Video stream
ffmpeg -re -stream_loop -1 -i test.mp4 -codec copy -f flv rtmp://localhost/LiveApp/videoStream
- Create Audio Streams
ffmpeg -re -i test.mp4 -codec copy -map 0:a:0 -f flv rtmp://localhost/LiveApp/audiostream1
ffmpeg -re -i test.mp4 -codec copy -map 0:a:1 -f flv rtmp://localhost/LiveApp/audiostream2
- Then, merge the audio streams with the video stream using the following REST API call:
curl -X POST -H "Accept: Application/json" -H "Content-Type: application/json" http://localhost:5080/LiveApp/rest/v1/hls-merger/videoStream/multi-audio-stream/merged_stream -d '["audiostream1", "audiostream2"]'
Once merged, the M3U8 file will use the audio group feature of HLS, enabling audio selection in compatible players.
- Play the output stream
The output file will be created under the application’s (LiveApp) streams folder. The URL would be as follows:
https://domain:5443/LiveApp/streams/merged_stream.m3u8
The best part is that this merged stream can be played with the AMS player as well with the below URL format:
https://domain:5443/LiveApp/play.html?name=merged_stream&playOrder=hls
- To delete this merged stream, call:
curl -X DELETE -H "Accept: Application/json" -H "Content-Type: application/json" http://localhost:5080/LiveApp/rest/v1/hls-merger/multi-audio-stream/merged_stream
Conclusion
In this blog post, we introduced the HLSMerger Plugin, a powerful tool for enhancing the HLS streaming experience. By enabling the creation of HLS streams with multiple resolutions and audio tracks, this plugin provides flexibility and efficiency for diverse streaming scenarios. Whether you aim to optimize server performance or offer multilingual content, the HLSMerger Plugin has you covered.
Give it a try and take your streaming experience to the next level!