nginx-rtmp-module icon indicating copy to clipboard operation
nginx-rtmp-module copied to clipboard

HLS files being deleted prematurely

Open renatoferreirarenatoferreira opened this issue 10 years ago • 14 comments

Hi,

I'm seeing an issue with the HLS files generation. With some versions of OBS, what I was not able to determine why, the chunk files are being deleted prematurely and sometimes even the m3u8 files are deleted for a few seconds that sometimes are enough for the player to receive 404 errors.

For working around this I added the following line in configuration file:

hls_cleanup off;

And, in order to still keep the destination directory clean, I also added the following cron job:

* * * * * /bin/find /path-to-hls_dir/* -mmin +1 -delete

I'm not sure if this is related, but when it happens the hls_fragment parameter is also not being respected. For instance, consider the following configuration:

hls_fragment 2s;
hls_playlist_length 6s;

With this parameters the m3u8 file always has 3 chunks (6 / 2 = 3), what seems to be good, but the chunks duration are 8 seconds. Take a look at this example:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:5
#EXT-X-TARGETDURATION:8
#EXTINF:8.334,
5.ts
#EXTINF:8.333,
6.ts
#EXTINF:8.333,
7.ts

I hope it helps.

Thanks,

Renato A. Ferreira

+1

mescalito avatar Mar 11 '16 02:03 mescalito

Figured out a workaround with OBS Studio on Mac. You'll need to set your keyframe interval manually (ex. 1s) in the output settings; the default is 0 (auto). That solved the issue for me.

kyle-shank avatar Mar 13 '16 07:03 kyle-shank

I met the same problem today. Pleeease tell me your solution, I'm nearly desperate now.

yypnathan avatar Jun 28 '16 10:06 yypnathan

In case someone may encounter the same problem... I find out the reason causes the problem today----------the particular source video missing audio part which may result in HLS slicing problem. When the audio part been added then the bug was fixed. Simple and easy.

yypnathan avatar Jul 05 '16 13:07 yypnathan

Hi, I encounter the problem today.

yypnathan: could you explain more about your solution.

van7hu avatar Apr 13 '17 04:04 van7hu

Hi, guys... Sorry, but I'm not activelly working on this project anymore. What I can say is that the "hls_cleanup off" parameter and the cron job was good enough to fix the 404 errors on live strraming.

Hi, thanks for kind reply, I have a question, am I mis-understanding the cron-job? /bin/find /path-to-hls_dir/* -mmin +1 -delete Is it suppose to clean ALL the files in the folder at specific interval (here is 1m?)?

van7hu avatar Apr 13 '17 11:04 van7hu

Hi van7hu: Lack of audio part of the live stream caused my problem. After the audio part been added to live stream then problem solved. I'm not sure if it is effective in your case.

yypnathan avatar Apr 13 '17 12:04 yypnathan

Hi yypnathan:

  1. What is 'audio part'?, could you explain it more clearly? I'm using OBS.
  2. I've reviewed my problem. The .ts file is generated, I tried also with hls_cleanup off; , and the problem persists.

For more clearly, my problem is as follow:

  1. I made and endpoint to live RTMP
  2. Convert RTMP to HLS with FFMPEG Result:
  3. The RTMP could not be watched, it just display nothing.
  4. I view on Server, the .ts file generated, and also the index.m3u8, I could watch for some .ts file with VLC player, and then the stream ends, although, the last .ts I watched is some index less than on server.

van7hu avatar Apr 13 '17 12:04 van7hu

van7hu: We were working on live stream server last year. Which means our procedure is as follows: camera & speaker ---> live stream ---> push to rtmp server ---> convert to hls --->client. Our problem was that the live stream didn't have audio track ----- not muted, NO AUDIO TRACK at all! Hence rtmp server seems has problem in converting this kind of video. Hope I've conveyed my idea clearly.

yypnathan avatar Apr 14 '17 14:04 yypnathan

I meet the same problem today and solve this when I change the source input keyframe. See https://stackoverflow.com/questions/38216051/hls-fragment-length-and-playlist-length-with-nginx-rtmp-module/44063680#44063680

Also: https://github.com/arut/nginx-rtmp-module/issues/775#issuecomment-195900365

In obs studio, see https://support.mediamotiononline.com/portal/kb/articles/how-to-change-the-keyframe-interval-in-obs-studio

dannyZhou avatar Jan 16 '19 08:01 dannyZhou

It looks like this plugin keeps files newer than 2 * the playlist length by default, but that might not be enough depending on keyframe settings and the overall latency.

I worked around the problem by patching the code to keep files newer than 5 * the playlist length: https://github.com/chenxiaolong/rtmp2hls/blob/db93ec26b45bfc53685dc27ce0f255c14591211d/0001-ngx_rtmp_hls_cleanup_dir-Only-delete-files-older-tha.patch

chenxiaolong avatar Dec 20 '19 08:12 chenxiaolong

Hello friends I'm not very familiar with the Nginx and I installed it on a Windows server Could you help me to define cron job? Where should I define that?

thank you

MehdiJafari84 avatar Jun 19 '22 07:06 MehdiJafari84

Two options to avoid premature deletion of segments:

  1. Reduce keyframe interval at the source. 2 sec is good for live streaming scenario (set hls_fragment=2 in such case).
  2. Increase the playlist length (hls_playlist_length)

If your source is OBS, then the default keyframe interval (0) is 10 secs. Set hls_playlist_length=10 (or higher) to avoid playback issues.

atyachin avatar Aug 11 '22 15:08 atyachin