基于nginx的hls直播系统

来源:互联网 发布:淘宝如何把好评改差评 编辑:程序博客网 时间:2024/05/18 16:15

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

之前写了一篇基于nginx的hls点播系统,本质上是把一个媒体文件做成m3u8索引,对应的文件都是提前做好放在服务器上的。

nginx充当的是个Http 服务器的角色,之所以说是基于nginx的,是因为它可以设置限速。

本文主要是描述一个直播系统,核心在于m3u8和里面对于的ts链接都是实时的,可以刷新。类似于cntv里面的直播。

这里分按顺序分几个部分讲述:软件编译,rtmp源的提供,nginx配置,html代码修改,客户端播放。

1,软件编译:

所需模块:nginx-rtmp-module

github:

https://github.com/arut/nginx-rtmp-module#example-nginxconf

 这个模块对nginx的版本好像没有什么要求,我用1.2.2是可以的。编译方法github上写的很清楚。

 

[plain] view plaincopy
  1. ./configure --add-module=<path-to-nginx-rtmp-module>  
  2. make  
  3. make install  

1.3.14-1.5.0版本

[plain] view plaincopy
  1. ./configure --add-module=<path-to-nginx-rtmp-module> --with-http_ssl_module  


2,rtmp源的提供

一类是用一个已有的媒体文件,一类是用摄像头和麦克风采集。

例如:

[plain] view plaincopy
  1. ffmpeg.exe -re -i sample.flv -vcodec copy -acodec copy -f flv rtmp://server-ip-address/hls/mystream  
  2. ffmpeg.exe -f dshow -i video="USB2.0 Camera" -vcodec libx264 -pix_fmt yuv420p -f flv rtmp://server-ip-address/hls/mystream  

第一个是基于一个媒体文件的,必须用re,标识native frame rate,意思是按照播放的帧率。

第二个是基于dshow的,在windows上,编码用x264,图像用420p,

两种方式都是以rtmp协议发给server,其中hls和mystream各有含义。hls表示application,mystream表示一个实例。稍后解释。

 

3,nginx配置

这个nginx-rtmp-module里面已经包含了一个nginx.conf,位于test目录下,如果你已经有了一个nginx配置文件,那么只需要用

[plain] view plaincopy
  1. include  <path-to-nginx-rtmp-module>/test/nginx.conf;  

即可包含这个新配置,Include必须与现有配置平级,即http级别的。

这里通常会有些问题,例如rtmp不能识别。

[plain] view plaincopy
  1. unknown directive "?rtmp  
  2. unknown directive "rtmp" in /etc/nginx/conf.d/rtmp.conf:1  

解决方法一般是两种,一个是新conf的编码必须是和原有的一样,一般都是ASCII的,用file指令就知道。

一是重新编译后的nginx的model没有加载进去,可以尝试stop nginx再start就行。

[plain] view plaincopy
  1. rtmp {  
  2.     server {  
  3.         listen 1935;  
  4.   
  5.         application myapp {  
  6.             live on;  
  7.   
  8.             #record keyframes;  
  9.             #record_path /tmp;  
  10.             #record_max_size 128K;  
  11.             #record_interval 30s;  
  12.             #record_suffix .this.is.flv;  
  13.   
  14.             #on_publish http://localhost:8080/publish;  
  15.             #on_play http://localhost:8080/play;  
  16.             #on_record_done http://localhost:8080/record_done;  
  17.   
  18.        }  
  19.        application hls {  
  20.              live on;  
  21.              hls on;  
  22.              hls_path /tmp/app;  
  23.              hls_fragment 5s;  
  24.   
  25.   
  26.        }  
  27.     }  
  28. }  
  29.   
  30. http {  
  31.     server {  
  32.         listen      8080;  
  33.   
  34.         location /stat {  
  35.             rtmp_stat all;  
  36.             rtmp_stat_stylesheet stat.xsl;  
  37.         }  
  38.   
  39.         location /stat.xsl {  
  40.             root <path-to-nginx-rtmp-module>;  
  41.         }  
  42.   
  43.         location /control {  
  44.             rtmp_control all;  
  45.         }  
  46.   
  47.         #location /publish {  
  48.         #    return 201;  
  49.         #}  
  50.   
  51.         #location /play {  
  52.         #    return 202;  
  53.         #}  
  54.   
  55.         #location /record_done {  
  56.         #    return 203;  
  57.         #}  
  58.   
  59.         location /rtmp-publisher {  
  60.             root <path-to-nginx-rtmp-module>/test;  
  61.         }  
  62.   
  63.         location /hls {  
  64.            #server hls fragments  
  65.            types{  
  66.              application/vnd.apple.mpegurl m3u8;  
  67.              video/mp2t ts;  
  68.            }  
  69.         alias /tmp/app;  
  70.         expires -1;  
  71.         }  
  72.   
  73.         location / {  
  74.             root <path-to-nginx-rtmp-module>/test/rtmp-publisher;  
  75.         }  
  76.   
  77.     }  
  78. }  


简单解释:application中app是rtmp直播的,就是flash用的。我这里没有用。有个player.html在test目录下就是为这个服务的。

hls是hls直播的。是我这里用的。

/tmp/app是一个目录,是用来存放实时刷新的m3u8里面的文件的。这个文件刷新时间大约是1分钟。老文件会不断的用新文件取代。

4,html代码修改

nginx-rtmp-module里面已经包含了一个测试html,player.html,那个是播放flash用的。我们这里为播放Hls,可以简单的修改如下。

命名为playhls.html

[plain] view plaincopy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>HLS Player</title>  
  5. </head>  
  6. <body>  
  7. <video height="270" width="480" controls>  
  8.     <source src="http://server-ip-address:8080/hls/mystream.m3u8" type="application/vnd.apple.mpegurl" />  
  9.     <p class="warning">Your browser does not support HTML5 video.</p>  
  10. </video>  
  11. </body>  
  12. </html>  


5 ,客户端播放

浏览器一般还不支持m3u8直接播放,因为这个是H5才有的。

在android手机端,我们可以用QQ浏览器最新版本去播放网页。

在ios设备上,我们可以用Iphone,ipad去播放,因为这个HLS本来就是apple的,所以它的safari天然支持

PC机上我们可以用ffplayer去播放。
 
http://server-ip-address:8080/hls/mystream.m3u8

如果能播,则浏览器的地址为
http://server-ip-address:8080/hls/playhls.html

6,状态查看
http://server-ip-address:8080/stat

 参考页面:
1,rtmp配置

http://yeyingxian.blog.163.com/blog/static/34471242012916050362/

2,ffmpeg 抓取设备

http://ffmpeg.org/trac/ffmpeg/wiki/How%20to%20capture%20a%20webcam%20input

http://ffmpeg.org/trac/ffmpeg/wiki/StreamingGuide

http://ffmpeg.gusari.org/viewtopic.php?f=11&t=841

 这里说一下,在windows7 上,声音设备的名字往往有中文字符,例如"麦克风(High Definition Audio设备)"

这个中文在ffmpeg下调用dshow是不能用的。所以我采集的是纯视频+音频(0 channels),即没有声音的视频。

audio的采集

  /dev/snd
http://man.chinaunix.net/linux/how/Alsa-sound-5.html
linux 音频驱动“
http://yiranwuqing.iteye.com/blog/1840176


版权声明:本文为博主原创文章,未经博主允许不得转载。

0

0 0