<p>Switching between multiple HLS streams can be useful in two scenarios:</p>
<ul>
<li>The typical scenario is multi-bitrate HLS, where you switch between different bitrates and resolutions to accommodate different client bandwidths.</li>
<li>Backup and disaster recovery scenarios, where you switch between two similar HLS streams when one becomes unavailable.</li>
</ul>
<h2>Multiple Resolution HLS</h2>
<p>Multi-bitrate HLS cannot be implemented on the streaming server itself. As you mentioned, two HLS .ts files need to be switchable, with the same name and content, to avoid issues like stuttering or image repetition. Multi-bitrate encoding needs to be done at the encoder level, such as using FFmpeg to transcode an RTMP stream into multiple HLS streams with different bitrates:</p>
<pre class="lang-bash prettyprint-override"><code>ffmpeg -f flv -i &quot;rtmp://server/live/livestream&quot; \
  -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 \
  -c:v libx264 -crf 22 -c:a aac -ar 44100 \
  -filter:v:0 scale=w=480:h=360  -maxrate:v:0 600k -b:a:0 500k \
  -filter:v:1 scale=w=640:h=480  -maxrate:v:1 1500k -b:a:1 1000k \
  -filter:v:2 scale=w=1280:h=720 -maxrate:v:2 3000k -b:a:2 2000k \
  -var_stream_map &quot;v:0,a:0,name:360p v:1,a:1,name:480p v:2,a:2,name:720p&quot; \
  -preset fast -hls_list_size 10 -threads 0 -f hls \
  -hls_time 3 -hls_flags independent_segments \
  -master_pl_name &quot;livestream.m3u8&quot; \
  -y &quot;livestream-%v.m3u8&quot;
</code></pre>
<p>It will generate a master.m3u8, which contains 3 different m3u8 with different resulutions. You can refer to the detailed explanation in the provided <a href="https://stackoverflow.com/a/71985380/17679565">link</a>.</p>
<h2>Fault Tolerance HLS</h2>
<p>For backup and disaster recovery scenarios, the two HLS streams don't need to be identical. When one HLS stream becomes unavailable, users will likely refresh the encoder. At this point, switching to another HLS stream or providing a manual switch option can achieve fault tolerance.</p>
<p>Additionally, HLS distribution is usually done through a CDN, which inherently provides fault tolerance for an HLS stream. For self-built HLS CDN distribution scenarios, you'll need to handle this yourself. In this case, it's more suitable to have backup and switching at the RTMP level. You can push multiple RTMP streams, choose an available one to generate HLS, and use an FFmpeg script to achieve this:</p>
<pre class="lang-bash prettyprint-override"><code>while TRUE; do
  ffmpeg -re -i rtmp://ip/app/stream1 -c copy -f flv rtmp://ip/app/production;
  ffmpeg -re -i rtmp://ip/app/stream2 -c copy -f flv rtmp://ip/app/production;
  sleep 0.5;
done
</code></pre>
<p>It only generate one HLS stream, but switch between different source RTMP stream. For more information, refer to the detailed explanation in this <a href="https://stackoverflow.com/a/77363633/17679565">post</a>.</p>

CC BY-SA 4.0
2023-10-26T01:47:26.260Z
2023-10-26T01:47:26.260Z
Winlin
<p>In a live stream setup, I have 2 cameras and each one sends RTMP stream to one different application which is on my Nginx-RTMP server. On the browser I am using Videojs Hls plugin. </p>

<p>Now my question is how can I load these two streams one after another in the same hlsplayer player instance. For example, I want to load the first 15 seconds in my first source and the next 15 seconds from my second source in the same player. Since the .ts files are named incrementally and if I have 5sec chunks of .ts files in each .m3u8 file, I want to load the first 3 .ts files to be loaded from the first source(.m3u8 file) and the next 3 from another source(.m3u8 file). How can I achieve that? </p>

<p>Can I generate some kind of master M3U8 file which has the list of these other .M3U8 files or can I develop a plugin in videojs to load the appropriate ts files directly?</p>

<p>source1.m3u8<br>
1.ts<br>
2.ts<br>
3.ts</p>

<p>source2.m3u8<br>
1.ts<br>
2.ts<br>
3.ts</p>

<p>I want to load 1.ts from source1.m3u8 and 2.ts from source2.m3u8 but with no delay or lag.</p>

CC BY-SA 4.0
2019-11-24T00:40:54.780Z
Do you have a solution?
Hastalavistababyml
CC BY-SA 4.0
2022-08-23T12:32:08.423Z
Did you ever work this out?
Piotr Kula
<ffmpeg><streaming><rtmp><hls>
Switching between multiple m3u8 playlist
formatkaka
# Switching between multiple m3u8 playlist

(Asked by: username_0 on 2018-01-25)

In a live stream setup, I have 2 cameras and each one sends RTMP stream to one different application which is on my Nginx-RTMP server. On the browser I am using Videojs Hls plugin.

Now my question is how can I load these two streams one after another in the same hlsplayer player instance. For example, I want to load the first 15 seconds in my first source and the next 15 seconds from my second source in the same player. Since the .ts files are named incrementally and if I have 5sec chunks of .ts files in each .m3u8 file, I want to load the first 3 .ts files to be loaded from the first source(.m3u8 file) and the next 3 from another source(.m3u8 file). How can I achieve that?

Can I generate some kind of master M3U8 file which has the list of these other .M3U8 files or can I develop a plugin in videojs to load the appropriate ts files directly?

source1.m3u8  
1.ts  
2.ts  
3.ts

source2.m3u8  
1.ts  
2.ts  
3.ts

I want to load 1.ts from source1.m3u8 and 2.ts from source2.m3u8 but with no delay or lag.

**Tags:** ffmpeg, streaming, rtmp, hls

> **Comments:**
> **username_1**: Do you have a solution?  
> **username_2**: Did you ever work this out?  

---

## 1 answers

### Answer by username_3 on 2023-10-26. Score: 0

Switching between multiple HLS streams can be useful in two scenarios:

The typical scenario is multi-bitrate HLS, where you switch between different bitrates and resolutions to accommodate different client bandwidths.
Backup and disaster recovery scenarios, where you switch between two similar HLS streams when one becomes unavailable.

Multiple Resolution HLS
Multi-bitrate HLS cannot be implemented on the streaming server itself. As you mentioned, two HLS .ts files need to be switchable, with the same name and content, to avoid issues like stuttering or image repetition. Multi-bitrate encoding needs to be done at the encoder level, such as using FFmpeg to transcode an RTMP stream into multiple HLS streams with different bitrates:
ffmpeg -f flv -i "rtmp://server/live/livestream" \
  -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 \
  -c:v libx264 -crf 22 -c:a aac -ar 44100 \
  -filter:v:0 scale=w=480:h=360  -maxrate:v:0 600k -b:a:0 500k \
  -filter:v:1 scale=w=640:h=480  -maxrate:v:1 1500k -b:a:1 1000k \
  -filter:v:2 scale=w=1280:h=720 -maxrate:v:2 3000k -b:a:2 2000k \
  -var_stream_map "v:0,a:0,name:360p v:1,a:1,name:480p v:2,a:2,name:720p" \
  -preset fast -hls_list_size 10 -threads 0 -f hls \
  -hls_time 3 -hls_flags independent_segments \
  -master_pl_name "livestream.m3u8" \
  -y "livestream-%v.m3u8"

It will generate a master.m3u8, which contains 3 different m3u8 with different resulutions. You can refer to the detailed explanation in the provided link.
Fault Tolerance HLS
For backup and disaster recovery scenarios, the two HLS streams don't need to be identical. When one HLS stream becomes unavailable, users will likely refresh the encoder. At this point, switching to another HLS stream or providing a manual switch option can achieve fault tolerance.
Additionally, HLS distribution is usually done through a CDN, which inherently provides fault tolerance for an HLS stream. For self-built HLS CDN distribution scenarios, you'll need to handle this yourself. In this case, it's more suitable to have backup and switching at the RTMP level. You can push multiple RTMP streams, choose an available one to generate HLS, and use an FFmpeg script to achieve this:
while TRUE; do
  ffmpeg -re -i rtmp://ip/app/stream1 -c copy -f flv rtmp://ip/app/production;
  ffmpeg -re -i rtmp://ip/app/stream2 -c copy -f flv rtmp://ip/app/production;
  sleep 0.5;
done

It only generate one HLS stream, but switch between different source RTMP stream. For more information, refer to the detailed explanation in this post.


---

