nginx-rtmp和ffmpeg

一、nginx-rtmp

1、基础准备

安装常用工具:yum -y install vim man wget lftp elinks net-tools lrzsz gpm ntsysv tree cifs-utils createrepo quota bind-utils lsof sysstat nmap bash-completion dos2unix nc telnet ntpdate rng-tools psmisc screen

镜像:nginx-1.15.0.tar.gz

文件:nginx-rtmp-module (下载路径:wget https://github.com/arut/nginx-rtmp-module.git )注:nginx-rtmp-module是一个文件包

安装依赖: yum -y install pcre pcre-devel zlib-devel openssl openssl-devel zlib zlib-devel gcc gcc-c++ autoconf automake make &>/dev/null

创建用户:useradd -M -s /sbin/nologin nginx ##-M不指定家目录,-s指定登录shell

解压:tar zxf nginx-1.15.0.tar.gz -C /usr/src/ &>/dev/null

配置编译安装:cd /usr/src/nginx-1.15.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --add-module=/usr/src/nginx-rtmp-module --with-http_ssl_module &&make &&make install
注:–add-module=/usr/src/nginx-rtmp-module 指定nginx-rtmp-module的位置
–prefix=/usr/local/nginx ##指定安装的位置
–user=nginx ##指定用户
–group=nginx ##指定组

创建软链接:ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
验证:cd
ls -l /usr/local/sbin/nginx
nginx -t
**ok则代表配置文件没有问题**

启动并验证:nginx
netstat -utpln |grep 8080
在这里插入图片描述

使用nginx启动脚本:要设置nginx的启动脚本,所以要先停止nginx进程(就是nginx这个服务)
killall -s KILL nginx ##强制杀死
netstat -utpln |grep 8080

vi /etc/init.d/nginx ##编写nginx的启动脚本

       #!/bin/bash
       # chkconfig: 35 99 20
       # description: Nginx Server Control Script
       NP="/usr/local/nginx/sbin/nginx"
       NPF="/usr/local/nginx/logs/nginx.pid"
       case "$1" in   ##$1表示第一位置变量,$0表示脚本本身
         start)
           $NP;
           if [ $? -eq 0 ]
          then
            echo "nginx is starting!! "
          fi
        ;;
        stop)
          kill -s QUIT $(cat $NPF)
          if [ $? -eq 0 ]
          then
          echo "nginx is stopping!! "
          fi
        ;;
        restart)
          $0 stop
          $0 start
        ;;
        reload)
          kill -s HUP $(cat $NPF)
          if [ $? -eq 0 ]
          then
            echo "nginx config file is reload! "
          fi
        ;;
        *)
          echo "Usage: $0 {start|stop|restart|reload}"
          exit 1
      esac
      exit 0

chkconfig --add nginx ##添加系统服务
chmod +x /etc/init.d/nginx ##授权
/etc/init.d/nginx stop
/etc/init.d/nginx start
/etc/init.d/nginx restart
netstat -utpln |grep nginx ##查看nginx监听端口
浏览器访问网站:
在这里插入图片描述

linux访问网站
yum -y install elinks
elinks --dump http://192.168.2.10
在这里插入图片描述

2、配置文件

vi /usr/local/nginx/conf/nginx.conf

       #user  nobody;
       worker_processes  1;
       
       #error_log  logs/error.log;
       #error_log  logs/error.log  notice;
       #error_log  logs/error.log  info;
       
       pid        /usr/local/nginx/logs/nginx.pid;
      
      
      events {
          worker_connections  1024;
      }
      rtmp {
          server {
              listen 1935;
              application live {
                  live on;
      }
              application hls {
                  live on;
                  hls on;
                  hls_path /usr/local/nginx/html/hls;
                  hls_fragment 8s;
              }
          }
      }
      
      http {
          include       mime.types;
          default_type  application/octet-stream;
      
          log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                            '$status $body_bytes_sent "$http_referer" '
                            '"$http_user_agent" "$http_x_forwarded_for"';
      
          access_log  logs/access.log  main;
      
          sendfile        on;
          #tcp_nopush     on;
      
          #keepalive_timeout  0;
          keepalive_timeout  65;
      
          #gzip  on;
      
          server {
              listen       8080;
              server_name  localhost;
      
              #charset koi8-r;
      
              #access_log  logs/host.access.log  main;
      
              location / {
                  root   html;
                  index  index.html index.htm;
              }
      #        location /hls {
      #            types {
      #                application/vnd.apple.mpegurl m3u8;
      #                video/mp2t ts;
      #            }
      #            root /tmp;
      #            add_header Cache-Control no-cache;
      #        }
              location /stat {
                  rtmp_stat all;
                  rtmp_stat_stylesheet stat.xsl;
              }
              location /stat.xsl {
                  root /usr/src/nginx-rtmp-module/;
              }
              error_page   500 502 503 504  /50x.html;
              location = /50x.html {
                  root   html;
              }
          }
      }

nginx -t ##测试配置文件
在这里插入图片描述

mkdir /usr/local/nginx/html/hls
chmod +x /usr/local/nginx/html/hls
/etc/init.d/nginx restart
netstat -utpln |grep nginx
在这里插入图片描述
在这里插入图片描述

rtmp监听1935端口
nginx 监听8080端口

二、ffmpeg

1、安装依赖:

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 &&make install
echo $?
cd

2、yum安装ffmpeg

## 1)升级系统

sudo yum install epel-release -y
sudo yum update -y
sudo shutdown -r now

2)安装Nux Dextop Yum 源

由于CentOS没有官方FFmpeg rpm软件包。但是,我们可以使用第三方YUM源(Nux Dextop)完成此工作。

## (1)CentOS 7

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

## (2) CentOS 6

sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el6/x86_64/nux-dextop-release-0-2.el6.nux.noarch.rpm

## 3)安装FFmpeg 和 FFmpeg开发包

sudo yum install ffmpeg ffmpeg-devel -y

## 4)测试是否安装成功

ffmpeg

## 5)如果你想了解更多关于FFmpeg使用方面的资料,可以输入:
ffmpeg -h

注:推流地址:rtmp://192.168.2.10:1935/hls/test 拉流地址:rtmp://192.168.2.10:1935/hls/test 注:test为名称自己去,拉流地址可以是http,也可以是rtmp。

Logo

更多推荐