转: 基于nginx的hls直播系统
转自: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上写的很清楚。
- ./configure --add-module=<path-to-nginx-rtmp-module>
- make
- make install
1.3.14-1.5.0版本
- ./configure --add-module=<path-to-nginx-rtmp-module> --with-http_ssl_module
2,rtmp源的提供
一类是用一个已有的媒体文件,一类是用摄像头和麦克风采集。
例如:
- 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配置文件,那么只需要用
- include <path-to-nginx-rtmp-module>/test/nginx.conf;
即可包含这个新配置,Include必须与现有配置平级,即http级别的。
这里通常会有些问题,例如rtmp不能识别。
- unknown directive "?rtmp
- unknown directive "rtmp" in /etc/nginx/conf.d/rtmp.conf:1
解决方法一般是两种,一个是新conf的编码必须是和原有的一样,一般都是ASCII的,用file指令就知道。
一是重新编译后的nginx的model没有加载进去,可以尝试stop nginx再start就行。
- 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
- <!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
转: 基于nginx的hls直播系统的更多相关文章
- 在Windows下搭建基于nginx的视频直播和点播系统
http://my.oschina.net/gaga/blog/478480 一.软件准备 由于nginx原生是为linux服务的,因此官方并没有编译好的windows版本可以下载,要在windows ...
- 使用ffmpeg搭建HLS直播系统
[时间:2018-04] [状态:Open] [关键词:流媒体,stream,HLS, ffmpeg,live,直播,点播, nginx, ssegment] 0 引言 本文作为HLS综述的后续文章. ...
- Centos7 搭建Nginx+rtmp+hls直播推流服务器
1 准备工具 使用yum安装git [root~]# yum -y install git 下载nginx-rtmp-module,官方github地址 // 通过git clone 的方式下载到服务 ...
- 基于nginx的rtmp直播服务器(nginx-rtmp-module实现)
首先,在搭建服务之前先了解下目前主流的几个直播协议: 1.RTMP: 实时消息传输协议,Real Time Messaging Protocol,是 Adobe Systems 公司为 Flash 播 ...
- 基于SRS+OBS搭建直播系统
这段时间与视频,直播相关的技术不可谓不热,今天我们就近距离接触下,尽早搭上这班车! 我们先看一张效果图 左边是OBS 推流端,右边是VLC播放器,稍微有延迟! 本文是基于VMware(12.5.7)+ ...
- 基于nginx的HLS简单服务器搭建
一,首先搭建nginx服务器: 1.1,选定源码目录 选定目录 /usr/local/HLS cd /usr/local/HLS 1.2,安装PCRE库 cd /usr/local/HLS 到www. ...
- 基于nginx+tomcat部署商城系统并连接数据库
需三台服务器nginx 192.168.200.111tomcat 192.168.200.112tomcat 192.168.200.113 192.168.200.111[root@localho ...
- 实时监控、直播流、流媒体、视频网站开发方案流媒体服务器搭建及配置详解:使用nginx搭建rtmp直播、rtmp点播、,hls直播服务配置详解
注意:这里不会讲到nginx流媒体模块如何安装的问题,只研究rtmp,hls直播和录制相关的nginx服务器配置文件的详细用法和说明.可以对照这些命令详解配置nginx -rtmp服务 一.nginx ...
- Ubuntu 14.10下基于Nginx搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具)
Ubuntu 14.10下基于Nginx搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具) 最近因为项目关系,收朋友之托,想制作秀场网站,但是因为之前一直没有涉及到这 ...
随机推荐
- HDU 5787 K-wolf Number (数位DP)
K-wolf Number 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5787 Description Alice thinks an integ ...
- UOJ Test Round #2
昨天晚上打的这个比赛,简直一颗赛艇啊-- 感觉发挥的并不好.比赛的时候比较紧张,最后一题还脑残写了个离散化结果爆零了,哎我怎么这么逗逼-- 讲讲比赛经过吧. 比赛之前逗逼地以为是8:00开始,然后淡定 ...
- Starter Set of Functional Interfaces
Java Development Kit 8 has a number of functional interfaces. Here we review the starter set-the int ...
- Mysql中关于 group_concat函数详解
group_concat()主要功能:能将相同的行组合起来 完整的语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Sepa ...
- ALV的报表对用户定义格式的控制(ALV I_SAVE)
很多ALV的报表都需要手动的进行设置格式以使数据看上去更有意义和条理,如果每次进来都重新操作一遍是很烦人的,所以SAP有提供了一个保存格式的功能,保存格式可以是 '缺省设置' 和 '特定用户' 两种 ...
- PostgreSQL的 initdb 源代码分析之十一
继续分析: /* Top level PG_VERSION is checked by bootstrapper, so make it first */ write_version_file(NUL ...
- Struts2内建校验器(基于校验框架的文件校验)
位于xwork-2.0.4.jar压缩包中( com.opensymphony.xwork2.validator.validators)有个文件default.xml ,该文件中定义了Struts2框 ...
- 直接下载Google Play市场的APK
传送门在这里:http://apps.evozi.com/apk-downloader/ 似乎很方便.很迅速的样子,忍不住在这里记录一下.
- Codeforces Beta Round #51 A. Flea travel 水题
A. Flea travel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/problem ...
- HDU 5514 Frogs 容斥定理
Frogs Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5514 De ...