zoukankan      html  css  js  c++  java
  • nginx上搭建HLS流媒体服务器

    http://blog.csdn.net/cjsafty/article/details/7922849

    简介:HTTP Live Streaming(缩写是 HLS)是一个由苹果公司提出的基于HTTP的流媒体 网络传输协议。

    是苹果公司QuickTime X和iPhone软件系统的一部分。它的工作原理是把整个流分成一个个小的基于HTTP

    的文件来下载,每次只下载一些。当媒体流正在播放时,客户端可以选择从许多不同的备用源中以不同的速

    率下载同样的资源,允许流媒体会话适应不同的数据速率。在开始一个流媒体会话时,客户端会下载一个包

    含元数据的extended M3U (m3u8) playlist文件,用于寻找可用的媒体流。


    HLS只请求基本的HTTP报文,与实时传输协议(RTP)不同,HLS可以穿过任何允许HTTP数据通过的防

    火墙或者代理服务器。它也很容易使用内容分发网络来传输媒体流。

    此协议详细内容请参考apple官方网站:https://developer.apple.com/resources/http-streaming/

    有两种方式搭建HLSserver,

    一种是利用apple SDK,

    一种是利用adobe 的fms,4.5版本支持hls,参考,

    http://www.adobe.com/products/flash-media-streaming/features._sl_id-contentfilter_sl_featuredisplaytypes_sl_new.html

    adobe的fms现在很强大,但是商用需要licence。有兴趣的可以研究下。

    一种是利用opensouce.我比较喜欢这一种。

    方法:

    opensource的方法主要是使用m3u8-segmenter+ffmpeg对ts文件进行分片。

    因此思路就是:

    1,用编译好的ffmpeg制作所需要的ts文件,

    2,安装libavformat-dev版本,

    3,编译m3u8-segmenter,

    4,部署到nginx

    5,高级功能,流切换

    6,页面

    过程

    1,本来想下载ffmpeg源码编译,但是因为要涉及到faac,x264,lame库。有时候ffmpeg版本对这些库的版本又有最低版本要求,在编译

    faac时候遇到以下问题

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. 安装支持库  
    2. apt-get install automake autoconf m4 libtool  
    3.   
    4. -bash: ./bootstrap: /bin/sh^M: bad interpreter: No such file or directory  
    5.   
    6. 转换字符:  
    7. dos2unix bootstrap  
    8. make  
    9.   
    10. 错误:mpeg4ip.h:126:58: error: new declaration ‘char* strcasestr(const char*, const char*)’  
    11.   
    12. 解决方法:Remove line 126 containing strcasecmp from mpeg4ip.h as a temporary workaround  
    13.   
    14. make install时遇到  
    15. usr/local/share/man/man1文件夹无法创建问题。  


    最后一个问题无法解决,好像是linux(ubuntu)下同一个目录下,如果已经有一个文件了,则不能创建同名文件夹,遂放弃编译,

    直接从ffmpeg网站:http://ffmpeg.org/download.html ,的linux下载页面下载编译好ffmpeg静态文件。这个静态文件的主要目的是

    为了把各种文件转换成apple所规定的文件。所以需要AAC,mp3,x264库支持。

    或者干脆按照2的方法。apt-get install ffmpeg.这样会得到ffmpeg可行性文件。

    2,安装ffmpeg支持库,主要用于编译m3u8-segmenter,这里的ffmpeg支持库,其目的是给segmenter提供libavformat支持。不涉及编解码。

    apt-get install libavformat-dev.

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. Reading package lists... Done  
    2. Building dependency tree         
    3. Reading state information... Done  
    4. The following extra packages will be installed:  
    5.   libavcodec-dev libavcodec53 libavformat53 libavutil-dev libavutil51 libgsm1 libogg0 liborc-0.4-0  
    6.   libschroedinger-1.0-0 libspeex1 libtheora0 libva1 libvorbis0a libvorbisenc2 libvpx1  
    7. Suggested packages:  
    8.   libfaad-dev libgsm1-dev libogg-dev libschroedinger-dev libspeex-dev libtheora-dev libvorbis-dev libx11-dev  
    9.   libxext-dev libraw1394-dev libdc1394-22-dev speex  
    10. The following NEW packages will be installed:  
    11.   libavcodec-dev libavcodec53 libavformat-dev libavformat53 libavutil-dev libavutil51 libgsm1 libogg0 liborc-0.4-0  
    12.   libschroedinger-1.0-0 libspeex1 libtheora0 libva1 libvorbis0a libvorbisenc2 libvpx1  


    这样会自动安装ffmepg几个相关库。

    3,从https://github.com/johnf/m3u8-segmenter 下载m3u8-segmenter

    下载后不要用它的反复编译,直接取m3u8-segmenter.c文件,

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. gcc -Wall -g segmenter.c -o segmenter -lavformat  

    从源码来看,因为只用到了avformat库,所以只链接这一个即可。生成segmenter文件,用help命令,可以看到已经成功。

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. HTTP Live Streaming - Segments TS file and creates M3U8 index.  
    2.         -i, --input FILE             TS file to segment (Use - for stdin)  
    3.         -d, --duration SECONDS       Duration of each segment (default: 10 seconds)  
    4.         -p, --output-prefix PREFIX   Prefix for the TS segments, will be appended  
    5.                                      with -1.ts, -2.ts etc  
    6.         -m, --m3u8-file FILE         M3U8 output filename  
    7.         -u, --url-prefix PREFIX      Prefix for web address of segments, e.g. http://example.org/video/  
    8.         -n, --num-segment NUMBER     Number of segments to keep on disk  
    9.         -h, --help                   This help  


    从上图来看,语法很简单,这里贴一个我用的。

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. ./segmenter -i test.ts -n 30 -p sample_test -m stream-test.m3u8 -u http://192.168.1.10:8080/hls/  


    i表示输入文件,n表示切割30个,p表示切割文件的前缀。m表示生成的m3u8文件名,u表示这些切割后的文件处于web server的哪个目录下,这个一定要和web目录匹配

    4,部署到nginx。

    nginx的相关部署我在前两个博客中已经详细说明,这里在jwplayer博客的基础上部署hls。

    1)目录问题:

    在html/jwplayer目录下,建立hls文件夹,将m3u8文件和切割后的全部ts文件拷贝到此目录下,

    在VLC PLAYER或者ipad safie浏览器或者在ffplayer(我用的是0.11版本的windows编译版本)

    上的访问路径应该是http://192.168.1.10:8080/hls/stream-test.m3u8

    2)文件类型问题:编辑 /usr/local/nginx/conf/mime.types 文件,添加如下类型

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. application/x-mpegURL                 m3u8;  
    2. video/MP2T                             ts;   

    3)重启nginx 

    输入上述路径,你应该就看到视频了。

    5,高级功能,流切换

    上述m3u8文件,只有一个流,不具备流切换功能。在优酷上,如果是ipad客户端,可以看到有标清,高清,超清的按钮,其实那个是对应着不同标准

    的(单个)m3u8文件,来实现流切换的,不知道apple是不是这样做的,apple好像是要求“智能”流切换。即不要求用户去选择,而是根据网络状况自适应的。

    apple给的sample的流切换是把各个流的m3u8写在一个m3u8文件里实现的。

    类似于这样,其实原理是一样的。

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. #EXTM3U  
    2. #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=100000  
    3. video1/index1.m3u8  
    4. #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=200000  
    5. video2/index2.m3u8  


    6,页面,

    如果再继续搭建一个页面,把上述地址嵌在页面里面,这样配合CSS就比较美观了。页面可以在这个基础上,用webpy去做。



    页面参考:

    苹果开发网:https://developer.apple.com/resources/http-streaming/

    博客:http://www.nginxs.com/linux/459.html

    ffmpeg开发网:http://ffmpeg.org/download.html

    segmenter源码:https://github.com/johnf/m3u8-segmenter

    adboe fms介绍:http://www.adobe.com/products/flash-media-streaming/features._sl_id-contentfilter_sl_featuredisplaytypes_sl_new.html


     

    2014.02备注:

    文中所描述的切片软件可能有问题,可选择这个

    https://code.google.com/p/httpsegmenter/downloads/list

    //--------------------------------------------------------------------------------

    目的:使Nginx支持Rtmp协议推流,并支持HLS分发功能及FFmpeg转码多码率功能。

    一、准备工作
    模块:nginx-rtmp-module-master(支持rtmp协议)
    下载地址:
    http://nginx.org
    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 gcc-c++

    2、安装git工具:
    #mkdir soft-source
    #cd soft-source
    #wget http://www.codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.gz
    #tar xzvf git-latest.tar.gz 
    #cd git-2013-02-04
    #autoconf
    #./configure
    #make && make install
    # git --version
    git version 1.8.1.GIT
    #cd ..

    【错误处理】

    如果 git-latest.tar.gz大小为0,请下载git-latest-tar.xz

    然后xz -d git-latest.tar.xz解压为.tar

    再tar xvf git-latest.tar

    3、安装ffmpeg及其依赖包:
    ++++++++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 ..
    ++++++++x264+++++++++++
    #git clone git://git.videolan.org/x264
    #cd x264
    #./configure --enable-shared 
    #make
    #make install
    #cd ..

    ++++++++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 ..
    ++++++++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 ..
    ++++++++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 ..
    ++++++++libvpx+++++++++++
    #git clone http://git.chromium.org/webm/libvpx.git
    #cd libvpx
    #./configure  --enable-shared
    #make
    #make install
    #cd ..
    ++++++++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 ..
    ++++++++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 ..

    【错误处理】

    编译FAAC-1.28时遇到错误:

    mpeg4ip.h:126: error: new declaration ‘char* strcasestr(const char*, const char*)’

    解决方法:

    从123行开始修改此文件mpeg4ip.h,到129行结束。
    修改前:
    #ifdef __cplusplus
    extern "C" {
    #endif
    char *strcasestr(const char *haystack, const char *needle);
    #ifdef __cplusplus
    }
    #endif

    修改后:
    #ifdef __cplusplus
    extern "C++" {
    #endif
    const char *strcasestr(const char *haystack, const char *needle);
    #ifdef __cplusplus
    }
    #endif


    ++++++++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 ../../../


    ++++++++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 ..
    【错误处理】

    如果提示libvpx decoder version must be >=0.91,请从Baidu搜索一下libvpx-v1.1.0.tar.bz下载。

    bzip2 -d  libvpx-v1.1.0.tar.bz2

    tar xvf  libvpx-v1.1.0.tar.bz2

    cd libvpx-v1.1.0

    ./configure --enable-shared --enable-vp8

    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

    【说明】

    动态装入器找到共享库要依靠两个文件 — /etc/ld.so.conf 和 /etc/ld.so.cache。

    安装完成后,ffmpeg位于/opt/ffmpeg/bin目录下。

    Linux下编译FFmpeg之下载源文件并编译 http://www.linuxidc.com/Linux/2012-02/54565.htm

    Linux 编译升级 FFmpeg 步骤 http://www.linuxidc.com/Linux/2013-08/88190.htm

    CentOS 5.6 上安装 FFMPEG http://www.linuxidc.com/Linux/2011-09/42793.htm

    Ubuntu下安装FFmpeg http://www.linuxidc.com/Linux/2012-12/75408.htm

    Ubuntu 12.04下编译ffmpeg  http://www.linuxidc.com/Linux/2013-02/78857.htm

    Ubuntu 14.04下PPA安装FFmpeg 2.2.2  http://www.linuxidc.com/Linux/2014-05/101322.htm

    更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2015-01/111182p2.htm

  • 相关阅读:
    1093 Count PAT's(25 分)
    1089 Insert or Merge(25 分)
    1088 Rational Arithmetic(20 分)
    1081 Rational Sum(20 分)
    1069 The Black Hole of Numbers(20 分)
    1059 Prime Factors(25 分)
    1050 String Subtraction (20)
    根据生日计算员工年龄
    动态获取当前日期和时间
    对计数结果进行4舍5入
  • 原文地址:https://www.cnblogs.com/jukan/p/5240486.html
Copyright © 2011-2022 走看看