安装nginx与nginx-rtmp-module实现流媒体服务器直播


1.安装依赖包:

yum -y install git gcc gcc-c++ automake autoconf pcre pcre-devel glibc glibc-devel pkgconfig lib-devel openssl openssl-devel expat-devel gettext-devel libtool mhash perl-Digest-SHA1 zlib zlib-devel libogg libvorbis libass libass-devel vorbis-tools libvorbis libvorbis-devel

2.安装yadmi

wget https://jaist.dl.sourceforge.net/project/yamdi/yamdi/1.9/yamdi-1.9.tar.gz

tar zxvf yamdi-1.9.tar.gz
cd yamdi-1.9
make && make install

flv视频添加关键帧测试

yamdi -i huamukan.flv -o yes_huamukan.flv

3.安装nasm

wget https://www.nasm.us/pub/nasm/releasebuilds/2.13.03/nasm-2.13.03.tar.gz

tar zxvf nasm-2.13.03.tar.gz
cd nasm-2.13.03
./configure
make
make install

4.安装x264

git clone git://git.videolan.org/x264
cd x264/
./configure --enable-shared
make
make install

5.安装FAAD2

wget http://downloads.sourceforge.net/project/faac/faad2-src/faad2-2.7/faad2-2.7.tar.gz
tar zxvf faad2-2.7.tar.gz
cd faad2-2.7
./configure
make
make install

 

6.安装Xvid

wget https://downloads.xvid.com/downloads/xvidcore-1.3.5.tar.gz
tar zxvf xvidcore-1.3.5.tar.gz
cd xvidcore
cd build/generic/
./configure
make
make install

7.安装lame

wget https://jaist.dl.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz
tar zxvf lame-3.100.tar.gz
cd lame-3.100
./configure --enable-nasm
make
make install

8.安装libvpx

wget http://www.loongnix.org/cgit/libvpx/snapshot/libvpx-1.7.0.tar.gz
tar zxvf libvpx-1.7.0.tar.gz
cd libvpx-1.7.0
./configure --enable-shared
make
make install

9.安装ffmpeg

git clone git://source.ffmpeg.org/ffmpeg
cd ffmpeg/
./configure --prefix=/opt/ffmpeg/ --enable-version3 --enable-libvpx --enable-libmp3lame --enable-libvorbis --enable-libx264 --enable-libxvid --enable-shared --enable-gpl --enable-postproc --enable-nonfree --enable-avfilter --enable-pthreads

make && make install

修改/etc/ld.so.conf如下:
include ld.so.conf.d/*.conf
/lib
/lib64
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/opt/ffmpeg/lib
# ldconfig

10.安装nginx与nginx-rtmp-module模块

下载最新版本nginx并解压

下载nginx-rtmp-module模块

1
git clone git://github.com/arut/nginx-rtmp-module.git
下载nginx_mod_h264_streaming-2.2.7并解压
http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
编译配置

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --add-module=nginx_mod_h264_streaming-2.2.7 --with-cc-opt=-I/opt/ffmpeg/include --with-ld-opt=`-L/opt/ffmpeg/lib -Wl, -rpath=/opt/ffmpeg/lib` --add-module=nginx-rtmp-module/

make && make install

可能遇到问题1
/root/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c: In function ‘ngx_streaming_handler’:
/root/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c:158: error: ‘ngx_http_request_t’ has no member named ‘zero_in_uri’
make[1]: *** [objs/addon/src/ngx_http_h264_streaming_module.o] Error 1
make[1]: Leaving directory `/root/nnginx-1.5.9'
make: *** [build] Error 2
那么将src/ngx_http_streaming_module.c文件中以下代码删除或者是注释掉就可以了:

/* TODO: Win32 */
if (r->zero_in_uri)
{
return NGX_DECLINED;
}

可能遇到问题2
../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c: 在函数‘esds_read’中:

../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:377:16: 错误: 变量‘stream_priority’被设定但未被使用 [-Werror=unused-but-set-variable]

../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:376:12: 错误: 变量‘stream_id’被设定但未被使用 [-Werror=unused-but-set-variable]

../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c: 在函数‘stsd_parse_vide’中:

../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:529:22: 错误: 变量‘level_indication’被设定但未被使用 [-Werror=unused-but-set-variable]

../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:528:22: 错误: 变量‘profile_compatibility’被设定但未被使用 [-Werror=unused-but-set-variable]

../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:527:22: 错误: 变量‘profile_indication’被设定但未被使用 [-Werror=unused-but-set-variable]

../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:526:22: 错误: 变量‘configuration_version’被设定但未被使用 [-Werror=unused-but-set-variable]

cc1: all warnings being treated as errors

make[1]: *** [objs/addon/src/mp4_reader.o] 错误 1

解决方法:
# vim objs/Makefile (修改objs/Makefile文件, 去掉其中的"-Werror"), 然后就能够正常编译了.

nginx完整配置如下:

#user nobody;
worker_processes 1;

#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 65535;
}

rtmp {
server {
listen 1935;
#点播配置
application vod {
play /opt/vide/vod;
}

#直播流配置
application live {
live on;
#为 rtmp 引擎设置最大连接数。默认为 off
max_connections 1024;
hls on; #这个参数把直播服务器改造成实时回放服务器。
wait_key on; #对视频切片进行保护,这样就不会产生马赛克了。
hls_path /opt/video/hls; #切片视频文件存放位置。
hls_fragment 10s; #每个视频切片的时长。
hls_playlist_length 60s; #总共可以回看的事件,这里设置的是1分钟。
hls_continuous on; #连续模式。
hls_cleanup on; #对多余的切片进行删除。
hls_nested on; #嵌套模式。
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;
location / {
root html;
index index.html index.htm;
}

location /live {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /opt/video/hls;
}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /usr/local/nginx/html/nginx-rtmp-module/;
}
location /rtmp-publisher {
root /usr/local/nginx/html/nginx-rtmp-module/test;
}
location /hls {
types{
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /opt/video/hls;
}

}
#支持flv
server
{
listen 8081;
server_name localhost;
root /usr/local/nginx/html/nginx-rtmp-module/test/www;
location ~ .*.(flv|swf|mp4|wma|wmv)$ {
valid_referers none blocked *.xxxx.com http://localhost;
if ($invalid_referer) {
return 403;
}
}
location ~ \.flv$ {
flv;
limit_rate 200k;
}
location ~ \.mp4$ {
flv;
#limit_conn one 20;
limit_rate 200k;
}
access_log logs/nginxflv_access.log main;
}

server
{
listen 8082;
server_name localhost;
index index.html;
location / {
root /usr/local/nginx/html/nginx-rtmp-module/test/rtmp-publisher;
}
access_log logs/nginxrtmpflv_access.log main;
}

server {
listen 8080;
server_name localhost;
index player.html;

location /stat {
rtmp_stat all;
rtmp_stat_stylesheet /usr/local/nginx/html/nginx-rtmp-module/stat.xsl;
}
location /stat.xsl {
root /usr/local/nginx/html/nginx-rtmp-module;
}

location /rtmp-publisher {
root /usr/local/nginx/html/nginx-rtmp-module/test/rtmp-publisher;
}

location / {
root /usr/local/nginx/html/nginx-rtmp-module/test/rtmp-publisher;
}
}

}

可以使用vlc播放器测试了。

ffmepg推流

/opt/ffmpeg/bin/ffmpeg -re -i /root/yes_huamukan.flv -vcodec copy -f flv rtmp://172.24.191.247:1935/live/test

 

 

 

 


Whatever is worth doing is worth doing well.