在Centos 7.x中使用ffmpeg搭建RTSP流處理環境


Centos7.x 搭建RTSP流處理環境

服務器環境
  • 服務器 centos 7 目前版本CentOS Linux release 7.6.1810下載地址
安裝nginx & nginx-rtmp-module
  1. 官網下載nginx 目前版本nginx-1.17.0下載地址

  2. 下載依賴pcre 目前版本pcre-8.38下載地址 使用pcre2 nginx會編譯失敗

  3. 下載依賴zlib 目前版本zlib-1.2.11下載地址

  4. 下載openssl 目前版本openssl-1.0.2s下載地址

  5. 下載nginx-rtmp-module

    git clone https://github.com/arut/nginx-rtmp-module.git
    
  6. 安裝gcc g++

    yum install gcc
    yum install gcc-c++
    
  7. 安裝pcre

    1. 解壓pcre

      tar -zxvf pcre-8.38.tar.gz

    2. 進入解壓后的文件夾

      cd pcre-8.38

    3. 編譯前配置

      ./configure

    4. 編譯

      make

    5. 安裝

      make install

  8. 安裝zlib

    1. 解壓zlib

      tar -zxvf zlib-1.2.11.tar.gz

    2. 進入解壓后的文件夾

      cd zlib-1.2.11

    3. 編譯前配置

      ./configure

    4. 編譯

      make

    5. 安裝

      make install

  9. 安裝openssl

    1. 解壓openssl

      tar -zxvf openssl-1.0.2s.tar.gz

    2. 進入解壓后的文件夾

      cd openssl-1.0.2s

    3. 編譯前配置

      ./configure

    4. 編譯

      make

    5. 安裝

      make install

  10. 安裝nginx

    1. 解壓nginx

      tar -zxvf nginx-1.17.0.tar.gz

    2. 進入解壓后的文件夾

      cd nginx-1.17.0

    3. 編譯前配置

      # 配置解釋
      ./configure 
      --prefix=/usr/local/nginx
      --with-pcre=/root/pcre-8.38 # pcre路徑 如有不同需要修改 
      --with-zlib=/root/zlib-1.2.11 # zlib路徑 如有不同需修改
      --with-openssl=/root/openssl-1.0.2s # openssl路徑 如有不同需修改
      --add-module=/root/nginx-rtmp-module # nginx-rtmp-module路徑 如有不同需要修改
      
      # 運行時去掉換行
      
      ./configure --prefix=/usr/local/nginx --with-pcre=/root/pcre-8.38 --with-zlib=/root/zlib-1.2.11 --with-openssl=/root/openssl-1.0.2s --add-module=/root/nginx-rtmp-module
      
    4. 編譯

      make

    5. 安裝

      make install

  11. /etc/init.d 目錄下創建 nginx 文件

    touch /etc/init.d/nginx
    
  12. 編輯nginx文件

    1. vim 打開

      vim /etc/init.d/nginx
      
    2. 復制以下內容

      #!/bin/bash
      #Startup script for the nginx Web Server
      #chkconfig: 2345 85 15
      nginx=/usr/local/nginx/sbin/nginx
      conf=/usr/local/nginx/conf/nginx.conf
      case $1 in 
      start)
      echo -n "Starting Nginx"
      $nginx -c $conf
      echo " done."
      ;;
      stop)
      echo -n "Stopping Nginx"
      killall -9 nginx
      echo " done."
      ;;
      test)
      $nginx -t -c $conf
      echo "Success."
      ;;
      reload)
      echo -n "Reloading Nginx"
      ps auxww | grep nginx | grep master | awk '{print $2}' | xargs kill -HUP
      echo " done."
      ;;
      restart)
      $nginx -s reload
      echo "reload done."
      ;;
      *)
      echo "Usage: $0 {start|restart|reload|stop|test|show}"
      ;;
      esac
      
    3. 保存並退出

  13. 編輯nginx配置文件

    1. 打開配置文件

      vim /usr/local/nginx/conf/nginx.conf
      
    2. 添加以下配置

      rtmp {  #rtmp 協議
      	server  {
      	listen  9999; #端口號
      
      		application live{ 
      		live    on;
      		record  off;
      		}
      		
      		application hls{ #輸出路徑
      		live    on;
      		hls     on;
      		hls_path nginx-rtmp-module/hls;
      		hls_cleanup off;
      		}
      	}
      }    
      
    3. 保存並退出

  14. 啟動nginx

    service nginx start
    
    
  15. 安裝ffmpeg

    1. yum升級

      sudo yum install epel-release -y
      sudo yum update -y
      
      
    2. 安裝第三方源

      sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
      sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
      
      
    3. 安裝ffmpeg

      sudo yum install ffmpeg ffmpeg-devel -y
      
      
  16. rtsp 轉 rtmp 流

    # 命令解釋
    ffmpeg -i 
    "rtsp://admin:admin@192.168.1.208:46599/cam/realmonitor?channel=1&subtype=0" #攝像頭地址
    -f flv -r 15 -an 
    "rtmp://192.168.1.240:9999/hls/demo" #輸出地址 要和ningx一致 demo可以自定義
    
    # 運行時需要去除換行
    # 命令行啟動
    ffmpeg -i "rtsp://admin:admin@192.168.1.208:46599/cam/realmonitor?channel=1&subtype=0" -f flv -r 15 -an "rtmp://192.168.1.240:9999/hls/demo" >> ./log/ffmpeg.out
    
    # 后台啟動 建議寫成shell腳本
    nohup ffmpeg -i "rtsp://admin:admin@192.168.1.208:46599/cam/realmonitor?channel=1&subtype=0" -f flv -r 15 -an "rtmp://192.168.1.240:9999/hls/demo" >> ./log/ffmpeg.out 2>&1 &
    
    
  17. 問題

    1. 無法訪問端口 可能是防火牆開啟中 可以關閉防火牆

      # 關閉防火牆命令:
      systemctl stop firewalld
      
      # 開啟防火牆
      systemctl start firewalld
      
      # 關閉開機自啟動
      systemctl disable firewalld.service
      
      # 開啟開機啟動
      systemctl enable firewalld.service
      
      
    2. ffmpeg解碼時掉線不會重連 目前尚未解決

    3. 花屏,需要下載ffmpeg源碼然后修改並重新編譯
      在ffmpeg源碼udp.c中:將#define UDP_MAX_PKT_SIZE 65536修改為#define UDP_MAX_PKT_SIZE 655360,或者更大值


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM