转自: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上写的很清楚。

  1. ./configure --add-module=<path-to-nginx-rtmp-module>
  2. make
  3. make install

1.3.14-1.5.0版本

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

2,rtmp源的提供

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

例如:

  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配置文件,那么只需要用

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

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

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

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

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

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

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

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

  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

转: 基于nginx的hls直播系统的更多相关文章

  1. 在Windows下搭建基于nginx的视频直播和点播系统

    http://my.oschina.net/gaga/blog/478480 一.软件准备 由于nginx原生是为linux服务的,因此官方并没有编译好的windows版本可以下载,要在windows ...

  2. 使用ffmpeg搭建HLS直播系统

    [时间:2018-04] [状态:Open] [关键词:流媒体,stream,HLS, ffmpeg,live,直播,点播, nginx, ssegment] 0 引言 本文作为HLS综述的后续文章. ...

  3. Centos7 搭建Nginx+rtmp+hls直播推流服务器

    1 准备工具 使用yum安装git [root~]# yum -y install git 下载nginx-rtmp-module,官方github地址 // 通过git clone 的方式下载到服务 ...

  4. 基于nginx的rtmp直播服务器(nginx-rtmp-module实现)

    首先,在搭建服务之前先了解下目前主流的几个直播协议: 1.RTMP: 实时消息传输协议,Real Time Messaging Protocol,是 Adobe Systems 公司为 Flash 播 ...

  5. 基于SRS+OBS搭建直播系统

    这段时间与视频,直播相关的技术不可谓不热,今天我们就近距离接触下,尽早搭上这班车! 我们先看一张效果图 左边是OBS 推流端,右边是VLC播放器,稍微有延迟! 本文是基于VMware(12.5.7)+ ...

  6. 基于nginx的HLS简单服务器搭建

    一,首先搭建nginx服务器: 1.1,选定源码目录 选定目录 /usr/local/HLS cd /usr/local/HLS 1.2,安装PCRE库 cd /usr/local/HLS 到www. ...

  7. 基于nginx+tomcat部署商城系统并连接数据库

    需三台服务器nginx 192.168.200.111tomcat 192.168.200.112tomcat 192.168.200.113 192.168.200.111[root@localho ...

  8. 实时监控、直播流、流媒体、视频网站开发方案流媒体服务器搭建及配置详解:使用nginx搭建rtmp直播、rtmp点播、,hls直播服务配置详解

    注意:这里不会讲到nginx流媒体模块如何安装的问题,只研究rtmp,hls直播和录制相关的nginx服务器配置文件的详细用法和说明.可以对照这些命令详解配置nginx -rtmp服务 一.nginx ...

  9. Ubuntu 14.10下基于Nginx搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具)

    Ubuntu 14.10下基于Nginx搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具) 最近因为项目关系,收朋友之托,想制作秀场网站,但是因为之前一直没有涉及到这 ...

随机推荐

  1. UVALive 7281 Saint John Festival (凸包+O(logn)判断点在凸多边形内)

    Saint John Festival 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/J Description Porto's ...

  2. C# 索引器 学习

    转载原地址: http://www.cnblogs.com/lxblog/p/3940261.html 1.索引器(Indexer): 索引器允许类或者结构的实例按照与数组相同的方式进行索引.索引器类 ...

  3. oracle对序列的操作

    select t.*, t.rowid from tbl_type t order by t.id desc Select SEQ_TBL_TYPE_ID.NextVal From Dual; ; ; ...

  4. 用 JavaScript 修改样式元素

    利用 <style> 元素,我们可以在网页中嵌入样式表.如果需要动态增加 <style> 元素,似乎可以用如下的 JavaScript 代码: var style = docu ...

  5. c# 递归算法

    c# 递归算法 2009-03-13 09:44 6950人阅读 评论(8) 收藏 举报 算法c#funn2c 1)1.1.2.3.5.8.......用递归算法求第30位数的值? 首先我们可以发现从 ...

  6. java读取properties的工具类PropertiesUtil

    package org.properties.util; import java.io.FileInputStream; import java.io.FileOutputStream; import ...

  7. c#与java webservice调用问题

    问题描述一: c#调用java写的websrevice,对方接收到的参数与实际传的值不一致,返回的时候值也获取不到,为null. 该参数为普通的string类型,因此不存在类型转换的问题. 处理: 使 ...

  8. 简单的玩玩etimer <contiki学习笔记之九 补充>

    这幅图片是对前面  <<contiki学习笔记之九>>  的一个补充说明. 简单的玩玩etimer <contiki学习笔记之九> 或许,自己正在掀开contiki ...

  9. 正则表达式从右往左进行匹配(Regex)

    #匹配最末两位为数字 $x=New-Object regex ('\d{2}','RightToLeft') #$x.RightToLeft $x.Match('abcd22') 结果:

  10. JQuery Plugin 1 - Simple Plugin

    1. How do you write a plugin in jQuery? You can extend the existing jQuery object by writing either ...