利用ffmpeg工具基于nginx-rtmp-module模块搭建HLS


FFMPEG:A complete, cross-platform solution to record, convert and stream audio and video.

HLS:HTTP Live Streaming Apple 动态码率自适应技术,主要用于PC和Apple端的音视频服务.包括一个m3u(8)的索引文件,ts分片文件和key加密串文件.

Nginx: 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。

nginx-rtmp-module:基于nginx的媒体流服务模块

1.ubuntu环境准备,sudo apt-get update && sudo apt-get install libpcre3 libpcre3-dev
2.下载nginx-rtmp-module模块,sudo git clone https://github.com/arut/nginx-rtmp-module.git
3.安装nginx-rtmp-module模块
  sudo ./configure --add-module=/home/liangf/nginx-rtmp-module --with-http_ssl_module

  sudo make

       sudo make install 
4.配置nginx的conf文件(nginx.conf),增加rtmp模块配置及hls Server,所增加的主要内容如下:
#=============================================================================
#nginx-rtmp-module config 该模块配置于http模块同级
rtmp {
  server {
    listen 1935;
    chunk_size 4000;
    application hls {
      live on;
      hls on;
      hls_path /path/to/hls/m3u8;
      hls_fragment 5s;
    }
  }
}
#=============================================================================
#rtsp server 该配置为http模块下的服务,可独立创建实例server,亦可放置于某个server配置中,注意区别
#rtsp server
server { #所为实例配置时需要
  listen 7010;#所为实例配置时需要
  server_name localhost;#所为实例配置时需要
  #hls server path
  location /hls {
    #Server HLS fragements
    types {
      application/vnd.apple.mpegurl m3u8;
      video/mp2t ts;
    }
    alias /path/to/hls/m3u8/;
    add_header Cache-Control no-cache;
  }
}

5.通过ffmpeg工具推流,其中流有两种类型的源头:一类是已有文件(如mp4文件等);另一类是设备(摄像头及麦克风采集)
  针对第一种方式:
  ffmpeg -re -i /path/to/hls/test.mp4 -vcodec copy -acodec copy -f flv rtmp://localhost:1935/hls/real,该命令将在rtmp模块所配置的路径下生成real.m3u8文件.注:基于媒体文件,必须用re来标识native frame rate,来约定照播放的帧率.
  针对第二种方式:
  ffmpeg -f dshow -i video="USB2.0 Camera" -vcodec libx264 -pix_fmt yuv420p -f flv rtmp://localhost:1935/hls/real.注:dshow是基于windows上的,编码用x264,图像用420p.
6.客户端访问,两种访问方式:1)基于播放器(vcl)访问:http://localhost:7010/hls/real.m3u8 ;2)基于h5网页访问:
<!DOCTYPE html>
<html>
<head>
<title>HLS Player</title>
</head>
<body>
  <video height="270" width="480" controls>
    <source src="http://server-ip-address:8080/hls/real.m3u8" type="application/vnd.apple.mpegurl" />
    <p class="warning">Your browser does not support HTML5 video.</p>
  </video>
</body>
</html>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM