ffmpeg limit number of segment file

Bino Oetomo picture Bino Oetomo · Aug 26, 2017 · Viewed 8.6k times · Source

I'm learning in generating hls stream

Curently my ffmpeg command looks like

ffmpeg -analyzeduration 100000000 -probesize 1000000 \
-i https://nasa-i.akamaihd.net/hls/live/253565/NASA-NTV1-Public/master_500.m3u8 \
-c:v copy -g 90 -c:a aac \
-strict -2 \
-strftime 1  -use_localtime 1 -g 90 \
-hls_time 20 \
-hls_list_size 15 \
-hls_wrap 16 \
-hls_segment_filename mydir/%Y-%m-%d_%H-%M-%S.ts \
-f hls $4/index.m3u8 \
-hls_flags delete_segments \

I want to have only 15 ts files with 20 seconds each.

Problems are:

  1. It still generates more than 15 files, never deletes older files. While the index.m3u8 file content keeps updating and only has 15 ts listed.

  2. when I restart the command, index.m3u8 starts with only 1 file rather than updating it's list.

Answer

Gyan picture Gyan · Aug 26, 2017

A few things:

1) -hls_flags delete_segments appears after the output filename, which here is the playlist. So it's not applied. It would be applied to the next output, had you specified one. Shift it to before the m3u8 name.

2) Since you are copying the video, -g has no effect. It's also repeated. Remove both.

3) If you're using hls_wrap, set it to 15 and remove the hls_flag. If you want to use the flag, remove hls_wrap. Because of how delete_segment works, old files are removed "after a period of time equal to the duration of the segment plus the duration of the playlist.". So, you'll end up with at least 2 x list_size segments.