环境
系统环境:Centos 7
需求
centos7编译安装nginx nginx-rtmp
准备
安装依赖:
yum install -y git gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel
下载安装包:
#download nginx
wget -c http://nginx.org/download/nginx-1.8.1.tar.gz
#download pcre
wget -c https://sourceforge.net/projects/pcre/files/pcre/8.35/pcre-8.35.tar.gz
#download zlib
wget -c http://zlib.net/zlib-1.2.8.tar.gz
#download openssl
wget -c http://www.openssl.org/source/openssl-1.0.1i.tar.gz
注意:这里的依赖包不需要编译版的,需要指定未编译的路径
注意:这里的依赖包不需要编译版的,需要指定未编译的路径(好像编译nginx的时候其他依赖都是需要未编译的)
添加用户
groupadd -r nginx
useradd -r -g nginx
下载nginx-rtmp-module
git clone https://github.com/arut/nginx-rtmp-module.git
#编译的时候添加nginx-rtmp-module模块不需要则不添加
--add-module=path_of_/nginx-rtmp-module
编译安装nginx
./configure \
--prefix=/opt/nginx \#nginx安装路径
--user=nginx \#添加用户
--group=nginx \#添加用户组
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_realip_module \
--pid-path=/var/run/nginx.pid \#pid路径
--with-pcre=/opt/software/pcre-8.35 \ #依赖pcre的源码路径
--with-zlib=/opt/software/zlib-1.2.8 \ #依赖zlib的源码路径
--with-openssl=/opt/software/openssl-1.0.1i #依赖openssl的源码路径
--add-module=/opt/software/nginx-rtmp-module #添加nginx-rtmp-module模块(要弄rtmp服务器必须加上这条)
make
make install
修改nginx.conf配置
#user nobody;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
#切换自动推送(多 worker 直播流)模式。默认为 off
rtmp_auto_push on;
#当 worker 被干掉时设置自动推送连接超时时间。默认为 100 毫秒
rtmp_auto_push_reconnect 1s;
rtmp {
server {
listen 1935;
#直播流配置
application myapp {
live on;
}
application hls {
live on;
hls on;
hls_path /tmp/hls;
}
application qiniu {
live on;
push rtmp://127.0.0.1:1935/myapp/test1;#rtmp配置
#push rtmp://127.0.0.1:1935/hls/test2;#hls配置
}
application pull {
live on;
pull rtmp://127.0.0.1:1935/myapp/test1;#rtmp配置
#pull rtmp://127.0.0.1:1935/hls/test2;#hls配置
}
#rtmp日志设置
access_log logs/rtmp_access.log ;
}
}
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 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /opt/www/html;
index index.html index.htm;
}
#rtmp状态页面
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /opt/software/nginx-rtmp-module/;
}
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include vhosts/*.conf;
}
重启ngixn
nginx -t #测试nginx配置是否有错
nginx -s reload #重新加载nginx配置
附录
使用ffmpeg推流到nginx
- 推一个本地的mp4到上面配置的myapp上(rtmp):
ffmpeg -re -i /tmp/ffmpeg_test.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.0.0.1:1935/myapp/test1"
rtmp://115.29.123.32:1935/myapp/test1 (115.29.123.32是该服务器ip,我是配合vlc拉流测试的)
- 推一个本地的mp4到hls上
ffmpeg -re -i /tmp/ffmpeg_test.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.0.0.1:1935/hls/test2"
流播放地址为: http://10.0.0.6/hls/test2.m3u8(这里没有端口号)
FFmpeg合并视频
ffmpeg -i input1.flv -c copy -bsf:v h264_mp4toannexb -f mpegts input1.ts
ffmpeg -i input2.flv -c copy -bsf:v h264_mp4toannexb -f mpegts input2.ts
ffmpeg -i input3.flv -c copy -bsf:v h264_mp4toannexb -f mpegts input3.ts
ffmpeg -i "concat:input1.ts|input2.ts|input3.ts" -c copy -bsf:a aac_adtstoasc -movflags +faststart output.mp4
如果上面个合并命令不成功
ffmpeg -i "concat:input1.ts|input2.ts|input3.ts" -c copy -bsf:a aac_adtstoasc output.mp4(去掉 -movflags +faststart)
FFmpeg的安装方法请参考文章:http://blog.csdn.net/loyachen/article/details/50909854
可能有的文件下载有些慢 可以下载好再上传上去