基于NGINX的HLS直播服务搭建

nginx配合ffmpeg做流媒体服务器的原理是:nginx通过rtmp模块提供rtmp服务, ffmpeg推送一个rtmp流到nginx, 然后客户端通过访问nginx来收看实时视频流. HLS也是差不多的原理,只是rtmp是直接推送到nginx,hls是通过暂存ts切片推送,类似点播的直播,最终客户端是通过HTTP协议来访问的,但是ffmpeg推送流仍然是rtmp的.
为了使能尽可能多的功能,重新编译nginx和ffmpeg,下面是编译成功的nginx编译选项,这一步不是必须的,可以通过apt-get或yum安装

1、nginx-1.8.1.tar.gz源码 http://nginx.org/download/nginx-1.8.1.tar.gz
2、nginx_mod_h264_streaming-2.2.7.tar.gz
http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
3、nginx-rtmp-module.zip
https://codeload.github.com/arut/nginx-rtmp-module/zip/master
4、openssl-0.9.8e.tar.gz
https://www.openssl.org/source/old/0.9.x/openssl-0.9.8e.tar.gz
解压nginx-1.8.1.tar.gz
5、ffmpeg-3.2.4.tar.bz2
http://ffmpeg.org/releases/ffmpeg-3.2.4.tar.bz2
cd nginx-1.8.1

./configure \
--prefix=/usr/local \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx --group=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-mail \
--with-mail_ssl_module \
--with-ipv6 \
--with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector' \
--add-module=/usr/local/src/nginx-rtmp-module-master \
--add-module=/usr/local/src/nginx_mod_h264_streaming-2.2.7 \
--with-openssl=/usr/local/src/openssl-0.9.8e;

修改/etc/nginx/nginx.conf使能rtmp和hls支持直播和点播,其中8080为直播端口,1935为推送端口

#user  www www;
worker_processes 1;
#error_log  /usr/local/nginx/logs/error.log  crit;
#pid        /usr/local/nginx/logs/nginx.pid;
 
events {
        use epoll;
        worker_connections      1024;
        }
rtmp {  
        server {  
            listen 1935;  
            application myapp {  
                live on;  
            }  
            application hls {  
                live on;  
                hls on;  
                hls_path /var/www/hls;
				hls_fragment 5s;
            }  
        }  
} 
http {
        include       mime.types;
        default_type  application/octet-stream;
        log_format main  '$remote_addr - $remote_user [$time_local] '
                                                '"$request" $status $bytes_sent '
                                               '"$http_referer" "$http_user_agent" '
                                               '"$gzip_ratio"';
       #keepalive_timeout  60;
        #server_names_hash_bucket_size  128;
        #client_header_buffer_size    32k;
        #large_client_header_buffers  4 32k;
        #access_log off;
        #gzip on;
        #gzip_min_length  1100;
        #gzip_buffers     4 8k;
        #gzip_types       text/plain;
 
        #output_buffers   1 32k;
        #postpone_output  1460;
 
        #client_header_timeout  3m;
        #client_body_timeout    3m;
        #send_timeout           3m;
 
        #sendfile                on;
        #tcp_nopush              on;
        #tcp_nodelay             on;
 
######################################################################
 
server {
        listen       80;
        server_name  localhost;
		#192.168.0.1;
        root    /var/www/html/mywebsite/;
        #limit_rate_after 5m;    ####在flv视频文件下载了5M以后开始限速
        #limit_rate 512k;         ####速度限制为512K
		index   index.html;
        charset utf-8;
        location ~ \.flv {
            flv;
        }
        location ~ \.mp4$ {
                 mp4;
        }
       error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /var/www/html;
        }
}
server {
		listen		8080;
		location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root /usr/local/src/nginx-rtmp-module-master/;
        }

        location /control {
            rtmp_control all;
        }
		
       location /rtmp-publisher {
            root /usr/local/src/nginx-rtmp-module-master/test;
        }
	   index  playhls.html;
       location /hls {  
           #server hls fragments  
           types{  
             application/vnd.apple.mpegurl m3u8;  
             video/mp2t ts;  
           }  
#root /var/www; 
		alias /var/www/hls;###hls文件暂存路径
        expires -1;  
        }  
        location / {
           root /var/www/html;
      }                    
	}
}

下面是ffmpeg-3.2.4编译选项配置,如果失败请安装对应的lib
./configure --prefix=/usr/local --extra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/arm-linux-gnueabihf --incdir=/usr/include/arm-linux-gnueabihf --cc=cc --cxx=g++ --enable-gpl --enable-shared --disable-stripping --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 –enable-libopencv

hls推送测试 ffmpeg -re -i sample.flv -vcodec copy -acodec copy -f flv rtmp://server-ip-address/hls/livehls
监控测试ffmpeg -f dshow -i video=“USB2.0 Camera” -vcodec libx264 -pix_fmt yuv420p -f flv rtmp://server-ip-address/hls/livehls

在/var/www/html创建播放索引文件playhls.html:

<!DOCTYPE html>  
<html>  
<head>  
    <meta http-equiv="content-type" content="text/html; charset=utf-8">  
    <title>LIVE</title>  
</head>  
<body>  
<video poster="poster.png" height="480" width="720" controls>  
    <source src="http://server-ip-address:8080/hls/livehls.m3u8" type="application/vnd.apple.mpegurl" />  
    <p class="warning">Your browser does not support HTML5 video.</p>  
</video>  
</body>  
</html>  

用支持hls的浏览器打开http://server-ip-address:8080
就可以观看直播,如果不行,测试http://server-ip-address:8080/hls/livehls.m3u8

推荐大家一个靠谱的论文检测平台。重复的部分有详细出处以及具体修改意见,能直接在文章上做修改,全部改完一键下载就搞定了。怕麻烦的话,还能用它自带的降重功能。哦对了,他们现在正在做毕业季活动, 赠送很多免费字数,可以说是十分划算了!地址是:http://www.paperpass.com/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值