nginx搭建支持http和rtmp协议的流媒体服务器

分类: 网络与安全

实验目的:
让Nginx支持flv和mp4格式文件,支持RTMP协议的直播和点播;
同时打开RTMP的HLS功能

资料:
HTTP Live Streaming(缩写是 HLS)是一个由苹果公司提出的基于HTTP的流媒体网络传输协议。
HLS只请求基本的HTTP报文,与实时传输协议(RTP)不同,HLS可以穿过任何允许HTTP数据通过的防火墙或者代理服务器。
它也很容易使用内容分发网络来传输媒体流。
使用ffmpeg来完成对flv、mp4、mp3等格式的转化(点播实验暂时不测试)

一、准备工作
Nginx: http://nginx.org
模块:nginx_mod_h264_streaming(支持h264编码的视频)
模块:http_flv_module 支持flv
模块:http_mp4_module 支持mp4
下载地址:
http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
https://github.com/arut/nginx-rtmp-module

1、安装依赖包:
#yum -y install gcc glibc glibc-devel make nasm pkgconfig lib-devel openssl-devel expat-devel gettext-devel libtool mhash.x86_64 perl-Digest-SHA1.x86_64

2、安装相关工具包
1). git
# mkdir soft-source
# cd soft-source
# wget http://codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.xz
# xz -d git-latest.tar.xz
# tar xzvf git-latest.tar 
# cd git-2014-06-27
# autoconf
# ./configure
# make && make install
# git --version
git version 2.0.0.GIT
# cd ..

2). zlib
# wget http://zlib.net/zlib-1.2.8.tar.gz
# tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8
# ./configure
# make
# make install
# cd ..

3). pcre
# wget http://exim.mirror.fr/pcre/pcre-8.12.tar.gz
# tar zxvf pcre-8.12.tar.gz
# cd pcre-8.12
# ./configure
# make && make install
# cd ..

4). yadmi
yadmi的作用是为flv文件添加关键帧,才能实现拖动播放
# wget http://sourceforge.net/projects/yamdi/files/yamdi/1.4/yamdi-1.4.tar.gz/download  
# tar xzvf yamdi-1.4.tar.gz
# cd yamdi-1.4
# make && make install
# cd ..
 
使用方法:
# yamdi -i input.flv -o out.flv
给input.flv文件 添加关键帧,输出为out.flv文件

5). OpenSSL
# wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
# tar -zxvf openssl-1.0.1c.tar.gz
# ./config
# make
# make install

3、安装ffmpeg及其依赖包:
1). Yasm
# wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
# tar xzvf yasm-1.2.0.tar.gz
# cd yasm-1.2.0
# ./configure
# make
# make install
# cd ..

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

3). LAME
# wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
# tar xzvf lame-3.99.5.tar.gz
# cd lame-3.99.5
#./configure --enable-nasm
# make
# make install
# cd ..

4). libogg
# wget http://downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.gz
# tar xzvf libogg-1.3.0.tar.gz
# cd libogg-1.3.0
# ./configure
# make
# make install
# cd ..

5). libvorbis
# wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz
# tar xzvf libvorbis-1.3.3.tar.gz
# cd libvorbis-1.3.3
# ./configure
# make
# make install
# cd ..

6). libvpx
# git clone http://git.chromium.org/webm/libvpx.git
# cd libvpx
# ./configure  --enable-shared
# make
# make install
# cd ..

7). 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
# cd ..

8). FAAC
# wget http://downloads.sourceforge.net/project/faac/faac-src/faac-1.28/faac-1.28.tar.gz
# tar zxvf faac-1.28.tar.gz
# cd faac-1.28
# ./configure
# make
# make install
# cd ..

9). Xvid
# wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
# tar zxvf xvidcore-1.3.2.tar.gz
# cd xvidcore/build/generic
# ./configure
# make
# make install
# cd ..

10). ffmpeg
# git clone git://source.ffmpeg.org/ffmpeg
# cd ffmpeg
# ./configure  --prefix=/opt/ffmpeg/ --enable-version3  
--enable-libvpx --enable-libfaac --enable-libmp3lame  
--enable-libvorbis --enable-libx264 --enable-libxvid 
--enable-shared --enable-gpl --enable-postproc --enable-nonfree  
--enable-avfilter --enable-pthreads
# make && make install
# cd ..

修改/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

二、安装Nginx相关模块
1. 模块安装
# wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
# tar zxvf nginx_mod_h264_streaming-2.2.7.tar.gz
# git clone git://github.com/arut/nginx-rtmp-module.git

2. 创建用户和组
# groupadd www
# useradd -g www www

3. nginx安装
# wget http://nginx.org/download/nginx-1.6.0.tar.gz
# tar zxvf nginx-1.6.0.tar.gz

# cd nginx-1.6.0


分类: 网络与安全

三、配置Nginx,实现VOD,以HTTP方式播放MP4、FLV,实现进度条可拖动
1. 设置configure,nginx的补充编译,增加FLV和MP4功能。
# cd cd nginx-1.6.0 
# vim nginx_configure.sh
#!/bin/sh

echo "configure start ..."
./configure --prefix=/opt/nginx \
--add-module=../nginx_mod_h264_streaming-2.2.7 \
--with-http_flv_module \
--with-http_ssl_module \
--with-http_mp4_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-pcre=/opt/nginx_http_rtmp/pcre-8.12 \
--with-zlib=/opt/nginx_http_rtmp/soft_source/zlib-1.2.8 \
--user=www --group=www \
--add-module=../nginx-rtmp-module \
--with-cc-opt=-I/opt/ffmpeg/include \
--with-ld-opt=`-L/opt/ffmpeg/lib -Wl, -rpath=/opt/ffmpeg/lib`
echo "configure end!"
【保存并退出】
# chmod +x nginx_configure.sh
# ./nginx_configure.sh
# make && make install

http_flv_module和http_mp4_module即为对应的解析和seek功能支持。

2. conf/nginx.conf 支持
# vim conf/nginx.conf
【编辑nginx.conf】
#user  www www;
worker_processes  4;
error_log  logs/error.log  info;
pid        logs/nginx.pid;

worker_rlimit_nofile 51200;
events
{
  use epoll;
  worker_connections  51200;
}

http
{
  include                       mime.types;
  default_type                  application/octet-stream;

  server_names_hash_bucket_size 128;
  client_header_buffer_size     32k;
  large_client_header_buffers   4 32k;

  client_max_body_size          50m;
  limit_conn_zone $binary_remote_addr zone=perip:256k;
  limit_conn_log_level notice;

  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

  sendfile        on;
  tcp_nopush      on;

  keepalive_timeout  65;
  tcp_nodelay on;

  gzip  on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;

  server
  {
    listen   8000;
    server_name localhost;

    location /
    {
      index  index.html;
      root /opt/pub/media/nginx;
      uwsgi_pass 127.0.0.1:9000;
      include uwsgi_params;
      limit_rate_after 50m;
      limit_rate       1m;
      uwsgi_param  UWSGI_CHDIR /opt/pub/media/nginx;
      uwsgi_param  UWSGI_SCRIPT apprun;
      location ~ \.flv$
      {
        flv;
      }
      location ~ \.mp4$
      {
        mp4;
      }
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
    {
      root /opt/pub/media/nginx;
      break;
    }
  }
}
【保存并退出】
NOTE: 
flv和mp4的location要写在上面目录location的里面,不然可能会有权限问题。
limit_rate_after,是说文件下载了5M以后才限速到limit_rate=512K;

3. 下载jwplayer
这个开源代码用的比较广,据说youtube第一版用的就是它。
方式一: 使用官方版本
下载地址在: 
  http://www.longtailvideo.com/support/jw-player/jw-player-for-flash-v5/15991/getting-started
下载时把Keep me informed of news, offers & updates和Include Viral, a video sharing plugin去掉。
或从这里下载:
  http://blogimg.chinaunix.net/blog/upfile2/100607142612.rar
下载下来的zip包,将plyaer.swf和video.mp4 直接放到
/opt/pub/media/nginx目录下或者
index.html同级目录下,即/usr/local/nginx/html/下,(我测试没有通过,不知道为什么),
启动nginx:
# /usr/local/nginx/sbin/nginx

在浏览器中输入:
http://192.168.1.106:8000/player.swf?type=http&file=video.mp4 

方式二: 使用去水印版本
也可以下个去水印,去版权版本的,如:
http://hi.baidu.com/tuberose1605/item/a09f271cd26ee90eb88a1a72
将解码后的文件放在:
/opt/pub/media/nginx/web 下;
将播放素材放在:
/opt/pub/media/nginx/vod 下;
在浏览器中输入:
http://10.2.175.10:8000/web/pl.swf?type=http&file=/vod/video.flv

4. HTML页面嵌入播放器模式
用chrome审查元素,观察network,你发现当你seek时,是会请求一个新流过来的,这个实际上已经相当于实时流媒体了。
因为普通http+mp4(flv)方式下,这个视频文件没有完全下载下来之前,你是无法拖动到后面的(后面没有下载的地方),
因此nginx这种方式称为http 伪流媒体(HTTP Pseudo-Streaming),参考jwplayer官方网站中关于这个伪流媒体概念的介绍。

但是这个地址的输入太死板了,你在浏览器中看到的是一个全屏幕的SWF播放器界面。如何在HTML中定制呢?
STEP1. 编写HTML代码



  
  



  
    
    
    
    
    <embed
      type="application/x-shockwave-flash"
      id="player2"
      name="player2"
      src="jwplayer_5_7_delogo/pl.swf" 
      width="400" 
      height="315"
      allowscriptaccess="always" 
      allowfullscreen="true"
      flashvars="file=/vod/video.flv&image=jwplayer_5_7_delogo/preview.jpg" 
    />
  
  



将此HTML代码(假设文件名为player.html)放到web目录下,
在web目录下新建jwplayer_5_7_delogo目录,将jwplayer.js,player.swf放在此目录下。
在web目录下新建vod目录,将video.flv放在vod目录下;

STEP2. 修改nginx.conf
在原有的配置上增加一个监听80端口的虚机:
【nginx.conf】
...
http 
{
  server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
      #root   html;
      root /opt/pub/media/nginx/web;
      index  index.html index.htm;
    }
  }
  ...
}
【保存并退出】

STEP3. reload nginx 
然后输入以下代码:
http://192.168.1.106/player.html
这个时候,你看到的是一个带有宽和高的swf初始页面,当然你可以写更多的css代码来美化; 

分类: 网络与安全

四、配置Nginx,实现VOD,以RTMP方式播放FLV
1. 设置configure,nginx的补充编译,增加FLV和MP4功能。
# cd cd nginx-1.6.0 
# vim nginx_configure.sh
#!/bin/sh

echo "configure start ..."
./configure --prefix=/opt/nginx \
--add-module=../nginx_mod_h264_streaming-2.2.7 \
--with-http_flv_module \
--with-http_ssl_module \
--with-http_mp4_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-pcre=/opt/nginx_http_rtmp/pcre-8.12 \
--with-zlib=/opt/nginx_http_rtmp/soft_source/zlib-1.2.8 \
--user=www --group=www \
--add-module=../nginx-rtmp-module \
--with-cc-opt=-I/opt/ffmpeg/include \
--with-ld-opt=`-L/opt/ffmpeg/lib -Wl, -rpath=/opt/ffmpeg/lib`
echo "configure end!"
【保存并退出】
# chmod +x nginx_configure.sh
# ./nginx_configure.sh
# make && make install

http_flv_module和http_mp4_module即为对应的解析和seek功能支持。

2. 修改 conf/nginx.conf配置
# vim conf/nginx.conf
... 
worker_rlimit_nofile 51200;
events
{
  use epoll;
  worker_connections  51200;
}

# RTMP点播的配置
rtmp {
  server {
    listen 1935;
    chunk_size 4000;
    application vod {
      play /opt/pub/media/nginx/web/vod;  #点播媒体文件存放目录
    }
  }
}

http
{
  ... 
  # 配置RTMP虚拟机
  # VOD for FLV by RTMP
  server
  {
    listen 8080;
    location /stat
    {
      rtmp_stat all;
      rtmp_stat_stylesheet stat.xsl;
    }

    location /stat.xsl
    {
      root /opt/nginx_http_rtmp/nginx-rtmp-module/;   #在nginx-rtmp-module源码根目录
    }
  }
}

3. 启动Nginx
# /opt/nginx/sbin/nginx

4. 创建嵌入播放器的HTML页面
# cd /opt/pub/media/nginx/web
# vim rtmp_player.html

  
    
  


 

  


【保存并退出】

5. 测试
在浏览器输入栏中输入:
http://192.168.1.10/rtmp_player.html 

分类: 网络与安全

五、配置Nginx, 实现LIVE、以RTMP方式进行直播
1. 配置conf/nginx.conf
...
rtmp {
  server {
    listen 1935;
    chunk_size 4000;
    application vod {
      play /opt/pub/media/nginx/web/vod;
    }

    # Setting for LIVE
    application live {
      live on;
    }
  }
}
http
{
  ...
  # LIVE and VOD by RTMP
  server
  {
    listen 8080;
    location /stat
    {
      rtmp_stat all;
      rtmp_stat_stylesheet stat.xsl;
    }

    location /stat.xsl
    {
      root /opt/nginx_http_rtmp/nginx-rtmp-module/;
    }
  }
}

2. 重新加载Nginx
# /opt/nginx/sbin/nginx -s reload

3. 准备网页 rtmp_live.html
下载播放器jwplayer,并新建网页


  



 

  



4. 在浏览器输中输入
http://10.2.175.10/rtmp_live.html

5.  用nginx-rtmp-module自带的一个例子修改,在test/rtmp-publisher目录下player.html

  

  

  

Flash not installed

访问http://10.2.175.10/player.html


6. 使用ffmpeg推流
用ffmpeg产生一个模拟直播源,向rtmp服务器推送
# ./ffmpeg –i/usr/local/nginx/vod/flvs/a.flv-strict -2 -c:v libx264 -c:a aac -f flv rtmp://192.168.1.4/live/test

注意,
源文件必须是H.264+AAC编码的。
192.168.1.4是运行nginx的服务器IP,
live是applicatioin,
test是直播缓存流文件,需要与配置文件中的直播缓存文件名一样。

六、配置Nginx 支持HLS
暂时没有做测试,可见其它网文;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值