First of all, sorry if I'm using the wrong terminology. I've been playing around with nginx and I'm still a bit confused about RTMP and HLS and other acronyms.
I've managed to setup OBS to stream to an nginx server, which takes the RTMP stream and chops it into pieces for HLS. Here's the relevant part of the nginx configuration file.
rtmp {
server {
listen 1935;
chunk_size 4000;
ping 30s;
deny play all;
application live {
live on;
hls on;
hls_nested on; # Create a new folder for each stream
hls_path /mnt/hls/live;
hls_fragment 3s;
hls_fragment_naming timestamp;
hls_playlist_length 60s;
}
}
}
http {
server {
listen 81 ssl;
#creates the http-location for our full-resolution (desktop) HLS stream - "http://localhost:8080/live/test/index.m3u8"
location /live {
# Elided caching and CORS for brevity
alias /mnt/hls/live;
add_header Cache-Control no-cache;
index index.m3u8;
}
}
}
This works well, I can view the stream in VLC or on a website and it looks smooth. Now I wanted to add some logging: I'd like to write full hours (starting at xx:00:00 and ending at xx:59:59) to a file named log_yyyymmdd_hh.mp4, e.g. log_20200707_18.mp4 for the files of 7 July 2020, 18:00 - 19:00 hrs. So I've set up an hourly cron job with the following ffmpeg command:
ffmpeg -i https://stream.example.com:81/live/<streamkey> -preset veryfast -maxrate 2000k \
-bufsize 2000k -g 60 -t 3600 -y /var/video/log/$(date +\%Y\%m\%d_\%H00).mp4 >/dev/null 2>&1
At first this seemed to work well, so I left it running happily for about 24 hours. When I checked, most of my hourly files were small (~100MB) files of about 10 to 15 minutes long. It seems like any small delay in the stream will cause ffmpeg to stop writing to the file. I suspect such hiccups may for example be caused by an OBS plugin and I'll need to look into that, but I would prefer that ffmpeg will retry for some time before giving up. What arguments should I be passing to ffmpeg to make it not break when the stream is down for, say, up to a second every now and then?.
When I view back the HLS files there don't seem to be any noticeable gaps, so eventually all the data arrives. I went for the crontab solution with ffmpeg because when recording from nginx I could not figure out how to start recording at the start of the whole hour.