nginx+ffmpeg+rtsp+rtmp/http-flv streaming media service construction [full version]

nginx + ffmpeg + rtsp + rtmp/http-flv streaming media service construction [full version]

Install nginx and its dependencies

  • gcc installation

    yum -y install gcc gcc-c + +
    
  • pcre installation

    #If you can’t pull it down, you can directly copy the link to the browser to download. I successfully downloaded it directly from the browser, and the same applies to all the links below.
    #If the link fails, you can download it online by yourself
    wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
    tar -zxvf pcre-8.35.tar.gz
    cd pcre-8.35
    ./configure
    make
    sudo make install
    
  • zlib installation

    wget http://zlib.net/zlib-1.2.13.tar.gz
    tar -zxf zlib-1.2.13.tar.gz
    cd zlib-1.2.13
    ./configure
    make
    sudo make install
    
  • openssl installation

    #1
    wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz
    tar -zxvf openssl-fips-2.0.10.tar.gz
    cd openssl-fips-2.0.10
    ./config & amp; & amp; make & amp; & amp; make install
    #2
    yum -y install openssl openssl-devel
    
  • Download nginx and nginx-http-flv-module

    nginx-http-flv-module

    #nginx-http-flv-module: https://github.com/winshining/nginx-http-flv-module
    git clone https://github.com/winshining/nginx-http-flv-module.git
    #I did not successfully clone the following one. I opened Science directly and downloaded it from the browser. Unzip and put it into the server
    #Website: https://github.com/winshining/nginx-http-flv-module.git
    

    nginx

    wget http://nginx.org/download/nginx-1.10.2.tar.gz
    tar zxvf nginx-1.10.2.tar.gz
    cd nginx-1.10.2
    # Specify the directory of nginx-http-flv-module when compiling
    ./configure --add-module=/usr/local/nginx-http-flv-module-master
    make
    sudo make install
    
  • Modify nginx.conf

    vi /usr/local/nginx/conf/nginx.conf
    cd /usr/local/nginx/sbin
    #Start nginx
    cd /usr/local/nginx/sbin
    ./nginx
    #Reload configuration file
    ./nginx -s reload
    

    The configuration file is as follows, you can copy it directly (you only need to modify server_name to your own server IP address):

    worker_processes 1; #should be 1 for Windows, for it doesn't support Unix domain socket
    #worker_processes auto; #from versions 1.3.8 and 1.2.5
    
    #worker_cpu_affinity 0001 0010 0100 1000; #only available on FreeBSD and Linux
    #worker_cpu_affinity auto; #from version 1.9.10
    
    error_log logs/error.log error;
    
    #if the module is compiled as a dynamic module and features relevant
    #to RTMP are needed, the command below MUST be specified and MUST be
    #located before events directive, otherwise the module won't be loaded
    #or will be loaded unsuccessfully when NGINX is started
    
    #load_module modules/ngx_http_flv_live_module.so;
    
    events {<!-- -->
        worker_connections 4096;
    }
    
    http {<!-- -->
        include mime.types;
        default_type application/octet-stream;
    
        keepalive_timeout 65;
    
        server {<!-- -->
            listen 80;
    
            location / {<!-- -->
                root /var/www;
                index index.html index.htm;
            }
    
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {<!-- -->
                roothtml;
            }
    
            location /live {<!-- -->
                flv_live on; #open flv live streaming (subscribe)
                chunked_transfer_encoding on; #open 'Transfer-Encoding: chunked' response
    
                add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
                add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
            }
    
            location /hls {<!-- -->
                types {<!-- -->
                    application/vnd.apple.mpegurl m3u8;
                    video/mp2t ts;
                }
    
                root /tmp;
                add_header 'Cache-Control' 'no-cache';
            }
    
            location /dash {<!-- -->
                root /tmp;
                add_header 'Cache-Control' 'no-cache';
            }
    
            location /stat {<!-- -->
                #configuration of streaming & recording statistics
    
                rtmp_stat all;
                rtmp_stat_stylesheet stat.xsl;
            }
    
            location /stat.xsl {<!-- -->
                root /var/www/rtmp; #specify in where stat.xsl located
            }
    
            #if JSON style stat needed, no need to specify
            #stat.xsl but a new directive rtmp_stat_format
    
            #location /stat {<!-- -->
            #rtmp_stat all;
            #rtmp_stat_format json;
            #}
    
            location /control {<!-- -->
                rtmp_control all; #configuration of control module of rtmp
            }
        }
    }
    
    rtmp_auto_push on;
    rtmp_auto_push_reconnect 1s;
    rtmp_socket_dir /tmp;
    
    rtmp {<!-- -->
        out_queue 4096;
        out_cork 8;
        max_streams 128;
        timeout 15s;
        drop_idle_publisher 15s;
    
        log_interval 5s; #interval used by log module to log in access.log, it is very useful for debug
        log_size 1m; #buffer size used by log module to log in access.log
    
        server {<!-- -->
            listen 1935;
            server_name www.test.*; #for suffix wildcard matching of virtual host name
    
            application myapp {<!-- -->
                live on;
                gop_cache on; #open GOP cache for reducing the watching time for the first picture of video
            }
    
            application hls {<!-- -->
                live on;
                hls on;
                hls_path /tmp/hls;
            }
    
            application dash {<!-- -->
                live on;
                dash on;
                dash_path /tmp/dash;
            }
        }
    
        server {<!-- -->
            listen 1935;
            server_name *.test.com; #for prefix wildcard matching of virtual host name
    
            application myapp {<!-- -->
                live on;
                gop_cache on; #open GOP cache for reducing the watching time for the first picture of video
            }
        }
    
        server {<!-- -->
            listen 1935;
            server_name www.test.com; #for completely matching of virtual host name
    
            application myapp {<!-- -->
                live on;
                gop_cache on; #open GOP cache for reducing the watching time for the first picture of video
            }
        }
    }
    

Installation of ffmpeg and its dependencies

  • Install yasm

    wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
    tar zxvf yasm-1.3.0.tar.gz
    cd yasm-1.3.0
    ./configure
    make
    sudo make install
    
  • Install nasm (x264 dependency)

    wget https://www.nasm.us/pub/nasm/releasebuilds/2.14/nasm-2.14.tar.gz --no-check-certificate
    tar -zxvf nasm-2.14.tar.gz
    cd nasm-2.14
    ./configure
    make & amp; & amp; make install
    
    #Add PATH to /etc/profile
    export PATH=$PATH:/usr/local/bin
    source /etc/profile
    
    
  • Install x264 (you need this to use h264 encoding)

    yum install git #If you don’t have git, install git first
    git clone https://code.videolan.org/videolan/x264.git
    cd x264
    #Run the following command. I remember there seems to be an error. If so, check it and it will be solved. After solving it, run the command again.
    ./configure --enable-shared
    ## --enable-shared parameter needs to be brought, otherwise only the x264 command will be installed without generating the relevant lib library file
    make & amp; & amp; make install
    
  • ffmpeg installation

    wget http://www.ffmpeg.org/releases/ffmpeg-5.0.1.tar.gz
    tar -zxvf ffmpeg-5.0.1.tar.gz
    cd ffmpeg-5.0.1
    ./configure --enable-shared --enable-swscale --enable-gpl --enable-nonfree --enable-pic --prefix=/usr/local/ffmpeg --enable-postproc --enable-pthreads -- enable-static --enable-libx264
    
    make & amp; & amp; make install
    
  • Add environment variables and edit /etc/ld.so.conf to add the directory of related library files

    vi /etc/profile
    # Add environment variables to PATH at the end:
    export PATH=$PATH:/usr/local/ffmpeg/bin
    # Save and exit
    source /etc/profile
    # Link the lib directory of ffmepg to the system library
    vi /etc/ld.so.conf
    #Append content after the document:
    #/usr/local/ffmpeg/lib/(ffmpeg compilation directory) /usr/local/lib/(library file generated by x264 compilation)
    #After adding, execute ldconfig to make the configuration take effect.
    ldconfig
    # Check the version and test whether it takes effect
    ffmpeg --version
    

At this point, both ffmpeg and nginx have been installed. Next, you only need to execute the ffmepg-related commands to start streaming for conversion. Then, cooperate with nginx to access the converted http, and you can achieve real-time preview on the page.

ffmpeg streaming command

The following is a clear and non-delayed parameter command that I personally tested. You only need to replace rtsp with your own and rtmp with your local machine.

ffmpeg -rtsp_transport tcp -probesize 32 -analyzeduration 5000000 -i rtsp://admin:[email protected]:554/h264/ch1/main/av_stream -an -c:v libx264 -tune zerolatency -preset ultrafast - b:v 1000k -r 30 -g 15 -c:a aac -f flv rtmp://127.0.0.1:1935/cr/one

Background process

nohup ffmpeg -rtsp_transport tcp -probesize 32 -analyzeduration 5000000 -i rtsp://admin:[email protected]:554/h264/ch1/main/av_stream -an -c:v libx264 -tune zerolatency -preset ultrafast -b:v 1000k -r 30 -g 15 -c:a aac -f flv rtmp://127.0.0.1:1935/cr/one & amp;

To push multiple ones, just change the last one of the rtmp link to something else. cr is the application name configured in nginx.conf.

  • Show all processes containing “ffmpeg”

    #Check which URLs are running
    ps aux | grep ffmpeg
    

If you have any questions, please feel free to contact me. Recently I have been working on video streaming and Hikvision-related things, and I would like to communicate more with you guys.

The above article of mine is based on the following article. New problems and installation content appear after practice, and the complete version is integrated.
Reference: https://blog.csdn.net/weixin_55775322/article/details/132338524?spm=1001.2014.3001.5506