This commit is contained in:
devdatt 2025-12-05 18:55:02 +05:30
parent f6072c6c63
commit d95bf2a8c4
4 changed files with 361 additions and 122 deletions

View File

@ -1,26 +1,72 @@
user www-data; user www-data;
worker_processes auto; worker_processes auto;
worker_cpu_affinity auto;
pid /run/nginx.pid; pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
include /etc/nginx/modules-enabled/*.conf; include /etc/nginx/modules-enabled/*.conf;
events { events {
worker_connections 768; worker_connections 2048;
multi_accept on; multi_accept on;
}
rtmp {
server {
listen 1935;
chunk_size 4096;
application shree {
live on;
record off;
meta off;
wait_video on;
hls on;
hls_path /var/www/html/hls/shree;
hls_fragment 3;
hls_playlist_length 60;
dash on;
dash_path /var/www/html/dash/shree;
}
application shreeshree {
live on;
record off;
meta off;
wait_video on;
hls on;
hls_path /var/www/html/hls/shreeshree;
hls_fragment 3;
hls_playlist_length 60;
dash on;
dash_path /var/www/html/dash/shreeshree;
}
}
} }
http { http {
sendfile on; sendfile on;
tcp_nopush on; tcp_nopush on;
types_hash_max_size 2048; types_hash_max_size 2048;
server_tokens build;
include /etc/nginx/mime.types; include /etc/nginx/mime.types;
default_type application/octet-stream; default_type application/octet-stream;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
access_log /var/log/nginx/access.log; ssl_prefer_server_ciphers on;
gzip on;
include /etc/nginx/conf.d/*.conf; access_log /var/log/nginx/access.log;
include /etc/nginx/sites-enabled/*; error_log /var/log/nginx/error.log warn;
}
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

View File

@ -18,8 +18,10 @@ function update_service($which_service)
$srt_pass1 = ""; $srt_pass1 = "";
$srt_pass2 = ""; $srt_pass2 = "";
$srt_pass3 = ""; $srt_pass3 = "";
$rtmp_multiple[] = []; $rtmp_multiple0[] = [];
$rtmp_multiple1[] = [];
$srt_multiple[] = []; $srt_multiple[] = [];
$defaults = [ $defaults = [
'input' => 'url', 'input' => 'url',
'hdmi' => [ 'hdmi' => [
@ -78,36 +80,103 @@ function update_service($which_service)
$jsonFile = __DIR__ . '/output.json'; $jsonFile = __DIR__ . '/output.json';
$defaults = [ $defaults = [
'video' => [
'resolution' => '1920x1080',
'format' => 'h264_qsv',
'framerate' => '25',
'data_rate' => '3.3M',
'gop' => '12'
],
'audio' => [
'format' => 'aac',
'sample_rate' => '48000',
'bit_rate' => '96k',
'db_gain' => '0dB'
],
'service_display' => 'disable', 'service_display' => 'disable',
'output_display' => '1920x1080@60.00', 'service_rtmp0_multiple' => 'disable',
'output_display_audio' => '0,3', 'service_rtmp0_hls' => 'disable',
'service_rtmp_multiple' => 'disable', 'service_rtmp0_dash' => 'disable',
'service_rtmp_hls' => 'disable', 'service_rtmp1_multiple' => 'disable',
'service_rtmp_dash' => 'disable', 'service_rtmp1_hls' => 'disable',
'service_rtmp1_dash' => 'disable',
'service_udp0' => 'disable',
'service_udp1' => 'disable',
'service_udp2' => 'disable',
'service_srt_multiple' => 'disable', 'service_srt_multiple' => 'disable',
'service_udp' => 'disable',
'service_custom' => 'disable', 'service_custom' => 'disable',
'rtmp_multiple' => [],
'rtmp_multiple0' => [],
'rtmp_multiple1' => [],
'srt_multiple' => [], 'srt_multiple' => [],
'udp' => '', 'rtmp0' => [
'resolution' => '1920x1080',
'data_rate' => '6M',
'framerate' => '30',
'gop' => '30',
'extra' => '',
'audio_data_rate' => '128k',
'audio_db_gain' => '0dB',
'audio_sample_rate' => '48000'
],
'rtmp1' => [
'resolution' => '720x576',
'data_rate' => '1.5M',
'framerate' => '25',
'gop' => '25',
'extra' => '',
'audio_data_rate' => '96k',
'audio_db_gain' => '0dB',
'audio_sample_rate' => '48000'
],
'udp0' => [
'udp' => '',
'formate' => '',
'resolution' => '',
'data_rate' => '',
'framerate' => '',
'gop' => '',
'extra' => '',
'audio_formate' => '',
'audio_data_rate' => '',
'audio_db_gain' => '',
'audio_sample_rate' => ''
],
'udp1' => [
'udp' => '',
'formate' => '',
'resolution' => '',
'data_rate' => '',
'framerate' => '',
'gop' => '',
'extra' => '',
'audio_formate' => '',
'audio_data_rate' => '',
'audio_db_gain' => '',
'audio_sample_rate' => ''
],
'udp2' => [
'udp' => '',
'formate' => '',
'resolution' => '',
'data_rate' => '',
'framerate' => '',
'gop' => '',
'extra' => '',
'audio_formate' => '',
'audio_data_rate' => '',
'audio_db_gain' => '',
'audio_sample_rate' => ''
],
'srt0' => [
'formate' => '',
'resolution' => '',
'data_rate' => '',
'framerate' => '',
'gop' => '',
'extra' => '',
'audio_formate' => '',
'audio_data_rate' => '',
'audio_db_gain' => '',
'audio_sample_rate' => ''
],
'display' => '1920x1080@60.00',
'display_audio' => '0,3',
'custom_output' => '' 'custom_output' => ''
]; ];
for ($i = 1; $i <= 11; $i++) { for ($i = 1; $i <= 11; $i++) {
$defaults['rtmp_multiple'][$i] = ['url' => '', 'name' => '', 'enabled' => false]; $defaults['rtmp_multiple0'][$i] = ['url' => '', 'name' => '', 'enabled' => false];
$defaults['rtmp_multiple1'][$i] = ['url' => '', 'name' => '', 'enabled' => false];
$defaults['srt_multiple'][$i] = ['url' => '', 'name' => '', 'enabled' => false]; $defaults['srt_multiple'][$i] = ['url' => '', 'name' => '', 'enabled' => false];
} }
@ -121,19 +190,25 @@ function update_service($which_service)
} }
$service_display = $data['service_display']; $service_display = $data['service_display'];
$service_rtmp_multiple = $data['service_rtmp_multiple']; $service_rtmp0_multiple = $data['service_rtmp0_multiple'];
$service_rtmp_hls = $data['service_rtmp_hls']; $service_rtmp0_hls = $data['service_rtmp0_hls'];
$service_rtmp_dash = $data['service_rtmp_dash']; $service_rtmp0_dash = $data['service_rtmp0_dash'];
$service_srt_multiple = $data['service_srt_multiple']; $service_rtmp1_multiple = $data['service_rtmp1_multiple'];
$service_udp = $data['service_udp']; $service_rtmp1_hls = $data['service_rtmp1_hls'];
$service_rtmp1_dash = $data['service_rtmp1_dash'];
$service_udp0 = $data['service_udp0'];
$service_udp1 = $data['service_udp1'];
$service_udp2 = $data['service_udp2'];
$service_custom = $data['service_custom']; $service_custom = $data['service_custom'];
$rtmp_multiple = $data['rtmp_multiple']; $service_srt_multiple = $data['service_srt_multiple'];
$rtmp_multiple0 = $data['rtmp_multiple0'];
$rtmp_multiple1 = $data['rtmp_multiple1'];
$srt_multiple = $data['srt_multiple']; $srt_multiple = $data['srt_multiple'];
switch ($which_service) { switch ($which_service) {
case 'input': case 'input':
$input .= " -f mpegts udp://239.255.254.254:39000?localaddr=127.0.0.1"; $input .= " -c:v copy -c:a copy -f mpegts udp://@239.255.254.254:39000?localaddr=127.0.0.1";
$service = $input; $service = $input;
$file = "/var/www/encoder-main.sh"; $file = "/var/www/encoder-main.sh";
@ -142,39 +217,67 @@ function update_service($which_service)
} else { } else {
echo "Error writing file."; echo "Error writing file.";
} }
exec('sudo systemctl restart encoder-main'); if ($service_display === "enable") {
exec('sudo systemctl restart encoder-main');
}
break; break;
case 'display'; case 'display';
break; break;
case 'rtmp'; case 'rtmp0';
if ($service_rtmp_hls === "enable") { case 'rtmp1';
$hls = " if ($service_rtmp0_hls === "enable") {
$hls0 = "
hls on; hls on;
hls_path /var/www/html/hls/shree; hls_path /var/www/html/hls/shree;
hls_fragment 3; hls_fragment 3;
hls_playlist_length 60; hls_playlist_length 60;
"; ";
} else { } else {
$hls = " $hls0 = "
"; ";
} }
if ($service_rtmp_dash === "enable") { if ($service_rtmp0_dash === "enable") {
$dash = " $dash0 = "
dash on; dash on;
dash_path /var/www/html/dash/shree; dash_path /var/www/html/dash/shree;
"; ";
} else { } else {
$dash = " $dash0 = "
";
}
if ($service_rtmp1_hls === "enable") {
$hls1 = "
hls on;
hls_path /var/www/html/hls/shreeshree;
hls_fragment 3;
hls_playlist_length 60;
";
} else {
$hls1 = "
";
}
if ($service_rtmp1_dash === "enable") {
$dash1 = "
dash on;
dash_path /var/www/html/dash/shreeshree;
";
} else {
$dash1 = "
"; ";
} }
$rtmp_push0 = "";
$rtmp_push = "";
for ($i = 1; $i <= 11; $i++) { for ($i = 1; $i <= 11; $i++) {
if ($rtmp_multiple[$i]['enabled'] == 1) { if ($rtmp_multiple0[$i]['enabled'] == 1) {
$rtmp_push .= " $rtmp_push0 .= "
push " . $rtmp_multiple[$i]['url'] . ";"; push " . $rtmp_multiple0[$i]['url'] . ";";
}
}
$rtmp_push1 = "";
for ($i = 1; $i <= 11; $i++) {
if ($rtmp_multiple1[$i]['enabled'] == 1) {
$rtmp_push0 .= "
push " . $rtmp_multiple1[$i]['url'] . ";";
} }
} }
@ -188,14 +291,16 @@ events {
worker_connections 2048; worker_connections 2048;
multi_accept on; multi_accept on;
} }
";
if ($input_source === "rtmp") {
$nginx .= "
rtmp { rtmp {
server { server {
listen 1935; listen 1935;
chunk_size 4096; chunk_size 4096;
";
if ($input_source === "rtmp") {
$nginx .= "
application " . $input_rtmp_mount . " { application " . $input_rtmp_mount . " {
live on; live on;
record off; record off;
@ -203,37 +308,31 @@ rtmp {
wait_video on; wait_video on;
} }
application shree { ";
live on; }
record off; $nginx .= "
meta off;
wait_video on;
allow publish 127.0.0.1;
deny publish all;
" . $rtmp_push . "
}
}
}
";
} else {
$nginx .= "
rtmp {
server {
listen 1935;
chunk_size 4096;
application shree { application shree {
live on; live on;
record off; record off;
meta off; meta off;
wait_video on; wait_video on;
" . $rtmp_push . " " . $hls0 . "
" . $hls . " " . $dash0 . "
" . $dash . " " . $rtmp_push0 . "
}
application shreeshree {
live on;
record off;
meta off;
wait_video on;
" . $hls1 . "
" . $dash1 . "
" . $rtmp_push1 . "
} }
} }
}"; }";
}
$nginx .= " $nginx .= "
http { http {
@ -258,26 +357,61 @@ http {
"; ";
$file = "/var/www/nginx.conf"; $file = "/var/www/nginx.conf";
file_put_contents($file, $nginx); file_put_contents($file, $nginx);
exec('sudo cp /var/www/nginx.conf /etc/nginx/nginx.conf');
if ($service_rtmp_multiple === "enable") { exec("sudo nginx -t 2>&1", $output, $status);
$rtmp = 'ffmpeg -hide_banner -fflags nobuffer -i "udp://@239.255.254.254:39000?fifo_size=5000000&overrun_nonfatal=1" -c:v copy -c:a aac -f flv rtmp://127.0.0.1:1935/shree/bhattji'; if ($status === 0) {
$file = "/var/www/encoder-rtmp.sh"; exec("sudo systemctl restart nginx 2>&1", $o, $s);
file_put_contents($file, $rtmp);
exec('sudo cp /var/www/nginx.conf /etc/nginx/nginx.conf');
exec("sudo nginx -t 2>&1", $output, $status);
if ($status === 0) {
exec("sudo systemctl restart nginx 2>&1", $o, $s);
exec('sudo systemctl enable encoder-rtmp');
exec('sudo systemctl restart encoder-rtmp');
} else {
exec('sudo cp /var/www/nginx.conf /etc/nginx/');
exec("sudo systemctl restart nginx");
exec('sudo systemctl stop encoder-rtmp');
exec('sudo systemctl disable encoder-rtmp');
}
} else { } else {
exec('sudo systemctl stop encoder-rtmp'); exec('sudo cp /var/www/default_nginx.conf.conf /etc/nginx/nginx.conf');
exec('sudo systemctl disable encoder-rtmp'); exec("sudo systemctl restart nginx");
}
if ($service_rtmp0_multiple === "enable") {
$rtmp = 'ffmpeg -hide_banner -fflags nobuffer -i "udp://@239.255.254.254:39000?fifo_size=5000000&overrun_nonfatal=1" '
.' -c:v h264 '
.' -vf "scale="'.str_replace("x",":",$data['rtmp0']['resolution'])
.'" -b:v '.$data['rtmp0']['data_rate']
.' -maxrate '.$data['rtmp0']['data_rate']
.' -bufsize '.$data['rtmp0']['data_rate']
.' -r '.$data['rtmp0']['framerate']
.' -g '.$data['rtmp0']['gop']
.' -c:a aac -b:a '.$data['rtmp0']['audio_data_rate']
.' '.$data['rtmp0']['audio_db_gain']
.' -ar '.$data['rtmp0']['audio_sample_rate']
.' '.$data['rtmp0']['extra']
.' -f flv rtmp://127.0.0.1/shree/bhattji';
$file = "/var/www/encoder-rtmp0.sh";
file_put_contents($file, $rtmp);
exec('sudo systemctl enable encoder-rtmp0');
exec('sudo systemctl restart encoder-rtmp0');
} else {
exec('sudo systemctl stop encoder-rtmp0');
exec('sudo systemctl disable encoder-rtmp0');
}
if ($service_rtmp0_multiple === "enable") {
$rtmp = 'ffmpeg -hide_banner -fflags nobuffer -i "udp://@239.255.254.254:39000?fifo_size=5000000&overrun_nonfatal=1" '
.' -c:v h264_qsv '
.' -vf "scale="'.str_replace("x",":",$data['rtmp1']['resolution'])
.'" -b:v '.$data['rtmp1']['data_rate']
.' -maxrate '.$data['rtmp1']['data_rate']
.' -bufsize '.$data['rtmp1']['data_rate']
.' -r '.$data['rtmp1']['framerate']
.' -g '.$data['rtmp1']['gop']
.' -c:a aac -b:a '.$data['rtmp1']['audio_data_rate']
.' '.$data['rtmp1']['audio_db_gain']
.' -ar '.$data['rtmp1']['audio_sample_rate']
.' '.$data['rtmp1']['extra']
.' -f flv rtmp://127.0.0.1/shreeshree/bhattji';
$file = "/var/www/encoder-rtmp1.sh";
file_put_contents($file, $rtmp);
exec('sudo systemctl enable encoder-rtmp1');
exec('sudo systemctl restart encoder-rtmp1');
} else {
exec('sudo systemctl stop encoder-rtmp1');
exec('sudo systemctl disable encoder-rtmp1');
} }
break; break;
case "srt"; case "srt";
@ -326,7 +460,7 @@ srt {
} }
} }
"; ";
$service = 'ffmpeg -fflags nobuffer -i udp://@239.255.254.254:39000 -c copy -f mpeg srt://127.0.0.1/'.$srt_pass1.'/'.$srt_pass2.'/ji'; $service = 'ffmpeg -fflags nobuffer -i udp://@239.255.254.254:39000 -c copy -f mpeg srt://127.0.0.1/' . $srt_pass1 . '/' . $srt_pass2 . '/ji';
$file = "/var/www/encoder-srt.sh"; $file = "/var/www/encoder-srt.sh";
file_put_contents($file, $service); file_put_contents($file, $service);
@ -336,6 +470,7 @@ srt {
exec('sudo systemctl restart srt'); exec('sudo systemctl restart srt');
exec('sudo systemctl enable encoder-srt'); exec('sudo systemctl enable encoder-srt');
exec('sudo systemctl restart encoder-srt'); exec('sudo systemctl restart encoder-srt');
} else { } else {
exec('sudo systemctl disable srt'); exec('sudo systemctl disable srt');
exec('sudo systemctl stop srt'); exec('sudo systemctl stop srt');

View File

@ -89,15 +89,29 @@ Environment=PATH=/usr/bin:/usr/local/bin
WantedBy=multi-user.target WantedBy=multi-user.target
EOL EOL
cat > /etc/systemd/system/encoder-rtmp.service<< 'EOL' cat > /etc/systemd/system/encoder-rtmp0.service<< 'EOL'
[Unit] [Unit]
Description= RTMP Encoder by ShreeBhattJi Description= RTMP Encoder by ShreeBhattJi
Requires=nginx.service
After=nginx.service
[Service] [Service]
ExecStart=/bin/bash /var/www/encoder-rtmp.sh ExecStart=/bin/bash /var/www/encoder-rtmp0.sh
WorkingDirectory=/var/www/
Restart=always
User=root
Environment=PATH=/usr/bin:/usr/local/bin
[Install]
WantedBy=multi-user.target
EOL
cat > /etc/systemd/system/encoder-rtmp1.service<< 'EOL'
[Unit]
Description= RTMP Encoder by ShreeBhattJi
[Service]
ExecStart=/bin/bash /var/www/encoder-rtmp1.sh
WorkingDirectory=/var/www/ WorkingDirectory=/var/www/
Restart=always Restart=always
User=root User=root
@ -110,8 +124,6 @@ EOL
cat > /etc/systemd/system/encoder-srt.service<< 'EOL' cat > /etc/systemd/system/encoder-srt.service<< 'EOL'
[Unit] [Unit]
Description= SRT Encoder by ShreeBhattJi Description= SRT Encoder by ShreeBhattJi
Requires=nginx.service
After=nginx.service
[Service] [Service]
@ -125,15 +137,45 @@ Environment=PATH=/usr/bin:/usr/local/bin
WantedBy=multi-user.target WantedBy=multi-user.target
EOL EOL
cat > /etc/systemd/system/encoder-udp.service<< 'EOL' cat > /etc/systemd/system/encoder-udp0.service<< 'EOL'
[Unit] [Unit]
Description= UDP Encoder by ShreeBhattJi Description= UDP Encoder by ShreeBhattJi
Requires=nginx.service
After=nginx.service
[Service] [Service]
ExecStart=/bin/bash /var/www/encoder-udp.sh ExecStart=/bin/bash /var/www/encoder-udp0.sh
WorkingDirectory=/var/www/
Restart=always
User=root
Environment=PATH=/usr/bin:/usr/local/bin
[Install]
WantedBy=multi-user.target
EOL
cat > /etc/systemd/system/encoder-udp1.service<< 'EOL'
[Unit]
Description= UDP Encoder by ShreeBhattJi
[Service]
ExecStart=/bin/bash /var/www/encoder-udp1.sh
WorkingDirectory=/var/www/
Restart=always
User=root
Environment=PATH=/usr/bin:/usr/local/bin
[Install]
WantedBy=multi-user.target
EOL
cat > /etc/systemd/system/encoder-udp2.service<< 'EOL'
[Unit]
Description= UDP Encoder by ShreeBhattJi
[Service]
ExecStart=/bin/bash /var/www/encoder-udp2.sh
WorkingDirectory=/var/www/ WorkingDirectory=/var/www/
Restart=always Restart=always
User=root User=root
@ -316,6 +358,8 @@ EOL
rm /var/www/html/index.nginx-debian.html; rm /var/www/html/index.nginx-debian.html;
sudo mkdir -p /var/www/html/hls/shree; sudo mkdir -p /var/www/html/hls/shree;
sudo mkdir -p /var/www/html/dash/shree; sudo mkdir -p /var/www/html/dash/shree;
sudo mkdir -p /var/www/html/hls/shreeshree;
sudo mkdir -p /var/www/html/dash/shreeshree;
sudo mkdir -p /var/www/encoder; sudo mkdir -p /var/www/encoder;
cp -r html/* /var/www/html/ cp -r html/* /var/www/html/
sudo cp -r encoder/* /var/www/encoder/ sudo cp -r encoder/* /var/www/encoder/
@ -325,18 +369,15 @@ sudo systemctl enable apache2
sudo systemctl restart apache2 sudo systemctl restart apache2
sudo chmod +x /usr/local/bin/nginx_system_monitor_sampler.py sudo chmod +x /usr/local/bin/nginx_system_monitor_sampler.py
sudo systemctl daemon-reload sudo systemctl daemon-reload
sudo systemctl enable --now system-monitor.service sudo systemctl enable --now system-monitor.service
sudo systemctl status system-monitor.service --no-pager sudo systemctl status system-monitor.service --no-pager
sudo systemctl enable --now encoder-main.service
sudo systemctl status encoder-main.service --no-pager
sudo systemctl enable --now srt.service
sudo systemctl status srt.service --no-pager
sudo systemctl enable --now nginx.service sudo systemctl enable --now nginx.service
sudo systemctl status nginx.service --no-pager sudo systemctl status nginx.service --no-pager
sudo chmod 777 -R /var/www sudo chmod 777 -R /var/www
sudo chown -R www-data:www-data /var/www sudo chown -R www-data:www-data /var/www
sudo ufw allow proto udp to 224.0.0.0/4 sudo ufw allow proto udp to 224.0.0.0/4
sudo ufw route allow proto udp to 224.0.0.0/4 sudo ufw route allow proto udp to 224.0.0.0/4
sudo ufw deny out to 239.255.254.254 port 39000 proto udp sudo ufw deny out to 239.255.254.254 port 39000 proto u

View File

@ -31,6 +31,23 @@ rtmp {
dash_path /var/www/html/dash/shree; dash_path /var/www/html/dash/shree;
} }
application shreeshree {
live on;
record off;
meta off;
wait_video on;
hls on;
hls_path /var/www/html/hls/shreeshree;
hls_fragment 3;
hls_playlist_length 60;
dash on;
dash_path /var/www/html/dash/shreeshree;
}
} }
} }