首先放上rtmp的git地址:https://github.com/arut/nginx-rtmp-module,里面也有教程。
lnmp 1.3以上版本的话可以直接编辑lnmp.conf 文件 Nginx_Modules_Options=” 单引号中加上 –add-module=/usr/local/nginx/extend_module/nginx-rtmp-module 保存,如果版本较低建议升级到最新版本,官方也出了1.x升级到1.4的教程。
cd /root/
git clone https://github.com/arut/nginx-rtmp-module
再./upgrade.sh nginx 升级一下nginx就行了。升级后可输入nginx -V查看是否正确安装。
lnmp 根目录下
1 lnmp.conf
Nginx_Modules_Options 后添加所需的模块
如:
Nginx_Modules_Options=’–add-module=/usr/local/nginx/extend_module/nginx-rtmp-module’
2./upgrade.sh nginx
参考lnmp 官方连接
https://lnmp.org/faq/lnmp1-2-upgrade.html
—————————————————————————————————————–
https://www.cnblogs.com/Archger/p/12774656.html
—————————————————————————————————————–
网上很多mac安装FFmpeg的方法都是使用 brew install 命令,就国内的网络环境而言,很难下载成功。
下面我来介绍下更简单的方法:
1、前往ffmpeg官网,下载页面
http://www.ffmpeg.org/download.html
2、选择苹果图标,进入如下链接,选择你需要的版本进行下载
3、网速好的话几秒钟就可成功,解压即可
4、前往命令行终端,执行ffmpeg命令(需要进入到安装目录,也可以配置到环境变量中)
————————————————
版权声明:本文为CSDN博主「YuePeng+」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43963725/article/details/113592856
—————————————————————————————————————–
https://live.qcloud.com/dy/jwplayer.html 腾讯直播测试url
—————————————————————————————————————–
https://live.qcloud.com/dy/test.html 腾讯直播测试网址1
<!doctype html>
URL:
—————————————————————————————————————–
—————————————————————————————————————–
https://www.jianshu.com/p/32417d8ee5b6
在开始之前,我先把流媒体服务中的双端关系说一下。在一个完整的流媒体服务框架中,角色就是”两端加一服”。推流端、拉流端加上媒体服务器。
同时按照应用场景的不同,协议又分:
- 推流协议;
- 拉流播放协议;
大部分教程在介绍这三个协议的时候,都忽略了一点,就是协议的应用场景到底是什么? RTMP 可以用在双端,但 HLS 只能用在拉流端,记住这层关系。
带着问题找答案:为什么RTMP比HLS快?
首先,这个问题发生在拉流端,协议也都是拉流协议。分别对RTMP和HLS的拉流播放进行抓包,能得到以下两张截图。


通过报文数据我们能看出:
• 在RTMP下,从Handshake到第一个VideoData用了700ms的时间;
• 在HLS下,从Get m3u8到response ts Data只有300ms!
问题来了,HLS的响应效率这么高,怎么就比RTMP还慢了呢?这都要从HLS的实现方案说起。

在上图的生产环境中,以RTMP协议推流,HLS拉流。端到端的时间消耗是:
- RTMP 推流端的联通成本是 700ms ,注意此时的 700 毫秒包含了 connect 和 send Video Data ;
- 推流端联通之后的时间成本,主要是采集编码封包的成本,不需要再次connect;
- HLS的请求响应成本是300ms;
- flvto ts的成本,用ffmpeg切一个10秒的码率500的视频,算上磁盘的写入时间最多 200ms;
所以说,HLS的慢的原因只有一个,就是等数据!

以 demo 中的这个 m3u8 来说,在直播的环境里媒体服务器要等到这 12 秒的数据推上来,我才有可能输出。即使切片成本降为零,拉流端看到的数据也是12秒之前的内容。
能不能优化?能!
缩短ts间隔与个数,HLS也能做到3秒+的延迟。但这个结果也拼不过RTMP这种不需切片的解决方案。
第三、怎么选
当您真正了解这三个协议之后,对于选择的问题我相信您已经有了自己的答案。我写这篇博客的初衷,也是想以历史背景入手,避免一上来就抛概念、甩结果。
希望我写的这些内容对您能有帮助,视频解决方案很复杂,它涉及的内容太多,在学习之初建立起清晰的脉络至关重要。为他人易,为自己难!祝大家在多媒体服务上,选你所选,爱你所爱。

—————————————————————————————————————–
—————————————————————————————————————–
—————————————————————————————————————–
—————————————————————————————————————–
—————————————————————————————————————–
—————————————————————————————————————–
—————————————————————————————————————–
—————————————————————————————————————–
原文: http://www.jianshu.com/p/0296a7be7928
关于更多rtmp的参数可以参考:https://github.com/arut/nginx-rtmp-module/wiki
https://github.com/arut/nginx-rtmp-module/wiki/Examples
https://github.com/arut/nginx-rtmp-module/wiki/Directives
1、Nginx环境搭建(基于lnmp环境)
lnmp环境一键安装脚本
按照👆教程安装!
//下载并安装lnmp环境
wget -c http://soft.vpser.net/lnmp/lnmp1.3.tar.gz && tar zxf lnmp1.3.tar.gz && cd lnmp1.3 && ./install.sh lnmp
安装完成后访问服务器地址会出现如下界面
PS:安装时生成的解压文件夹lnmp1.3先别删除!!!
2、安装Nginx的扩展rtmp模块
//下载扩展包
wget https://github.com/arut/nginx-rtmp-module/archive/v1.1.10.tar.gz
//解压扩展包
tar -xzvf v1.1.10.tar.gz
//为nginx创建扩展模块目录
mkdir /usr/local/nginx/extend_module
//将解压后的nginx-rtmp-module目录移动到nginx扩展模块目录下
mv nginx-rtmp-module-1.1.10/ /usr/local/nginx/extend_module/nginx-rtmp-module
nginx -V //查看nginx配置参数
复制configure arguments:后的所有参数!
例如:--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-ipv6 --with-http_sub_module
关闭nginx、php-fpm服务
//关闭nginx
service nginx stop
//关闭php-fpm
service php-fpm stop
进入安装时生成的文件夹:lnmp1.3/lnmp1.3/src
//解压nginx源码包
tar -xzvf nginx-1.10.0.tar.gz
//进入nginx源码目录
cd nginx-1.10.0
//安装rtmp扩展模块
./configure 刚才复制的nginx configure参数 --add-module=rtmp扩展包目录
如:
./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-ipv6 --with-http_sub_module --add-module=/usr/local/nginx/extend_module/nginx-rtmp-module
//编译
make
//安装
make install
//重启nginx
service nginx start
service php-fpm start
//查看安装状态
nginx -V
!上图如果出现nginx-rtmp-module说明安装成功!
3、配置rtmp
进入cd /usr/local/nginx/conf 目录
//编辑配置
vim nginx.conf
#尾部加入
rtmp {
server {
listen 1935; #监听的端口
chunk_size 4000;
application hls { #rtmp推流请求路径
live on;
record off;
}
}
}
参数说明:
rtmp是协议名称
server 说明内部中是服务器相关配置
listen 监听的端口号, rtmp协议的默认端口号是1935
application 访问的应用路径是 hls
live on; 开启实时
record off; 不记录数据
保存修改后:nginx -s reload //重载下nginx配置
4、服务器开放1935端口
(如服务器防火墙已关闭跳过此步)
//开放1935端口
/sbin/iptables -I INPUT -p tcp --dport 1935 -j ACCEPT
//保存配置
/etc/rc.d/init.d/iptables save
//重启服务
/etc/rc.d/init.d/iptables restart
//查看端口开放状态
/etc/init.d/iptables status
5、本地安装推流工具(ffmepg)及rtmp播放器(VLC)测试
ffmepg安装:brew install ffmpeg
VLC播放器下载:VLC
//ffmepg推流(本地准备一个视频文件)
ffmpeg -re -i 本地视频文件的绝对路径(如/Users/flycorn/Downloads/demo-hls.mp4) -vcodec copy -f flv rtmp://服务器IP:1935/hls/test
// 如:
ffmpeg -re -i /Users/flycorn/Downloads/demo-hls.mp4 -vcodec copy -f flv rtmp://服务器IP:1935/hls/test
PS:
如推流时出现
Connection to tcp://服务器IP:1935 failed: Connection refused
请先执行:
nginx -s reload
如不成功执行:
service nginx reload
推流界面如下:
开始推流后,打开VLC播放器验证结果
用VLC打开串流媒体–>打开网络串流–>输入rtmp://ip:1935/hls/test–>打开效果如下
5、配置HLS
进入/usr/local/nginx/conf 目录
//编辑配置
vim nginx.conf
//修改rtmp配置
rtmp {
server {
listen 1935; #监听的端口
chunk_size 4000;
application hls { #rtmp推流请求路径
live on;
hls on;
hls_path /home/hls/test; #视频流文件目录(自己创建)
hls_fragment 3s;
}
}
}
//修改server的配置
server
{
listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
server_name www.lnmp.org;
index index.html index.htm index.php;
root /home/wwwroot/default;
#error_page 404 /404.html;
include enable-php.conf;
#加入hls支持
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
#或 application/x-mpegURL
video/mp2t ts;
}
alias /home/hls/test/; #视频流文件目录(自己创建)
expires -1;
add_header Cache-Control no-cache;
}
#end...
#以下代码省略.....
}
进入/home目录并创建 hls及其子目录test
配置改完后执行nginx -s reload
在/home/wwwroot/default目录下创建test.html文件
//test.html
<html>
<body>
<video autoplay webkit-playsinline controls>
<source src="http://服务器IP/hls/test.m3u8" type="application/vnd.apple.mpegurl" />
<p class="warning">Your browser does not support HTML5 video.</p>
</video>
<video controls>
<source src="http://服务器IP/hls/test.m3u8" type="application/x-mpegURL">
</video>
</body>
</html>
使用ffmpeg进行推流:
ffmpeg -re -i /Users/flycorn/Downloads/demo-hls.mp4 -vcodec copy -f flv rtmp://服务器IP:1935/hls/test
此时服务器上的/home/hls/test/目录下会生成许多ts文件及test.m3u8文件!
使用Safari浏览器访问 http://服务器IP/test.html
如直播没有声音,请参考ffmpeg没有声音!~
PS:浏览器对hls的支持
也可使用第三方插件实现对hls的支持!如videojs-contrib-hls
作者:flycorn 链接:http://www.jianshu.com/p/0296a7be7928 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
nginx-rtmp-module默认所有客户端都可以推流,那么问题就来了,怎么限制客户端推流呢?nginx-rtmp-module提供了限制发布者以及观看者的IP地址:
allow publish 127.0.0.1;
deny publish all;
allow play 192.168.0.0/24;
deny play all;
显然IP地址限制,达不到需求。
但是可以通过在模块配置文件中设置 on_publish
nginx.conf
rtmp {
server {
listen 1935;
ping 30s;
notify_method get;
application myapp {
live on;
on_publish http://localhost:8080/on_publish.php;
}
}
然后在on_publish.php中做我们的处理。例如:
on_publish.php
<?php
// ?user=user&pass=pass
$user = isset($_GET['user']) ? $_GET['user'] : '';
$pass = isset($_GET['pass']) ? $_GET['pass'] : '';
if (empty($user) || empty($pass)) {
echo "wrong query input";
header('HTTP/1.0 404 Not Found');
exit();
}
$saveuser = user;
$savepass = pass;
if (strcmp($user, $saveuser) == 0 && strcmp($pass, $savepass) == 0) {
echo "Username and Password OK";
} else {
echo "Username or Password wrong";
header('HTTP/1.0 404 Not Found');
exit();
}
?>
此代码简单的作了用户验证,也可以改成带数据库验证以及加入更多参数进行验证。
客服端推流设定
以Open Broadcaster Software(OBS)为例:
1、FMS URL:
rtmp://localhost:1935/myapp
- 1
2、播放路径/串码流(如果存在):
test?user=username&pass=password
- 1
其中test为streamkey,?user=user&pass=pass用来验证的
以上是推流权限设定,同理我们可以设置播放流时的权限限制
播放流时我们可以通过设置 on_play来限定,具体实现跟设置on_publish类似。也可以参考nginx-rtmp的官方wiki:
https://github.com/arut/nginx-rtmp-module/wiki/Directives
编辑nginx.conf文件,我贴出整个配置文件:
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; #==========浏览器监视直播流信息配置============== location /stat { #第二处添加的location字段。 rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { #第二处添加的location字段。 root /usr/local/nginx/nginx-rtmp-module/; #nginx-rtmp-module是模块下载的位置 }
#==========浏览器监视直播流信息配置结束============== location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } rtmp { server { listen 1935; chunk_size 4096; application live { live on; publish_notify on; on_publish http://192.168.0.199/rtmp/index.php; //授权验证地址 record off; } } }
index.php
<?php /* $_POST返回数组值 array ( 'app' => 'live', 'flashver' => 'FMLE/3.0 (compatible; FMSc/1.0)', 'swfurl' => 'rtmp://192.168.0.115/live', 'tcurl' => 'rtmp://192.168.0.115/live', 'pageurl' => '', 'addr' => '192.168.0.199', 'clientid' => '79', 'call' => 'publish', 'name' => 'test3', 'type' => 'live', 'pass' => '123456', ) */ $name = $_POST['name']; #$name = test3 $pass = $_POST['pass']; $pass = 123456 if(empty($name) || empty($pass)){ echo "串码流不正确"; // 这个是thinkphp5的返回头信息的函数 header("HTTP/1.1 404 Not Found"); header("Status: 404 Not Found"); exit; } echo "正常"; ?>
用ffmpeg推流方式:
ffmpeg -re -i 1255199778_3032750917_20170326113341.mp4 -c copy -f flv rtmp://192.168.0.115/live/test4?pass=123456
用obs推流方式:
test3相当于账号,pass相当于密码
检测直播流信息 nginx.conf配置方式
location /stat { #第二处添加的location字段。 rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { #第二处添加的location字段。 root /usr/local/nginx/nginx-rtmp-module/; #nginx-rtmp-module是模块下载的位置 }
配置完成后,在浏览器输入:http://192.168.0.115/stat
关于nginx-stmp模块的回调接口参考:http://www.ptbird.cn/nginx-rtmp-multi-channel.html
关于ffmpeg相关命令 :http://www.xuebuyuan.com/1740527.html