StreamWriter writing wrong HLS format

Hi,
I have a use case where I need to write a stream of images as HLS stream to disk. The resulting m3u8 file seems wrong, as it always only contains the last 5 segments.

Here’s a simple toy example:

import torch
import torio
from torchaudio.io import StreamWriter

# Parameters
width, height = 800, 450
frame_rate = 10
num_frames = 300  # 30 seconds of video
manifest_file_path = "output.m3u8"
stream_format = "hls"
codec = "libx264"
pixel_format = "rgb24"
encoder_format = None
gop_size = 2
encoder_opts = {
    "g": str(int(gop_size * frame_rate)),
}
codec_config = torio.io.CodecConfig()
codec_config.gop_size = int(gop_size * frame_rate)


print(
    f"Writer: starting stream writer with format {stream_format}, width {width}, height {height}, "
    f"frame rate {frame_rate}, pixel format {pixel_format}, codec {codec}, "
    f"encoder format {encoder_format}, encoder options {encoder_opts}, codec config {codec_config}, "
    f"manifest file {manifest_file_path}"
)
writer = StreamWriter(manifest_file_path, format=stream_format)
writer.add_video_stream(
    width=width,
    height=height,
    frame_rate=frame_rate,
    format=pixel_format,
    encoder=codec,
    encoder_format=encoder_format,
    hw_accel=None,
    encoder_option=encoder_opts,
    codec_config=codec_config,
)

writer.open()

for _ in range(num_frames):
    frame = torch.randint(0, 256, (1, 3, height, width), dtype=torch.uint8)
    writer.write_video_chunk(0, frame, pts=None)

writer.close()
print(f"Video written to {manifest_file_path}")

This writes 14 .ts files with the video content but the output.m3u8 only contains the last 5 segments:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:2
#EXT-X-MEDIA-SEQUENCE:9
#EXTINF:1.900000,
output9.ts
#EXTINF:1.900000,
output10.ts
#EXTINF:1.900000,
output11.ts
#EXTINF:1.900000,
output12.ts
#EXTINF:1.300000,
output13.ts
#EXT-X-ENDLIST

So the player only sees the last 8 of 30 seconds in this example.

Am I using it wrongly or is this a bug?