接上一篇用Windows向Ubuntu推流接流,这里用Windows向Windows推流接流,推流部分参考上一篇,这篇主要是Windows接流,篇幅太长了,主要是nginx+rtmp在Windows上编译太麻烦了
参考链接:
1.windows安装MinGW
MinGW - Minimalist GNU for Windows download | SourceForge.netDownload MinGW - Minimalist GNU for Windows for free. A native Windows port of the GNU Compiler Collection (GCC) This project is in the process of moving to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), with freely distributable import libraries and header files for building native Windows applications;https://sourceforge.net/projects/mingw/
建议都勾选了,免得后面出现缺少东西的情况
勾选完后apply changes,等待安装完成
默认装在C盘根目录,找到后双击msys.bat测试情况如下
2. 安装Mercurial
打开命令行输入hg,确认安装没问题
3.安装strawberryperl
一直next就行
4.安装nasm
5.下载nginx相关的包
(1)利用Mercurial下载nginx-win32-src
hg clone http://hg.nginx.org/nginx nginx-win32-src
(2)下载nginx-rtmp-module
https://github.com/arut/nginx-rtmp-modulehttps://github.com/arut/nginx-rtmp-module(3)下载openssl
Index of /opensslhttp://distfiles.macports.org/openssl/(4)下载zlib
zlib Home Sitehttp://www.zlib.net/
(5)下载pcre
现在pcre已经到pcre2了,官网有说明
http://www.pcre.org/http://www.pcre.org/官网链接转到下面下载
6.开始编译nginx
(1)将上面下载的5个压缩包解压放到MingGW的用户文件夹下,那个Administrator是我自己的用户名
(2)打开编译窗口,双击那个msys.bat
此时你用命令ls查看,发现你所在的位置正好在所有包的根目录,接下来你cd nginx-win32-src到你的nginx根目录去
在这个根目录手动创建一个configuration-nginx.bat文件,文件内容为
auto/configure --with-cc=cl --builddir=objs --prefix= \
--conf-path=conf/nginx.conf --pid-path=logs/nginx.pid \
--http-log-path=logs/access.log --error-log-path=logs/error.log \
--sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp \
--http-proxy-temp-path=temp/proxy_temp \
--http-fastcgi-temp-path=temp/fastcgi_temp \
--with-cc-opt=-DFD_SETSIZE=1024 \
--with-select_module \
--with-debug \
--with-http_ssl_module \
--with-pcre=../pcre2-pcre2-10.39 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.1.1l \
--add-module=../nginx-rtmp-module-1.2.2
下面这些路径换成自己的,由于上面的操作已经进入了nginx-win32-src文件夹,所以以 ../ 的方式给相对路径就行
运行configuration-nginx.bat发现报错,这个错误可能是因为每行末尾多了一个空格,删除掉再试试
再次运行后发现有个小错误
根据参考链接的提示,在msvc的第17行(版本不同可能位置不一样)加了NGX_MSVC_VER=15.00,据说不加也行的。
加了之后那句话果然不再提示
(3)编译nginx(需要用到VS,我用的是最新的2022)
2022下载地址,最好下Community2022,因为是免费的
Visual Studio 2022 IDE - 适用于软件开发人员的编程工具借助 Visual Studio 设计,具有自动完成、构建、调试、测试功能的代码将与 Git 管理和云部署融为一体。https://visualstudio.microsoft.com/zh-hans/vs/VS的安装的就不多说了,网上很多,没有装的去装一下
在VS有的情况下确认下有没有下面这个工具
我的全路径为:C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\usr\bin
打开
切换到nginx根目录,输入:nmake /f objs\MakeFile
报错提示nasm没有在环境变量里,添加了以下变量,包括nasm的,还有nmake的
其中nmake.exe的路径在,
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\bin\Hostx64\x64
接着编译发现错误
../nginx-rtmp-module-1.2.2/ngx_rtmp_core_module.c(611): error C2220: 以下警告被视为错误
../nginx-rtmp-module-1.2.2/ngx_rtmp_core_module.c(611): warning C4456: “sa”的声明隐藏了上一个本地声明
../nginx-rtmp-module-1.2.2/ngx_rtmp_core_module.c(506): note: 参见“sa”的声明
NMAKE : fatal error U1077: “"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\bin\HostX86\x86\cl.EXE"”: 返回代码“0x2”
Stop.
上面说警告被视为错误,去到下面的msvc文件84行(版本不一样可能不太一样)
把那个warning下面的CFLAGS="$CFLAGS -W4"改为CFLAGS="$CFLAGS -W3"
另外我上面报错的时候感觉是openssl版本太高我换了个openssl-1.0.2l,如果你们有人报错,也换一下试试吧
改了上面再试还是报同样的错误,网上还有说是注释rtmp的611行(上面错误提示是611行)
这下终于可以了,成功的样子如下
可以看到nginx下产生了很多文件
7.配置nginx
(1)在msys下新建一个nginx文件
里面放上面编译生成的下面两个文件
(2)拷贝源码下的conf文件过去
(3) 拷贝源码下docs文件夹中的html文件夹过去
(4)接着新建logs和temp两个文件夹,最终文件结构如下
8.启动带rtmp的nginx
打开conf/nginx.conf更改配置,我的配置如下
#user nobody;
worker_processes 2;
#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;
}
rtmp
{
server
{
listen 1992;
chunk_size 4096;
application live
{
live on;
}
access_log logs/rtmp_access.log;
}
}
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 8080;
location /stat{
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl{
root /html/nginx-rtmp-module;
}
}
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 = /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;
# }
#}
}
更改完毕后,在根目录打开命令行输入启动服务命令按enter后即可
start /b nginx.exe -c conf/nginx.conf
关闭命令
nginx.exe -s stop
启动后访问本机ip出现下面的页面说明启动成功
注意上面这步只是说明了带rtmp的nginx没问题,正确的配置在下面接流中,如果你不想验证了可以直接做下面这步,不过启动服务和关闭服务还是一样的
9.接流
根据最上面参考链接来
(1)在nginx/html/目录下新建一个文件夹 nginx-rtmp-module,将之前解压的nginx-rtmp-module目录下的stat.xsl文件以及test文件夹拷贝到新建的目录nginx-rtmp-module下
(2)修改nginx/html/nginx-rtmp-module/test/nginx.conf文件,关闭nginx的守护和master-worker模式,将nginx服务的访问目录修改为刚才nginx-rtmp-module中指向的index文件。再加入RTMP模块的配置,最终的配置文件内容如下:
worker_processes 2;
daemon off;
master_process off;
error_log logs/error.log info;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
application live {
live on;
}
}
}
http {
server {
listen 8080;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root html/nginx-rtmp-module/;
}
location /control {
rtmp_control all;
}
location /rtmp-publisher {
root html/nginx-rtmp-module/test;
}
location / {
root html/nginx-rtmp-module/test/www;
}
}
}
(3)配置拉流名字,修改nginx/html/nginx-rtmp-module/test/www/index.html文件,将 file: "rtmp://localhost/myapp?carg=1/mystream?sarg=2"
修改为 file: "rtmp://localhost/live?carg=1/test?sarg=2"
以保持和上面的配置文件一致
(4)配置推流名称,修改nginx/html/nginx-rtmp-module/test/www/record.html文件,将开始的 'streamer': 'rtmp://localhost/myapp',
修改为 'streamer': 'rtmp://localhost/live'
以保持和上面的配置文件一致
(5)在msys下进入nginx目录,执行 nginx.exe -c html/nginx-rtmp-module/test/nginx.conf
访问网页后出现下面的情况,我查了下,这个问题很麻烦,我换种方式,用python代码接流
注意,推流在上一篇已经说过了,在Windows上用ffmpeg推流,不知道的去看下
代码和上一篇博客用的一样,只是ip不一样了
import cv2
cap = cv2.VideoCapture("rtmp://192.168.41.132/live/test")
#ret,frame = cap.read()
#print(ret)
while cap.isOpened():
ret,frame = cap.read()
if ret:
cv2.imshow('frame',frame)
key=cv2.waitKey(delay=2)
if key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
接流的结果如下
可以接流说明没问题,主要问题在于浏览器或者是那个jwplayer.js插件有问题,也许是不兼容,用别的方式代替也行的,别纠结这里
下面给出我的用到的包
链接:https://pan.baidu.com/s/1zwmsi-mcEjwAJ--u1mykLw
提取码:y23v
--来自百度网盘超级会员V5的分享
另外编译好的可以在其它机子直接用,已经测试过了
雁过留声,人过留名,有用点赞,不要白嫖!