转自: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

05-07 15:29