zoukankan      html  css  js  c++  java
  • 在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,或者更大值

  • 相关阅读:
    botzone Tetris2
    NOIP2017游记
    城乡联谊胡策会糊厕R3
    SRM 20
    AtCoder Regular Contest 082
    AtCoder Grand Contest 019
    复数模版
    SRM13
    NOI2017&&codeM2017游记
    java多线程编程
  • 原文地址:https://www.cnblogs.com/97jay/p/12286845.html
Copyright © 2011-2022 走看看