centos7+nginx+rtmp+ffmpeg搭建流媒體服務器
1.安裝前需要的工具
#net-tool 查本地IP
#wget 下載安裝包
#unzip 解壓zip包
#gcc gcc-c++ perl 編譯軟件包用
yum install -y net-tools wget unzip gcc gcc-c++ perl
2. 將將Centos的yum源更換為國內的阿里云源
#備份yum源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
#下載阿里源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#清空緩存
yum makecache
3. 安裝nginx及rtmp
將當前目錄切換的
/home
下#切換的homme目錄 cd /home
1. 下載并解壓pcre
#下載pcre包
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.41.tar.gz
#解壓pcre包
tar -zxvf pcre-8.41.tar.gz
2. 下載并解壓zlib
#下載zlib包
wget http://www.zlib.net/zlib-1.2.11.tar.gz
#解壓zlib包
tar -zxvf zlib-1.2.11.tar.gz
3.下載并安裝openssl
#下載openssl包
wget https://www.openssl.org/source/old/1.0.1/openssl-1.0.1i.tar.gz
#解壓openssl包
tar -zxvf openssl-1.0.1i.tar.gz
#切換到openssl里
cd openssl-1.0.1i
#生成配置文件 默認配置
./configure
#編譯程序
make
#安裝程序
make install
4. 下載并解壓nginx-rtmp-model
#下載rtmp包
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
#解壓下載包
unzip -o master.zip
#修改文件夾名
mv master nginx-rtmp-module
5.安裝nginx
#下載nginx
wget http://nginx.org/download/nginx-1.12.2.tar.gz
#解壓nignx
tar -zxvf nginx-1.12.2.tar.gz
#切換到nginx中
cd nginx-1.12.2
#生成配置文件,將上述下載的文件配置到configure中
./configure --prefix=/usr/local/nginx --with-pcre=/home/pcre-8.41 --with-zlib=/home/zlib-1.2.11 --with-openssl=/home/openssl-1.0.1i --add-module=/home/nginx-rtmp-module
#編譯程序
make
#安裝程序
make install
#查看nginx模塊
nginx -V
4. 安裝ffmpeg
#安裝epel包
yum install -y epel-release
#導入簽名
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
#導入簽名
rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
#升級軟件包
rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
#更新軟件包
yum update -y
#安裝ffmpeg
yum install -y ffmpeg
#檢查版本
ffmpeg -version
5. 修改配置運行服務
1.修改nginx配置
vi /usr/local/nginx/conf/nginx.conf
修改之后的配置
#工作進程
worker_processes 1;
#事件配置
events {
worker_connections 1024;
}
#RTMP配置
rtmp {
server {
#監(jiān)聽端口
listen 1935;
#
application myapp {
live on;
}
#hls配置
application hls {
live on;
hls on;
hls_path /tmp/hls;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
#配置hls
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
2.執(zhí)行nginx
/usr/local/nginx/sbin/nginx
3. 打開防火墻
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
4.測試功能
#查看IP
ifconfig
#在home下上傳一個test.mp4視頻
#推送rtmp流 下載一個VLC測試
ffmpeg -re -i "/home/test.mp4" -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://{IP}:1935/myapp/test1
#推送hls流 寫個HTML測試
ffmpeg -re -i "/home/test.mp4" -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://{IP}:1935/hls/test2
<script src="https://cdn.jsdelivr.net/hls.js/latest/hls.min.js"></script>
<video id="video"></video>
<script>
if(Hls.isSupported()) {
var video = document.getElementById('video');
var hls = new Hls();
hls.loadSource('http://{IP}/hls/test2.m3u8');
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED,function() {
video.play();
});
}
</script>