for this script we are going to assume you’ve started with a clean version of rocky 9 / alma linux 9.
first we will install ffmpeg, then nginx.
after that it’s a simple configuration additon to serve from nginx, and a systemd service to restream. let’s begin.
either login as root to your virtual machine, or use sudo:
-
sudo yum install epel-release
next, install the RPM fusion repo:
-
sudo yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-9.noarch.rpm
next, enable the CRB repo:
/usr/bin/crb enable
now you can install ffmpeg
-
yum -y install ffmpeg
confirm the ffmpeg install:
-
ffmpeg -version
Now that ffmpeg is installed and confirmed, we can install and configure nginx to serve the HLS restream, and setup a script to re-stream using ffmpeg.
install nginx, and nano, a text editor we’ll use to edit the configuration:
yum -y install nginx nano
nano /etc/nginx/nginx.conf
Paste the following into nginx.conf in the http section, right above the default port 80 configuration. On Windows and Putty, right click to insert, on mac terminal, command + v:
server {
listen 8080;
server_name _;
location /hls/ {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /var/www/html;
add_header Access-Control-Allow-Origin "*";
}
}
The file should end up looking like this.
Next, save nginx.conf. Now let’s make the directories needed and give ownership to nginx.
mkdir -p /var/www/html/
mkdir -p /var/www/html/hls/
chown nginx.nginx /var/www/html/
chown nginx.nginx /var/www/html/hls/
Now that we’ve created the directories needed, you can run:
systemctl enable nginx; systemctl start nginx
Now, check your web browser at http://[SERVERIP]:8080/
The web-server is now started and ready to serve HLS streams once fed them by ffmpeg.
Let’s get that done in the final step by setting up a systemd restream script. You’ll need the source .ts you want to restream.
Let’s create a systemd service by creating the file ffmpeg-stream.service.
[root@cdn-relay ~]# nano /etc/systemd/system/ffmpeg-stream.service
Here’s the full script:
[Unit]
Description=Persistent FFmpeg HLS Stream
After=network.target
[Service]
ExecStart=/usr/bin/ffmpeg -loglevel info -re \
-threads 4 \
-headers "Range: bytes=0-\r\n" \
-user_agent "VLC/3.0.17 LibVLC/3.0.17" \
-protocol_whitelist file,http,https,tcp,tls \
-rw_timeout 5000000 \
-err_detect ignore_err \
-fflags +genpts+discardcorrupt \
-i http://[URL_SOURCE] \
-reorder_queue_size 1024 \
-rtbufsize 512M \
-max_muxing_queue_size 1024 \
-c:v libx264 -preset veryfast -tune zerolatency \
-c:a aac \
-bufsize 512M \
-hls_time 3 \
-hls_list_size 50 \
-hls_flags delete_segments+independent_segments+append_list \
-start_at_zero \
-muxdelay 1 \
/var/www/html/hls/stream.m3u8
Restart=always
RestartSec=3
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
Replace [URL_SOURCE] with the .ts or file you copy from your playlist or provider, and save the file using CTRL + X. It’ll ask if you want to save, hit y and enter.
Next start the script and see if it works:
systemctl start ffmpeg-stream
If it is working, you can ls -al /var/www/html/hls, check for files, and you will see a .m3u8 manifest and .ts files for it. Open that in VLC or alike.
If so, you should be able to open this with VLC or alike to http://[YOUR_SERVER_IP:8080/hls/stream.m3u8.
If this works, keep the script persistent by running:
systemctl enable ffmpeg-stream
Should your server reboot, the script will start again automatically upon server restart.
The script will only use around 100MB to create a buffer to smooth out any network issues. Many variables have been modified to smooth out streams such as max_muxing_size, the reorder_queue_size, ignoring errors and more. Please keep in mind this script is optimized for ffmpeg 5.1.x, which is different than ffmpeg 4.x which required many extra flags to stabilize. I’ve used this script on all of the major providers without issue, but feel free to change the User-Agent slightly, or to match your native usual player.
The next update of this script will include a proxy, for providers who not only concentrate on the User-Agent, but ISP name and kick users manually.
Will also release the script I was using for a month on ffmpeg 4.x with no issues. Please keep in mind required many more flags to stabilize and reduce exiting, not required by ffmpeg 5.1.x which is more stable, why I’m using Rocky or Alma (RHEL) + ffmpeg 5.x for this script.
You can watch an hour or two and you’ll realize you aren’t getting an input bitrate, rather streaming HLS directly from the server ~4500kbps if 1920x1080p HD, and should your local ISP to your server source be stable, you’ll have zero lost frames.
Enjoy!
Leave a Reply
You must be logged in to post a comment.