Nginx+FFmpeg+nginx-http-flv-module realizes RTSP/RTMP video streaming to FLV for web playback

Generally, monitoring video streams such as Hikvision and Dahua are pushed rtsp or rtmp streams, which cannot be played directly in the browser. At present, the mainstream method is to use WebRTC to realize web playback or Nginx+FFmpeg to convert flv stream to play in the browser through flv.js. This article will introduce how to use Nginx+FFmpeg+nginx-http-flv-module to realize RTSP/RTMP video streaming to FLV for web playback.

Environment: install Nginx on Ubuntu, install FFmpeg streaming on CentOS (product requirements, can be installed on the same system)

  1. Go to the official website to install Nginx and unzip http://nginx.org/
  2. Install nginx-http-flv-module module and decompress  https://github.com/winshining/nginx-http-flv-module/
  3. Install the dependencies required for Nginx compilation
    Ubuntu
    sudo apt-get update
    sudo apt-get install libpcre3 libpcre3-dev
    sudo apt-get install openssl libssl-dev

    CentOS

    //安装gcc
    
    yum install gcc-c++
    
    yum install -y openssl openssl-devel
    
    //安装pcre,zlib包
    
    yum install -y pcre pcre-devel
    
    yum install -y zlib zlib-devel
  4. Compile Nginx and add modules

    ./configure --add-module=/usr/local/nginxDir/nginx-http-flv-module           ###此处换成自己的解压路径
    make
    make install
  5. Modify the Nginx configuration file (note the placement of the configuration)

    vim /usr/local/nginx/conf/nginx.conf
    ## 添加rtmp模块,在http的外面
    rtmp {
        server {
            listen 1985; 
            application myapp {
                live on;
            }
        }
    }
    
    ## 以下都添加在http的server中
    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 /stat {
                #configuration of streaming & recording statistics
    
                rtmp_stat all;
                rtmp_stat_stylesheet stat.xsl;
            }
    location /stat.xsl {
                root /var/www/rtmp; #该路径修改为自己nginx-http-flv-module模块路径(stat.xsl的所在目录)
            }
  6. start nginx

    cd /usr/local/nginx/sbin
    ./nginx
  7. Visit localhost, if you can visit, nginx starts successfully

  8. Check whether the port is monitored, if there is a corresponding output, the rtmp module configuration is successful

    lsof -i:1985
  9. Install FFmpeg (windows can directly go to the official website to install the exe file to start, Linux can install yasm and ffmpeg to compile by itself, omitted here)

  10. Turn off the firewall and use FFmpeg to push the stream (the rtsp address is an available test address, and myapp corresponds to the name in the nginx configuration)

    ffmpeg -rtsp_transport tcp -i rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 -vcodec h264 -f flv -an rtmp://nginx服务器ip地址:1985/myapp/test
  11. Access the ip address/stat to view the status

  12. Use vlc to check whether the converted video stream is playing normally

Guess you like

Origin blog.csdn.net/qq_28174545/article/details/126060343