1

Is there a way I can execute a script when my stream finishes creating a ts segment? Or when a user connects and starts/stops viewing a stream?

The NGINX RTMP EXEC Directives says: "Specifies external command with arguments to be executed on every stream published. When publishing stops the process is terminated. Full path to binary should be specified as the first argument. There are no assumptions about what this process should do. However this feature is useful with ffmpeg for stream transcoding.".

I try but cannot get any of the exec directives executing, nor are there any errors in /var/log/nginx/error_log. Can someone advise?

Note I use user apache, not user nginx on purpose for my goals further down the road - however apache is not running on the system, so the username shouldn't be an issue.

I stream from my windows laptop to my remote server using the following: ffmpeg.exe -re -i movie.mp4 -c:v libx264 -b:a 128k -crf 23 -strict experimental -f flv rtmp://x.x.x.x:1604/live/teststream This works - I get index.m3u8 and ts files filling up /efsr/5/nginx/hls/teststream on my server.

All my exec directives call the same script (for testing purposes only):

$ ls -ld /efsr/5/nginx/scripts/nginx_publish_done.sh
-rwxr-xr-x 1 apache apache 66 Jul 12 20:01 /efsr/5/nginx/scripts/nginx_publish_done.sh

And...

$ cat /efsr/5/nginx/scripts/nginx_publish_done.sh
#!/usr/bin/bash

/bin/date >> /tmp/nginx_publish_done.tmp

exit 0

My nginx.conf file has no errors:

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Even though there are no errors reported in the config file I have tried a variety of syntaxes calling, for example none of the following execute my script:

exec_publish bash -c "/efsr/5/nginx/scripts/nginx_publish_done.sh 1";
exec_publish bash -c /efsr/5/nginx/scripts/nginx_publish_done.sh 1;
exec_publish /efsr/5/nginx/scripts/nginx_publish_done.sh 1;

My nginx.conf follows:

worker_processes  auto;
user apache;
events {
    worker_connections  1024;
}

http {
    server {
        listen 8080;
        root /var/www/nginx/hls;

        location / {
            index index.html index.htm index.m3u8;
        }
    }
}

# RTMP configuration
rtmp {
        # Sets maximum number of connections for rtmp engine. Off by default.
        max_connections 200;

        # LIVE
        live on;

        # meta on|copy|off : on=receive reconstructed meta data : copy=copy meta data
        meta copy;

        # interleave on|off : on=audio and video data is transmitted on the same RTMP chunk stream.
        interleave on;

        # wait_key on|off : on=Makes video stream start with a key frame
        wait_key on;

        # idle_streams on|off : If disabled nginx-rtmp prevents subscribers from connecting to
        # idle/nonexistent live streams and disconnects all subscribers when stream publisher disconnects
        idle_streams on;

        # disable consuming the stream from nginx as rtmp
        #deny play all;

        # HLS
        # Turn on HLS
        hls on;

        # Sets HLS playlist type specified in X-PLAYLIST-TYPE playlist directive.
        # options: live|event
        hls_type live;

        # Sets HLS playlist and fragment directory.
        # If the directory does not exist it will be created.
        hls_path /efsr/5/nginx/hls;

        # Sets HLS fragment length. Defaults to 5 seconds.
        hls_fragment 10s;

        # Sets HLS playlist length. Defaults to 30 seconds. (can set to 10m for 10mins)
        hls_playlist_length 1440m;

        # Sets fragment naming mode: sequential|timestamp|system
        hls_fragment_naming system;

        # Toggles HLS nested mode. In this mode a subdirectory of hls_path is
        # created for each stream. Playlist and fragments are created in that
        # subdirectory. Default is off.
        hls_nested on;

        # HLS continuous mode. In this mode HLS sequence number is started
        # from where it stopped last time. Old fragments are kept.
        hls_continuous on;

    server {
        listen 1604; # Listen (standard RTMP port 1935)

        # Default is 4096. Prior value was 8192
        # maximum chunk size for stream multiplexing.
        # Bigger value = lower CPU
        chunk_size 4096;

        # buffer length. Default is 1000ms
        buflen 8000ms;

        application live {
                live on;
                wait_key on;
                wait_video on;
                publish_notify on;

                exec_publish bash -c "/efsr/5/nginx/scripts/nginx_publish_done.sh 1";
                exec_publish_done bash -c "/efsr/5/nginx/scripts/nginx_publish_done.sh 2";

                exec_play bash -c "/efsr/5/nginx/scripts/nginx_publish_done.sh 3";
                exec_play_done bash -c "/efsr/5/nginx/scripts/nginx_publish_done.sh  4";

                exec_pull bash -c "/efsr/5/nginx/scripts/nginx_publish_done.sh  5";
                exec_push bash -c "/efsr/5/nginx/scripts/nginx_publish_done.sh  6";
                exec_record_done bash -c "/efsr/5/nginx/scripts/nginx_publish_done.sh  7";
                exec_static bash -c "/efsr/5/nginx/scripts/nginx_publish_done.sh  8";

                # live view off|all|audio|video;
                record off;
                record_path /var/www/nginx/record;
                record_suffix -%F.flv;
                record_unique on;
                record_interval 15m;
                record_max_size 100M;
        }
    }
}

Version and config parameters when building nginx

# nginx -V nginx version: nginx/1.23.1 (CentOS) built by gcc 7.3.1 20180712 (Red Hat 7.3.1-15) (GCC) built with OpenSSL 1.1.1q  5 Jul 2022 TLS SNI support enabled configure arguments: --prefix=/etc/nginx
--sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=apache --group=apache --build=CentOS --add-module=/usr/local/src/nginx-rtmp-module --with-select_module --with-poll_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module --with-stream_ssl_preread_module --with-compat --with-pcre --with-pcre-jit --with-zlib=/usr/local/src/zlib-1.2.12 --with-openssl=/usr/local/src/openssl-1.1.1q --with-openssl-opt=no-nextprotoneg --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-debug
Ran Bo
  • 33
  • 8
  • I found issues going back 6+ years to 2015 with users having issues using the exec directives. Strangely, some have it working though without obvious reason. I recompiled. have tried reducing the size of my nginx.conf to see if there might be conflicts. I moved other directives further up in to rtmp{} and server{} and moved hls directives to application{}. None of these made improvement. I misspelt the exec directives and nginx objected which leads me to believe they are compiled in but not responding. I suspect its a feature that works on specific linux flavours, just not on my AWS AMI2. – Ran Bo Jul 14 '22 at 18:34

1 Answers1

0

It appears the exec functionality within nginx rtmp is specifically related to manipulating the stream and thus will not work using scripts. If I replace my commands with ffmpeg commands, nginx rtmp will execute ffmpeg.

Ran Bo
  • 33
  • 8