直播环境
直播环境



直播协议
流媒体协议
直播常用到的流媒体协议有 rtmp、http、hls、rtsp。
传输协议 | 播放器 | 延迟 | 内存 | CPU |
---|---|---|---|---|
RTMP | Flash | 1s | 430M | 11% |
HTTP-FLV | Video | 1s | 310M | 4.4% |
HLS | Video | 20s | 205M | 3% |
WebSocket-FLV | H5-FLV | <1s | 410M | 8% |
在支持浏览器的协议里,延迟排序是:
WebSocket-FLV<RTMP = HTTP-FLV < HLS
而性能排序恰好相反:
RTMP > =WebSocket-FLV >HTTP-FLV> HLS
也就是说延迟小的性能不好。
可以看出在浏览器里做直播,使用 HTTP-FLV 或者 websocket-flv 协议是不错的,性能优于 RTMP+Flash,延迟可以做到和 RTMP+Flash 一样甚至更好。
协议名称 | 优势 | 劣势 |
---|---|---|
rtmp | 实时性高:一般能做到 3 秒内。 支持加密:rtmpe 和 rtmps 为加密协议。 稳定性高:在 PC 平台上 flash 播放的最稳定方式是 rtmp,如果做 CDN 或者大中型集群分发,选择稳定性高的协议一定是必要的。 一般主流编码器都支持该协议。 | 协议复杂:开发者写起来累,效率也不行。 Cache 麻烦:流协议做缓存不方便。 |
hls | 性能好:和 http 一样。 穿墙:和 http 一样。 原生支持很好:iOS 上支持完美,Android 上支持差些。PC/flash 上现在也有各种 as 插件支持 HLS。 | 实时性差:与 ts 切片长度有关,大约 3 个切片长度时间的延迟,基本上 HLS 的延迟在 10 秒以上。 文件碎片:若分发 HLS,码流低,切片较小时,会导致太多的文件碎片 |
rtsp | 延迟低,一般都能够做到 500ms。 带宽好,时效率高。 倍速播放,主要是回放的时候提供的功能。 控制精准,任意选择播放点。 | 服务端实现复杂。 代理服务器弱:数量少,优化少。 无路由器防火墙穿透。 管流分离:需要 1-3 个通道。 |
网络流媒体协议之——HLS 概述
https://blog.csdn.net/fireroll/article/details/77658855
直播源制作
参考 https://www.jianshu.com/p/aa7f9e204a62
安装 ffmpeg
yum install -y yasm
#安装epel包
yum install -y epel-release
#导入签名
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
#导入签名
rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
#升级软件包
rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
#安装ffmpeg
yum -y install ffmpeg-libs
yum install -y ffmpeg
#检查版本
ffmpeg -version
添加 nginx-rtmp-module 模块
第一种方式
nginx 添加模块参考 https://zhidao.baidu.com/question/581069144.html
cd /usr/local
git clone http://github.com/wandenberg/nginx-rtmp-module.git
nginx -V
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-openssl=/root/lnmp1.5/src/openssl-1.0.2o --add-module=/usr/local/nginx-rtmp-module
make
第二种方式 lnmp 一键安装包环境
lnmp 添加模块参考 https://lnmp.org/faq/lnmp1-2-upgrade.html
lnmp 根目录下
vi lnmp.conf
Nginx_Modules_Options 后添加所需的模块
如:
Nginx_Modules_Options='--add-module=/usr/local/nginx-rtmp-module'
./upgrade.sh nginx
重启 nginx
etc/init.d/nginx restart
修改 nginx 配置
打开/usr/local/etc/nginx/nginx.conf 文件
添加 rtmp 模块
rtmp {
server{
listen 1935;
chunk_size 4000;
# RTMP 直播流配置
application rtmplive {
# 直播的模式
live on;
# 设置最大连接数
max_connections 1024;
}
# hls 直播流配置
application hls{
live on;
hls on;
hls_path /home/live/hls;
# 每个hls分片多大
hls_fragment 5s;
}
}
}
# 视频拉流
server {
listen 9005;
server_name localhost;
index index.html;
#root /home/wwwroot/live-pull;
#include rewrite/none.conf;
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
#配置hls
location /hls {
expires max;
# Disable cache
add_header 'Cache-Control' 'no-cache';
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
types {
application/dash+xml mpd;
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /home/live;
add_header Cache-Control no-cache;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
deny all;
}
access_log off;
}
- RTMP 推流
请使用 Item(Mac 系统) 或者 gitbash(windows 系统)
ffmpeg -re -i test.mp4 -vcodec libx264 -acodec aac -strict -2 -f flv rtmp://120.78.219.51:1935/rtmplive/rtmp
验证地址 rtmp://120.78.219.51:1935/rtmplive/rtmp // 需要使用播放插件 或专用播放器
- HLS 推流
请使用 Item(Mac 系统) 或者 gitbash(windows 系统)
ffmpeg -re -i test.mp4 -vcodec libx264 -acodec aac -strict -2 -f flv rtmp://120.78.219.51:1935/hls/stream
验证地址 http://120.78.219.51:9005/hls/stream.m3u8 // 支持苹果手机直接打开观看 其他需要使用播放插件或专用播放器
nodejs 直播服务器搭建
https://github.com/illuspas/Node-Media-Server/blob/master/README_CN.md
使用 ffmpeg 推流本地视频
ffmpeg 中文文档
ffmpeg -re -i /home/lee/video.mp4 -vcodec copy -acodec copy -b:v 800k -b:a 32k -f flv rtmp://localhost/hls/rtmp
-re : 表示使用文件的原始帧率进行读取,因为 ffmpeg 读取视频帧的速度很快,如果不使用这个参数,ffmpeg 可以在很短时间就把 video.mp4 中的视频帧全部读取完并进行推流
-i :这个参数表示输入 ,后面/home/lee/video.mp4 就是输入文件。
-vcodec copy : -vcodec 表示使用的视频编解码器 ,前缀 v 表示 video。后面紧跟的 copy 表示复制使用源文件的视频编解码器,比如原文件的编解码器(codec)是 h264,则这里就使用 h264。
-acodec copy : -acodec 表示使用的音频编解码器,前缀 a 表示 audio。后面的 copy 表示使用源文件的音频编解码器。
-b:v 800k : -b:v 表示视频的比特率(bitrate) ,为 800k。
-b:a 32k : 表示音频的比特率为 32k。
-f flv : -f 表示 format ,就是强制输出格式为 flv,这一步其实也叫封装(mux),封装要做的事就是把视频和音频混合在一起,进行同步。紧跟在后面的 rtmp://localhost/videotest 表示输出的"文件名",这个文件名可以是一个本地的文件,也可以指定为 rtmp 流媒体地址。指定为 rtmp 流媒体地址后,则 ffmpeg 就可以进行推流。