您的位置:首页 > 运维架构 > Nginx

转: 基于nginx的hls直播系统

2016-05-23 16:20 579 查看
转自:http://blog.csdn.net/cjsafty/article/details/9108587

看点:

1. 详细解解答了 nginx rtmp配置过程。

前写了一篇基于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 plain copy

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

make

make install

1.3.14-1.5.0版本

[plain] view plain copy

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

2,rtmp源的提供

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

例如:

[plain] view plain copy

ffmpeg.exe -re -i sample.flv -vcodec copy -acodec copy -f flv rtmp://server-ip-address/hls/mystream

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 plain copy

include <path-to-nginx-rtmp-module>/test/nginx.conf;

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

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

[plain] view plain copy

unknown directive "?rtmp

unknown directive "rtmp" in /etc/nginx/conf.d/rtmp.conf:1

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

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

[plain] view plain copy

rtmp {

server {

listen 1935;

application myapp {

live on;

#record keyframes;

#record_path /tmp;

#record_max_size 128K;

#record_interval 30s;

#record_suffix .this.is.flv;

#on_publish http://localhost:8080/publish;
#on_play http://localhost:8080/play;
#on_record_done http://localhost:8080/record_done;
}

application hls {

live on;

hls on;

hls_path /tmp/app;

hls_fragment 5s;

}

}

}

http {

server {

listen 8080;

location /stat {

rtmp_stat all;

rtmp_stat_stylesheet stat.xsl;

}

location /stat.xsl {

root <path-to-nginx-rtmp-module>;

}

location /control {

rtmp_control all;

}

#location /publish {

# return 201;

#}

#location /play {

# return 202;

#}

#location /record_done {

# return 203;

#}

location /rtmp-publisher {

root <path-to-nginx-rtmp-module>/test;

}

location /hls {

#server hls fragments

types{

application/vnd.apple.mpegurl m3u8;

video/mp2t ts;

}

alias /tmp/app;

expires -1;

}

location / {

root <path-to-nginx-rtmp-module>/test/rtmp-publisher;

}

}

}

简单解释: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 plain copy

<!DOCTYPE html>

<html>

<head>

<title>HLS Player</title>

</head>

<body>

<video height="270" width="480" controls>

<source src="http://server-ip-address:8080/hls/mystream.m3u8" type="application/vnd.apple.mpegurl" />

<p class="warning">Your browser does not support HTML5 video.</p>

</video>

</body>

</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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: