基于nginx+jwplayer的点播服务器(测试成功)

来源:互联网 发布:北大黎小夏 知乎 编辑:程序博客网 时间:2024/05/28 15:49

环境:虚拟机Ubuntu14,网络请使用桥接模式。

参考:http://jingyan.baidu.com/article/4e5b3e1957979d91901e24f1.html

如果桥接模式还不能联网要在windows下把VirtualBox Host-Only Network的虚拟网卡和其他虚拟机的虚拟网卡都要禁掉,这个问题困扰了我2个小时。

有问题请在评论下回复,thx for...


1、ffmpeg 安装:

http://blog.csdn.net/u010694337/article/details/51335790

其中a52库是没安装的,有些模块编译过程是有问题的,编译错误问题直接百度关键词,基本上都能找到答案。

2、 nginx安装:

一 准备

搭建点播服务器需要如下几个模块:

  • nginx_mod_h264_streaming: 使nginx支持h264编码的视频
  • http_flv_module: 支持flv
  • http_mp4_module: 支持mp4
  • nginx-rtmp-module: 支持rtmp协议

其中http_flv_module和http_mp4_module两个模块是nginx自带的, 可以在编译的时候加上相应的选项.

nginx_mod_h264_streaming的下载地址: http://h264.code-shop.com/trac/wiki/Mod-H264-Streaming-Nginx-Version2

nginx-rtmp-module托管在GitHub上: https://github.com/arut/nginx-rtmp-module

注意在GitHub的"Build"部分有这样一句话:

Several versions of nginx (1.3.14 - 1.5.0) require http_ssl_module to be added as well:

1
./configure --add-module=<path-to-nginx-rtmp-module> --with-http_ssl_module

我用的nginx版本是1.4.1, 所以在configure的时候需要加上"--with-http_ssl_module"选项, 否则会出现错误:

1
2
3
4
5
src/http/ngx_http_request.c: 在函数‘ngx_http_set_virtual_server’中:
src/http/ngx_http_request.c:1992:9: 错误: 未知的类型名‘ngx_http_ssl_srv_conf_t’
src/http/ngx_http_request.c:1999:16: 错误: ‘ngx_http_ssl_module’未声明(在此函数内第一次使用)
src/http/ngx_http_request.c:1999:16: 附注: 每个未声明的标识符在其出现的函数内只报告一次
src/http/ngx_http_request.c:2001:17: 错误: 在非结构或联合中请求成员‘verify’

ssl的安装可以参考网上教程, 也可以参考 [1] . 注意Ubuntu环境下SSL库是libssl-dev:

sudo apt-get install libssl-dev


   

 二 安装Nginx及其相关模块

(Nginx使用的是Perl正则表达式, 所以需要首先安装PCRE, 读者能够从网上找到PCRE的安装过程, 在此不赘述. )

# mkdir nginx

# cd nginx

# cp ../nginx-1.4.1.tar.gz ../nginx_mod_h264_streaming-2.2.7.tar.gz ../nginx-rtmp-module-master.zip ./

#  tar -zxvf nginx-1.4.1.tar.gz                                   (同样地将其余两个包解压缩)

# cd nginx-1.4.1

# ./configure --prefix=/usr/local/nginx --add-module=../nginx_mod_h264_streaming-2.2.7 --add-module=../nginx-rtmp-module-master/ --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_stub_status_module --with-cc-opt="-I/usr/local/ffmpeg2/include" --with-ld-opt="-L/usr/local/ffmpeg2/lib"

# make

这个时候出现了错误:

1
2
3
4
5
6
7
8
9
10
../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"), 然后就能够正常编译了.

# make

# make install

至此nginx安装成功了.

3、修改nginx配置文件nginx.conf

配置文件一般在 /usr/local/nginx/conf/nginx.conf 。这里对代码作下简单说明,配置除掉了rtmp模块,其中最重要的是http模块,root html,代表的是./html文件夹,由于服务器的根目录是/usr/local/nginx,所以就是/usr/local/nginx/html文件夹,详细配置文件说明请查看陶辉的《深入理解nginx模块》第二章,有配置说明。

  #filename:nginx.conf
    #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  1024;
    }

http {
        include mime.types;
        default_type application/octet-stream;
        sendfile on;#系统调用,优化,磁盘直接发到网卡
        keepalive_timeout 65;
        gzip on;

         #log format
        log_format  access  ‘$remote_addr – $remote_user [$time_local] “$request” ‘
                 ‘$status $body_bytes_sent “$http_referer” ‘
                 ‘”$http_user_agent” $http_x_forwarded_for’;
         #定义一个名为addr的limit_zone,大小10M内存来存储session
        limit_conn_zone $binary_remote_addr zone=addr:10m;

   server {
            listen 80;
             server_name localhost;

             location / {
                    root html;
                    index index.html jwplayer.html;
                  }

            location ~* \.flv$ {
                 root /mnt/media/vod;
                 flv;
                 limit_conn addr 20;
                 limit_rate 2000k;
            }

            location ~* \.mp4$ {
                 root /mnt/media/vod;
                 mp4;
                 limit_conn addr 20;
                 limit_rate 2000k;           }
                    

   }

             access_log  logs/nginxflv_access.log access;
}

4 flv文件转码和元数据的注入

使用ffmpeg转码后的flv文件需要注入元数据(metadata)之后才能实现拖放, metadata中主要是关键帧. 我使用yamdi实现元数据的注入. yamdi为flv文件增加了很多metadata信息,比如创建者、是否有关键帧、是否有视频、是否有音频,视频高度和宽度等等。而yamdi加入的meta数据中,最有效的要数关键帧。被注入了关键帧的flv可以实现像土豆网、优酷网等大型视频网站一样的“拖进度”,提前拖到缓冲还未加载到的位置开始播放。另一个可用的工具是flvtool2, 网上一个文章提供了两者的用法和比较: http://blog.csdn.net/zhangxh1013/article/details/5896482.

yamdi的安装也很简单:

(1) 下载: http://yamdi.sourceforge.net/

(2) # tar -zxvf yamdi-1.9.tar.gz

(3) # cd yamdi-1.9

(4) # make & make install

元数据注入:

1
yamdi -i input.flv -o output.flv

 

输出的文件使用ffmpeg查看, 可以看到这样的信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Metadata:
   creator         : m_8efd9a8eb9c1c7434e76fc0e27052f91_md Tudou, Inc.
   metadatacreator : Tudou Metadata Injector for FLV - Version 1.0
   hasKeyframes    : true
   hasVideo        : true
   hasAudio        : true
   hasMetadata     : true
   canSeekToEnd    : false
   datasize        : 14485828
   videosize       : 13399710
   audiosize       : 1041346
   lasttimestamp   : 251
   lastkeyframetimestamp: 245
   lastkeyframelocation: 14458306
   encoder         : Lavf55.12.100

 这说明元数据注入成功, 并且输出文件中含有关键帧.

当然, 在ffmpeg转码的同时也可以向flv文件中加入关键帧, 只需加入 "-keyint_min" 参数. ffmpeg文档中对此的解释是:

1
2
3
‘keyint_min integer (encoding,video)’
 
    Set minimum interval between IDR-frames.      (设定IDR帧之间的最小间隔)

 

但是用ffmpeg转码的方法添加元数据仍然不能对视频随意拖放, 用ffmpeg可以看到转码后的视频的元数据信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ffmpeg -i output.flv
......
 Metadata:
    creator         : m_8efd9a8eb9c1c7434e76fc0e27052f91_md Tudou, Inc.
    metadatacreator : Tudou Metadata Injector for FLV - Version 1.0
    hasKeyframes    : true
    hasVideo        : true
    hasAudio        : true
    hasMetadata     : true
    canSeekToEnd    : false
    datasize        : 14485828
    videosize       : 13399710
    audiosize       : 1041346
    lasttimestamp   : 251
    lastkeyframetimestamp: 245
    lastkeyframelocation: 14458306
    encoder         : Lavf55.12.100

 它与使用yamdi方法产生的结果的位于区别是"canSeekToEnd"为false, 为什么这样的flv视频不能


5、 jwplayer播放器的设定

通过以上四个步骤, flv视频点播服务器已经基本搭建成功了. 下面要使用jwplayer播放视频, 检验以上的设置是否成功. 编写以下PHP文档并在浏览器中打开:

网上下载jwplayer播放器后解压到/usr/local/nginx/html文件夹下面,jwplayer文件夹下面的swf文件名修改与flashplayer: "/jwplayer/player.swf"的player.swf一致,讲output.flv文件放入html文件夹下就可以播放了,并讲下面的配置文件命名jwplayer3.html,打开nginx, 输入链接127.0.0.1/jwplayer3.html。

 <script type="text/javascript" src="/jwplayer/jwplayer.js"></script>
 <div id="container">Loading the player ...</div>
    <script type="text/javascript">
       jwplayer("container").setup({
                          flashplayer: "/jwplayer/player.swf",
                          file: "output.flv",
                          startparam: "start",
                          height: 270,
                          width: 480
      });
   </script>



6、后话

看了两天jwplayer的官方api介绍,一头雾水。搜了很多demo,大多已经是过时的版本,并不是很实用。而且最大的缺点是在手机上不能播放,找了一下午也没搜到个什么,请知道的朋友不妨告知一下这个jwplayer是个强大的开源软件,很多附加功能待研究。nginx配置有很多可以优化的地方,以及可加上直播的功能等等。这个链接下有并发测试http://5iqiong.blog.51cto.com/2999926/1132639。

7、参考

http://blog.chinaunix.net/uid-25723371-id-3320065.html

http://hdu104.com/294

http://www.cnblogs.com/wanghetao/archive/2013/11/11/3418744.html

1 0
原创粉丝点击