FFmpeg HLS: Splitting a Video into .ts Segments and an m3u8 Playlist
With -f hls, FFmpeg can split one MP4 into short video files (.ts) of a few seconds each, plus a playlist (.m3u8) that lists them in order. This is one way to stream video on a website. In my tests, when I offered two quality levels, the browser (hls.js) read the .ts files one at a time in order and switched quality to match the connection speed.
When I split with -c copy, the split points were set by the keyframe positions. When I left -hls_list_size at its default, the playlist kept only the last 5 segments. With two quality levels, the order of entries in the master playlist changed how long playback took to start.
The test environment was Ubuntu 24.04.5, FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5), Node.js 22.23.4, the Chromium bundled with Playwright 1.63.0 (153.0.8010.12), hls.js 1.6.15, and a virtual machine with 4 CPU cores. The video was a 30-second clip made from FFmpeg's test video and audio (1280×720, 30fps, H.264 at about 2 Mbps, AAC at 128 kbps). Playback was served by a Node.js test server running on the same virtual machine. To simulate a slow connection, I limited the rate at which the server sent data. I did not test browsers other than Chromium (Safari, Firefox, mobile browsers, etc.), CDN delivery, live streaming, encryption (AES-128), or fMP4 segments.
- 1. Splitting into HLS at a single quality level
- 2. Segment length is determined by the keyframe interval
- 3. Without -hls_playlist_type, only the last 5 segments were listed
- 4. Two quality levels and a master playlist
- 5. Playing in the browser
- 6. On a slow connection, the order of the master playlist changed how long playback took to start
Splitting into HLS at a single quality level
The simplest approach is to split with -c copy, which copies the video and audio without re-encoding. Create the output folder first.
mkdir out1
ffmpeg -i src.mp4 -c copy -f hls -hls_time 6 -hls_playlist_type vod \
-hls_segment_filename "out1/seg_%03d.ts" out1/index.m3u8
The options mean the following. The explanations are based on the hls muxer section of the official FFmpeg documentation.
| Option | Meaning |
|---|---|
-f hls |
Write output in HLS format |
-hls_time 6 |
Target length of each .ts file in seconds. The default is 2 |
-hls_playlist_type vod |
Write the output as a fixed video that does not change later. The playlist lists all .ts files |
-hls_segment_filename |
File name pattern for the .ts files. %03d is replaced with a number starting at 000 |
In my test, the out1 folder contained index.m3u8 and 4 .ts files. However, the segments were not 6 seconds long.
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:8
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:8.333333,
seg_000.ts
#EXTINF:8.333333,
seg_001.ts
#EXTINF:8.333333,
seg_002.ts
#EXTINF:5.000000,
seg_003.ts
#EXT-X-ENDLIST
The #EXTINF value is the length of each segment. I specified 6 seconds, but each segment came out at 8.333333 seconds.
Segment length is determined by the keyframe interval
I used ffprobe to check the keyframe positions in the original MP4.
ffprobe -v error -select_streams v -skip_frame nokey \
-show_entries frame=pts_time -of csv=p=0 src.mp4
0.000000,
8.333333
16.666667
25.000000
The keyframes occurred only every 8.33 seconds (every 250 frames at 30fps). The documentation says that splitting happens at the first keyframe after the hls_time duration has passed. With -c copy, the keyframe positions cannot be changed, so the split happened at 8.33 seconds, the first keyframe after 6 seconds.
If you want to split exactly at 6 seconds, you need to re-encode and insert keyframes at a fixed interval. The following example inserts a keyframe every 60 frames (2 seconds) in a 30fps video. -g and -keyint_min set the maximum and minimum keyframe interval (the GOP length), and -sc_threshold sets the threshold for detecting scene changes. In the FFmpeg libx264 documentation, these correspond to x264's keyint, min-keyint, and scenecut settings.
mkdir out2
ffmpeg -i src.mp4 -c:v libx264 -preset veryfast -b:v 2M \
-g 60 -keyint_min 60 -sc_threshold 0 -c:a aac -b:a 128k \
-f hls -hls_time 6 -hls_playlist_type vod \
-hls_segment_filename "out2/seg_%03d.ts" out2/index.m3u8
This time, all 5 segments were #EXTINF:6.000000. When I used ffprobe to check the keyframes in the split .ts files, they were exactly 2 seconds apart. Because I made hls_time (6 seconds) a multiple of the keyframe interval (2 seconds), the split points lined up.
Without -hls_playlist_type, only the last 5 segments were listed
I split a 30-second video with -hls_time 2, without -hls_playlist_type vod.
mkdir out4
ffmpeg -i src.mp4 -c:v libx264 -preset veryfast -b:v 2M \
-g 60 -keyint_min 60 -sc_threshold 0 -c:a aac -b:a 128k \
-f hls -hls_time 2 -hls_segment_filename "out4/seg_%03d.ts" out4/index.m3u8
15 .ts files were created, but the playlist listed only the last 5.
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:2
#EXT-X-MEDIA-SEQUENCE:10
#EXTINF:2.000000,
seg_010.ts
(中略)
#EXTINF:2.000000,
seg_014.ts
#EXT-X-ENDLIST
According to the documentation, the default value of hls_list_size, which limits how many segments the playlist lists, is 5. This playlist does not include the .ts files for the first 20 seconds of the video.
When you deliver a pre-recorded video, add -hls_playlist_type vod or -hls_list_size 0. The documentation states that specifying vod sets hls_list_size to 0 (list all segments). When I split again with -hls_list_size 0, all 15 segments were listed in the playlist.
Two quality levels and a master playlist
To let playback switch quality based on connection speed, create a playlist for each quality level and place a master playlist that refers to them.

With FFmpeg, you can create this in a single run.
mkdir out6
ffmpeg -i src.mp4 \
-filter_complex "[0:v]split=2[v1][v2];[v2]scale=640:360[v2s]" \
-map "[v1]" -map "[v2s]" -map 0:a -map 0:a \
-c:v libx264 -preset veryfast -g 60 -keyint_min 60 -sc_threshold 0 \
-b:v:0 2500k -maxrate:v:0 2500k -bufsize:v:0 5000k \
-b:v:1 800k -maxrate:v:1 800k -bufsize:v:1 1600k \
-c:a aac -b:a 128k \
-f hls -hls_time 6 -hls_playlist_type vod \
-var_stream_map "v:0,a:0,name:720p v:1,a:1,name:360p" \
-master_pl_name master.m3u8 \
-hls_segment_filename "out6/%v/seg_%03d.ts" out6/%v/index.m3u8
split divides the video into two streams, and one of them is scaled down to 640×360. -var_stream_map specifies which video and audio streams are grouped into one quality level. Each space-separated group becomes one quality level, and the name given with name: is substituted for %v. As described in the documentation, putting %v in the folder name created a separate folder for each quality level.
The resulting master.m3u8 looks like this.
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-STREAM-INF:BANDWIDTH=2890800,RESOLUTION=1280x720,CODECS="avc1.64001f,mp4a.40.2"
720p/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=1020800,RESOLUTION=640x360,CODECS="avc1.64001e,mp4a.40.2"
360p/index.m3u8
BANDWIDTH was 1.1 times the sum of the specified video and audio bitrates. For 720p, that is (2,500k + 128k) × 1.1 = 2,890,800. The total size of the 30 seconds of .ts files was 10,430,428 bytes for 720p and 3,757,368 bytes for 360p.
Playing in the browser
I placed the playlist and .ts files on a web server and played them with hls.js. As described in the hls.js README, hls.js is a library that plays video using the browser's Media Source Extensions. I installed it with npm i hls.js@1.6.15 and placed hls.min.js in the same folder as the page. The page is shown below (the code for recording measurements is omitted).
<video id="v" muted playsinline controls></video>
<script src="hls.min.js"></script>
<script>
const v = document.getElementById('v');
const hls = new Hls();
hls.loadSource('master.m3u8');
hls.attachMedia(v);
</script>
The test server returned .m3u8 files with the Content-Type application/vnd.apple.mpegurl and .ts files with video/mp2t. I started playback by calling v.play() after the page loaded. Without limiting the transfer speed, the 720p .ts files were read in order, and playback started 0.26 seconds after the page was opened.
In Chromium 153, which I tested, canPlayType('application/vnd.apple.mpegurl') on <video> returned "maybe", and <video src="master.m3u8"> could play the video without hls.js.
On a slow connection, the order of the master playlist changed how long playback took to start
I limited the server's send rate to 160,000 bytes per second (1.28 Mbps) and played the video with hls.js. This is less than the BANDWIDTH of 720p (about 2.9 Mbps), but enough for 360p (about 1.0 Mbps).
With 720p listed first in the master.m3u8 above, hls.js first requested the first 720p segment (2,168,956 bytes). Receiving it took a long time, and playback started 13.82 seconds after the page was opened. Playback then switched to 360p, but at the 5.92-second point of the video, loading could not keep up and playback stalled. It took 3.5 seconds to resume.
The hls.js API documentation says that when startLevel is not specified, playback starts with the quality level listed first in the manifest (the master playlist). So I swapped the order in -var_stream_map and created a master playlist with 360p listed first.
-var_stream_map "v:1,a:1,name:360p v:0,a:0,name:720p" \
At the same speed, playback read the first 360p segment and started at 5.03 seconds. After that, it continued in 360p without stalling.

Without limiting the speed, playback started 0.23 seconds after the page was opened even with 360p first, and from the second segment onward, the 720p .ts files were read. The picture switched to 1280×720 at the 6-second point of the video. In this case, the first 6 seconds were 360p, but on a slow connection, this shortened the wait before playback started.
I also tried the same condition (720p first, 160,000 bytes per second) using the HLS playback built into Chromium without hls.js. In that case, it read 360p from the start, and playback started at 4.89 seconds. Which quality level playback starts with depends on the player.





