Nginx 搭建流媒体服务器

Published on 2015 - 12 - 17

Nginx 搭建流媒体服务器

安装EPEl仓库与RPMFORGE仓库

wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm

wget -c http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

安装依赖包

yum install gcc make automake gcc-c++ bzip2 unzip patch subversion libjpeg-devel conf autoconf libtool libtool-devel zlib zlib-devel faac-devel lame-devel amrnb-devel opencore-amr-devel amrwb-devel libvorbis-devel libtheora-devel xvidcore-devel mplayer mencoder flvtool2 openssl openssl-devel yamdi pcre pcre-devel gd libXpm libxslt freetype freetype-devel

安装yasm

wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz

tar -zxvf yasm-1.2.0.tar.gz && cd yasm-1.2.0

./configure

make && make install

安装Mplayer

wget -c http://www2.mplayerhq.hu/MPlayer/releases/codecs/essential-amd64-20071007.tar.bz2

mkdir /usr/local/lib/codecs

tar -xvf essential-amd64-20071007.tar.bz2 -C /usr/local/lib/codecs/

vim /etc/ld.so.conf

/usr/lib
/usr/local/lib

ldconfig

安装X.264

wget ftp://ftp.videolan.org/pub/videolan/x264/snapshots/last_stable_x264.tar.bz2

tar -jxvf last_stable_x264.tar.bz2

cd x264-snapshot-20151215-2245-stable/

./configure --enable-shared -enable-pic

make && make install

安装libvpx

wget http://webm.googlecode.com/files/libvpx-v1.2.0.tar.bz2

tar -xvf libvpx-v1.2.0.tar.bz2

cd libvpx-v1.2.0

./configure --enable-shared --enable-pic

make && make install

安装ffmpeg

wget http://ffmpeg.org/releases/ffmpeg-2.0.1.tar.bz2

tar -jxvf ffmpeg-2.0.1.tar.bz2 && cd ffmpeg-2.0.1

./configure --enable-gpl --enable-version3 --enable-shared --enable-nonfree --enable-postproc --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-libfreetype

make && make install    `

测试mencoder

mencoder -oac help
# 查看视频编码

安装Nginx 与 所需模块

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

wget http://sourceforge.net/projects/pcre/files/pcre/8.37/pcre-8.37.tar.gz

wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz

wget -O nginx-rtmp-module.zip  https://github.com/arut/nginx-rtmp-module/archive/master.zip

tar -xvf nginx_mod_h264_streaming-2.2.7.tar.gz

cd nginx_mod_h264_streaming-2.2.7

vi ngx_http_streaming_module.c

注释掉nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c的158到161行
 /* TODO: Win32 */
 //if (r->zero_in_uri)
 // {     
 //   return NGX_DECLINED;     
 // }    

安装RTMP

unzip nginx-rtmp-module.zip

安装清缓存模块

unzip ngx_cache_purge.zip

编译Nginx

tar -zxvf nginx-1.8.0.tar.gz

./configure --user=nobody --group=nobody --prefix=/usr/local/nginx/ --add-module=../nginx-rtmp-module-master --add-module=../ngx_cache_purge-master --add-module=../nginx_mod_h264_streaming-2.2.7 --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_flv_module --with-pcre

make && make install

/usr/local/nginx/sbin/nginx    

添加到环境变量

vi /root/.bash_profile

PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin

source /root/.bash_profile

测试

http://192.168.18.128/test2.mp4

/mnt/media/video -> 存放原始视频
/mnt/media/app  -> 存放转成m3u8的视频,供http访问(HLS)
/mnt/media/vod  -> 存放转换后的flv和mp4视频,供http或rtmp访问

Nginx 配置文件

#####以下是配置文件#####

user nobody nobody;
worker_processes  1;
events {
worker_connections  1024;
}
rtmp {
server {
listen 1935;
chunk_size 4000;
application vod {
    play /mnt/media/vod;
}
application hls {
        hls on;
        hls_path /mnt/media/app;
        hls_fragment 10s;
}
}
}
http {
include       mime.types;
default_type  application/octet-stream;
sendfile        on;
keepalive_timeout  65;
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
    listen       8080;
    server_name  localhost;
    location / {
    root /mnt/soft/nginx-rtmp-module-master/test/rtmp-publisher;
    }
location /stat {
    rtmp_stat all;
    rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
    root /mnt/soft/nginx-rtmp-module-master;
}
location /control {
    rtmp_control all;

}
}
server {
listen        80;
server_name     localhost;
location / {
    root /mnt/wwwroot;
    index index.html;
}
location ~ \.flv$ {
    root /mnt/media/vod;
    flv;
    limit_conn addr 20;
    limit_rate 200k;
}
location ~ \.mp4$ {
    root /mnt/media/vod;
    mp4;
    limit_conn addr 20;
    limit_rate 200k;
}
location /hls {
    alias /mnt/media/app;
}
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
}

#####以上是配置文件#####

ffmpeg转码命令

ffmpeg -i test1.mp4 -vf "drawtext=fontfile=/mnt/media/vod/SimSun.ttf:fontsize=48:fontcolor=white:text='WoYaoZhongDaJiang':x=0:y=0" -y -vcodec libx264 -b 1000k -bt 750k -acodec libfaac -ac 2 -ar 44100 -ab 128 -s 640x360 -aspect 16:9 test6.mp4