搭建简单hls直播测试服务

来源:互联网 发布:知乎 南北战争的武器 编辑:程序博客网 时间:2024/06/03 02:54

经过实践,通过h5 video 直接m3u8直播, ios 是都没有问题的。android 4.2以上才基本上没问题。4.2以下的各品牌有些问题需要做不同的兼容。

HTTP Live Streaming(缩写是 HLS)是一个由苹果公司提出的基于HTTP的流媒体网络传输协议。是苹果公司QuickTime X和iPhone软件系统的一部分。它的工作原理是把整个流分成一个个小的基于HTTP的文件来下载,每次只下载一些。当媒体流正在播放时,客户端可以选择从许多不同的备用源中以不同的速率下载同样的资源,允许流媒体会话适应不同的数据速率。在开始一个流媒体会话时,客户端会下载一个包含元数据的extended M3U (m3u8) playlist文件,用于寻找可用的媒体流。

在测试直播各种兼容性时, 为了测试方便,可以自己搭建一个直播服务器来输入视频流供测试。

rtmp直播流会被动态切分为ts片段和一个不断刷新的u3m8文件, 这个正是h5直播时的方式,因此我们通过配置nginx 的rtmp 模块来支持 rtmp 流媒体直播服务。

主要以下几步, 一些具体的安装步骤就不细说了:

1. 安装强大的音视频转换工具ffmpeg , 相信你对这个不陌生.

2.安装nginx 以及 nginx-rtmp-module 模块(自行搜索下载)

进入你的nginx源码目录, 执行以下命令, 注意, 之前安装的模块如果有其他配置需要带上

./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/nginx-rtmp-module --with-debugmakemake install

 3.配置 nginx 以支持rtmp

在nginx.conf配置文件末尾加上以下配置:

rtmp{        server{                 listen 1935;                chunk_size      4000;                   # For HLS to work please create a directory in tmpfs (/tmp/app here)           # for the fragments. The directory contents is served via HTTP (see          # http{} section in config)          #               # Incoming stream must be in H264/AAC. For iPhones use baseline H264          # profile (see ffmpeg example).          # This example creates RTMP stream from movie ready for HLS:          #               # ffmpeg -loglevel verbose -re -i movie.avi  -vcodec libx264           #    -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1           #    -f flv rtmp://localhost:1935/hls/movie          #               # If you need to transcode live stream use 'exec' feature.          #                       application hls{                        live on;                        hls on;                         hls_path        /data/maxwellxwma/html5hls/html/hls;                        hls_fragment 5s;                }                       }       }

 这样配置好后, 我们就可以对当前服务器生成rtmp 直播流了,执行以下命令会不断向/data/maxwellxwma/html5hls/html/hls下面写入ts片段和m3u8文件:

./ffmpeg -re -i /data/maxwellxwma/src/t.flv -vcodec copy -acodec copy -f flv rtmp://10.6.224.185/hls/mystream

 如下图所示:

边生成边播放边删除:

4. 配置vhosts支持外部调用m3u8文件播放:

server{        listen 8080;        server_name html5hls.qq.com;        error_log /data/maxwellxwma/logs/nginx/hls.qq.com_error.log;        index index.html index.php;        root /data/maxwellxwma/html5hls/html;        location        /hls{                   #server HLS fragments                types{                          text/html html htm;                        #application/vnd.apple.mpegurl m3u8;                        application/x-mpegurl m3u8;                        video/mp2t ts;                }                       root /data/maxwellxwma/html5hls/html;                index index.html;                expires -1;        }       }

 这些配置可以直接加入到nginx.conf里, 但建立虚拟主机配置,增加可维护性

然后重启nginx  ,  直播服务就搭建好了。

用VLC播放器测试一下正常:

 

5. html5  video 标签嵌入播放测试:

<!DOCTYPE html><html><head><meta http-equiv="content-type" content="text/html; charset=utf-8">    <title>HLS Player</title></head><body><video id="video" src="http://10.6.224.185:8080/hls/mystream.m3u8" width="100%" heigh="100%" autoplay="autoplay" controls="controls">不支持videos</video></body></html> 

 

m3u8文件记录了待播放的ts列表:

好了, 到此终于可以随心随地随时,随心所欲的测试直播了, 还有那些非常蛋疼的安卓兼容 性问题

1 0