nginx直播搭建

cd安装之前,先要切换到存放安装软件的目录,目录自定义,如:/usr/local

命令:

cd /usr/local

一、安装ffmpeg以及所需要的辅助工具

1、安装yasm

从http://www.tortall.net/projects/yasm/releases上面获取到最新的安装包下载路径。

安装:

cd /usr/local

# 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

2、安装libx264

cd /usr/local

git clone git://git.videolan.org/x264.git

cd x264

./configure --enable-shared --enable-static --disable-asm

make && make install

3、安装ffmpeg

cd /usr/local

wget http://www.ffmpeg.org/releases/ffmpeg-4.2.1.tar.gz

tar -zxvf ffmpeg-4.2.1.tar.gz

cd ffmpeg-4.2.1

./configure --enable-shared --enable-yasm --enable-libx264 --enable-gpl --enable-pthreads --extra-cflags=-I/usr/local/x264/include --extra-ldflags=-L/usr/local/x264/lib

make && make install

二、安装nginx和nginx-rtmp-module

1、下载nginx-rtmp-module

cd /usr/local

git clone https://github.com/arut/nginx-rtmp-module.git

2、编译安装nginx

yum install -y pcre pcre-devel

yum install -y zlib zlib-devel

yum install -y openssl openssl-devel

cd /usr/local

wget http://nginx.org/download/nginx-1.9.9.tar.gz

tar -zxvf nginx-1.9.9.tar.gz

cd nginx-1.9.9

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --add-module=/usr/local/nginx-rtmp-module

make && make install

3、配置nginx

server {

        listen      8082;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /control {

            rtmp_control all; #可以配置all/record/redirect/drop

        }

        location / {

                root /usr/local/www/mp4;

        }

        error_page  500 502 503 504  /50x.html;

        location = /50x.html {

            root  html;

        }

    }

rtmp {

    server {

        listen 1935;  #监听的端口

        chunk_size 4096;

        application live {  #rtmp推流请求路径

            live on;

            recorder rec1 {

                record all manual;

                record_path /usr/local/www/flv;

                record_suffix -%Y-%m-%d-%H-%M-%S.flv;

                exec_record_done /usr/bin/sh /usr/local/www/rtmp.sh $path $basename >>/usr/local/www/ffmpeg.log;

            }

        }

    }

}


rtmp.sh内容如下:

#!/bin/bash

/usr/local/bin/ffmpeg -y -i $1 -vcodec copy -acodec copy /www/video/mp4/$2.mp4 && pid=$!;

apiUrl="这里是接口地址"

domain="这里是播放地址"

while true

do

    existpid=$(ps aux | awk '{print $2}'| grep -w $pid)

    if [ ! $existpid ]

    then

        rm -rf $1

    curl ${apiUrl}${domain}$2".mp4"

        break

    else

        echo $pid

        sleep 10

    fi

done

你可能感兴趣的:(nginx直播搭建)