windows系统编译nginx并添加rtmp依赖教程,结合ffmpeg推流给nginx供浏览器页面查看视频
一、环境准备
1、安装perl
安装地址: https://strawberryperl.com/, 选择msi安装程序安装即可。
https://github.com/StrawberryPerl/Perl-Dist-Strawberry/releases/download/SP_54001_64bit_UCRT/strawberry-perl-5.40.0.1-64bit.msi
2、安装sed for windows
从 sourceforge 上下载安装程序。
https://sourceforge.net/projects/gnuwin32/files/sed/
执行安装程序结束后,需要将安装包bin目录配置到环境变量下。
https://nchc.dl.sourceforge.net/project/gnuwin32/sed/4.2.1/sed-4.2.1-setup.exe?viasf=1
C:\Program Files (x86)\GnuWin32\bin
3、安装MSYS2
参考 https://www.msys2.org/ 下 installation 内容下载安装即可。
https://github.com/msys2/msys2-installer/releases/download/2025-02-21/msys2-x86_64-20250221.exe
使用的命令行名称是:MSYS2 MSYS (命令行界面,在这里进行命令操作,不是在windows系统自带的cmd命令行里操作) =>开始菜单程序中找到这个
4、安装visual studio 2022环境
选择下载安装 Visual Studio 2022 社区版
地址:https://visualstudio.microsoft.com/zh-hans/vs/ ,主要使用其中的nmake环境。开发组件选择:C++桌面开发
nmake命令在Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\lib\x64
文件夹底下,可以打开看到,需要配置环境变量,否则找不到nmake命令
在系统变量Path中添加如下四个路径
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\bin\Hostx64\x64 (包含cl.exe文件)
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\bin\Hostx64\x86 (包含ml.exe文件)
C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE
C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools
新建用户变量LIB
并添加如下路径
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\lib\x64
新建用户变量INCLUDE
并添加如下路径
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\include
验证是否能查到nmake
按Windows+R->cmd进入命令行,可分别输入“cl”,“ml”,“link”,'nmake'进行测试。
如不成功则重启电脑再次测试。如配置成功,cmd输入nmake则会出现:
C:\Users\Administrator>nmake
Microsoft (R) 程序维护实用工具 14.43.34809.0 版
版权所有 (C) Microsoft Corporation。 保留所有权利。
NMAKE : fatal error U1064: 未找到 MAKEFILE 并且未指定目标
Stop.
5、下载源码、依赖库
1、ngninx源码,下载地址:http://hg.nginx.org/nginx 左侧导航找到zip,点击下载,文件名:nginx-2e63bf0a7821.zip (1.27.5版本)
2、pcre2 源码下载地址:https://www.pcre.org/(注:部分版本需要下载pcre https://sourceforge.net/projects/pcre/)
https://github.com/PCRE2Project/pcre2/releases ==》pcre2-10.45.tar.gz
3、zlib 下载地址: https://zlib.net/
https://github.com/madler/zlib/releases ==》zlib-1.3.1.tar.gz
4、openssl 下载地址:https://www.openssl.org/
https://openssl-library.org/source/ ==》openssl-3.4.1.tar.gz
5、nginx-http-flv-module-1.2.12.tar.gz 下载地址:https://github.com/winshining/nginx-http-flv-module/releases ==》nginx-http-flv-module-1.2.12.tar.gz
在这里不使用nginx-rtmp-module,nginx-http-flv-module包含nginx-rtmp-module提供的所有功能
nginx-http-flv-module 的其他功能与 nginx-rtmp-module的对比:
功能 | nginx-http-flv-module | nginx-rtmp-module | 备注 |
---|---|---|---|
HTTP-FLV (播放) | √ | x | 支持 HTTPS-FLV 和 chunked 回复 |
GOP 缓存 | √ | x | |
虚拟主机 | √ | x | |
省略 listen 配置项 |
√ | 见备注 | 配置中必须有一个 listen |
RTMP/HTTP-FLV 纯音频支持 | √ | 见备注 | wait_video 或 wait_key 开启后无法工作 |
HLS 单轨支持 | √ | x | |
reuseport 支持 |
√ | x | |
定时打印访问记录 | √ | x | |
JSON 风格的数据信息 | √ | x | |
录制的数据信息 | √ | x | |
大小端无关 | √ | 见备注 | big-endian 分支部分支持 |
二、编译
1、使用MSYS2 中解压依赖库
$ cd d:
# 事先把这个压缩包解压了 nginx-2e63bf0a7821.zip
$ cd nginx-2e63bf0a7821
# 修改错误级别为3级,nginx-2e63bf0a7821\auto\cc\msvc的98行,把 -W4 修改为 -W3
# 否则,编译时(nmake -f build/Makefile),会出现如下错误:
objs/lib/nginx-rtmp-module-1.2.2/ngx_rtmp_core_module.c(611): error C2220: 以下警告被视为错误
objs/lib/nginx-rtmp-module-1.2.2/ngx_rtmp_core_module.c(611): warning C4456: “sa”的声明隐藏了上一个本地声明
objs/lib/nginx-rtmp-module-1.2.2/ngx_rtmp_core_module.c(506): note: 参见“sa”的声明
NMAKE : fatal error U1077: “cl -c -O2 -W4 -WX -nologo -MT -Zi -Fdobjs/nginx.pdb -DFD_SETSIZE=1024 -DNO_SYS_TYPES_H -Yungx_config.h -Fpobjs/ngx_config.pch -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/win32 -I objs/lib/nginx-rtmp-module-1.2.2 -I objs/lib/pcre2-10.45/src/ -I objs/lib/openssl-3.4.1/openssl/include -I objs/lib/zlib-1.3.1 -I objs -I src/http -I src/http/modules -Foobjs/addon/nginx-rtmp-module-1.2.2/ngx_rtmp_core_module.obj objs/lib/nginx-rtmp-module-1.2.2/ngx_rtmp_core_module.c”: 返回代码“0x2”
Stop.
NMAKE : fatal error U1077: “"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\bin\HostX86\x64\nmake.exe" -f objs/Makefile”: 返回代码“0x2”
Stop.
# 具体修改如下:
原先是:CFLAGS="$CFLAGS -W4" 修改成:CFLAGS="$CFLAGS -W3"
$ mkdir objs
$ mkdir objs/lib
$ cd objs/lib ===> 把pcre2-10.45.tar.gz,zlib-1.3.1.tar.gz,openssl-3.4.1.tar.gz这三个文件放到这个目录下
$ tar -zxv -f pcre2-10.45.tar.gz
$ tar -zxv -f zlib-1.3.1.tar.gz
$ tar -zxv -f openssl-3.4.1.tar.gz
$ tar -zxv -f nginx-http-flv-module-1.2.12.tar.gz
2、生成MAKEFILE文件
在nginx 源码根目录下执行auto命令
$ cd d:/nginx-2e63bf0a7821
$ auto/configure \
--with-cc=cl \
--with-debug \
--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 \
--http-scgi-temp-path=temp/scgi_temp \
--http-uwsgi-temp-path=temp/uwsgi_temp \
--with-cc-opt=-DFD_SETSIZE=1024 \
--with-pcre=objs/lib/pcre2-10.45 \
--with-zlib=objs/lib/zlib-1.3.1 \
--with-openssl=objs/lib/openssl-3.4.1 \
--with-openssl-opt=no-asm \
--with-http_ssl_module \
--with-http_sub_module \
--add-module=objs/lib/nginx-http-flv-module-1.2.12
# 输出结果如下
checking for OS
+ MSYS_NT-10.0-19045 3.5.7-2644508f.x86_64 x86_64
+ using Microsoft Visual C++ compiler
+ cl version:
auto/cc/msvc: line 132: [: : integer expression expected
checking for MSYS_NT-10.0-19045 specific features
configuring additional modules
adding module in objs/lib/nginx-rtmp-module-1.2.2
+ ngx_rtmp_module was configured
creating objs/Makefile
Configuration summary
+ using PCRE2 library: objs/lib/pcre2-10.45
+ using OpenSSL library: objs/lib/openssl-3.4.1
+ using zlib library: objs/lib/zlib-1.3.1
nginx path prefix: ""
nginx binary file: "/nginx.exe"
nginx modules path: "/modules"
nginx configuration prefix: "/conf"
nginx configuration file: "/conf/nginx.conf"
nginx pid file: "/logs/nginx.pid"
nginx error log file: "/logs/error.log"
nginx http access log file: "/logs/access.log"
nginx http client request body temporary files: "temp/client_body_temp"
nginx http proxy temporary files: "temp/proxy_temp"
nginx http fastcgi temporary files: "temp/fastcgi_temp"
nginx http uwsgi temporary files: "temp/uwsgi_temp"
nginx http scgi temporary files: "temp/scgi_temp"
这里会发现有一个异样的提示,auto/cc/msvc: line 132: [: : integer expression expected ,只要修改文件 d:/nginx-2e63bf0a7821\auto\cc\msvc 即可,在 echo " + cl version: $NGX_MSVC_VER" 的前面加入一行 NGX_MSVC_VER=19.00 ,当然不修改也不会影响后续的编译。
# 再次编译就不会有这个提示了
$ auto/configure \
--with-cc=cl \
--with-debug \
--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 \
--http-scgi-temp-path=temp/scgi_temp \
--http-uwsgi-temp-path=temp/uwsgi_temp \
--with-cc-opt=-DFD_SETSIZE=1024 \
--with-pcre=objs/lib/pcre2-10.45 \
--with-zlib=objs/lib/zlib-1.3.1 \
--with-openssl=objs/lib/openssl-3.4.1 \
--with-openssl-opt=no-asm \
--with-http_ssl_module \
--with-http_sub_module \
--with-openssl-opt=no-asm \
--add-module=objs/lib/nginx-rtmp-module-1.2.2
checking for OS
+ MSYS_NT-10.0-19045 3.5.7-2644508f.x86_64 x86_64
+ using Microsoft Visual C++ compiler
+ cl version: 19.00
checking for MSYS_NT-10.0-19045 specific features
configuring additional modules
adding module in objs/lib/nginx-rtmp-module-1.2.2
+ ngx_rtmp_module was configured
creating objs/Makefile
Configuration summary
+ using PCRE2 library: objs/lib/pcre2-10.45
+ using OpenSSL library: objs/lib/openssl-3.4.1
+ using zlib library: objs/lib/zlib-1.3.1
nginx path prefix: ""
nginx binary file: "/nginx.exe"
nginx modules path: "/modules"
nginx configuration prefix: "/conf"
nginx configuration file: "/conf/nginx.conf"
nginx pid file: "/logs/nginx.pid"
nginx error log file: "/logs/error.log"
nginx http access log file: "/logs/access.log"
nginx http client request body temporary files: "temp/client_body_temp"
nginx http proxy temporary files: "temp/proxy_temp"
nginx http fastcgi temporary files: "temp/fastcgi_temp"
nginx http uwsgi temporary files: "temp/uwsgi_temp"
nginx http scgi temporary files: "temp/scgi_temp"
# OpenSSL的坑
需要修改 d:/nginx-2e63bf0a7821\auto\lib\openssl\makefile.msvc 文件
将 ms\do_ms 改为 ms\do_nasm
3、执行编译命令
注: 执行nmake命令需要提前安装好 perl 和 sed for windows
使用x86_x64 Cross Tools Command Prompt for VS 2022
命令行工具执行编译。=>开始菜单程序中找到这个
进入nginx源代码根目录并执行nmake命令。这个过程稍长一些,需要5-10分钟左右,安装成功后如图所示:
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.13.5
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86_x64'
C:\Program Files\Microsoft Visual Studio\2022\Community>d:
D:\>cd nginx-2e63bf0a7821
D:\nginx-2e63bf0a7821>nmake -f objs/Makefile
正在搜索库
正在搜索 C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22621.0\\um\x64\kernel32.lib:
正在搜索 C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22621.0\\um\x64\user32.lib:
正在搜索 C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22621.0\\um\x64\advapi32.lib:
正在搜索 C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22621.0\\um\x64\ws2_32.lib:
正在搜索 objs\lib\pcre2-10.45\src\pcre2-8.lib:
正在搜索 objs\lib\openssl-3.4.1\openssl\lib\libssl.lib:
正在搜索 objs\lib\openssl-3.4.1\openssl\lib\libcrypto.lib:
正在搜索 C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22621.0\\um\x64\gdi32.lib:
正在搜索 C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22621.0\\um\x64\crypt32.lib:
正在搜索 objs\lib\zlib-1.3.1\zlib.lib:
正在搜索 C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\lib\x64\LIBCMT.lib:
正在搜索 C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\lib\x64\OLDNAMES.lib:
正在搜索 C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\lib\x64\libvcruntime.lib:
已完成库搜索
sed -e "s|%PREFIX%||" -e "s|%PID_PATH%|/logs/nginx.pid|" -e "s|%CONF_PATH%|/conf/nginx.conf|" -e "s|%ERROR_LOG_PATH%|/logs/error.log|" < docs/man/nginx.8 > objs/nginx.8
4、测试nginx
安装结束后会在 objs下生成nginx.exe可执行程序。其中conf 需要从根目录下拷贝到objs下。
使用 nginx.exe -t -c conf\nginx.conf
或者 nginx.exe -t
命令测试nginx.conf 文件,使用 start nginx 命令启动nginx服务,服务启动成功。
或者使用的时候可以直接下载官方的zip压缩包,替换掉里面的nginx.exe即可
三、结合ffmpeg使用
将RTSP流转换为HLS格式,并使用nginx推送HLS流供前端播放
1、拉取视频流保存到本地
ffmpeg -y -rtsp_transport tcp -i rtsp://admin:jddhlw966@192.168.2.248/LiveMedia/ch1/Media1/trackID=1 -t 10 -s 854x480 -c:v libx264 -f mp4 1.mp4
2、拉取视频流推送给nginx,然后供浏览器页面访问观看
nginx配置
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 65535;
multi_accept off;
}
rtmp{
server {
listen 1935;
chunk_size 4000;
application hls {
live on;
hls on;
hls_path html;
hls_fragment 5s;
hls_playlist_length 60;
play all;
}
}
}
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;
tcp_nodelay on;
keepalive_timeout 65;
proxy_hide_header X-Powered-By;
proxy_hide_header Server;
server_tokens off;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
gzip_proxied any;
server {
listen 80;
server_name 127.0.0.1;
location / {
root html;
index index.html index.htm;
}
location /hls {
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/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root html;
}
access_log logs/host.access.log main;
}
}
在nginx的html目录下有俩文件,index.html和hls.js
index.html文件内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./hls.js"></script>
<title>Document</title>
</head>
<body>
<video id="my-video" width="640" height="360" controls></video>
</body>
<script>
if (Hls.isSupported()) {
var video = document.getElementById('my-video'); // 获取video标签
var hls = new Hls(); // 实例化Hls对象
hls.loadSource('http://127.0.0.1/f1.m3u8'); // 传入m3u8视频文件的URL
hls.attachMedia(video); // 将Hls.js实例与video标签关联
hls.on(Hls.Events.MANIFEST_PARSED, function() {
video.play(); // 调用播放API
});
}
</script>
</html>
hls.js文件内容
从如下地址下载并保存到本地
https://files.cnblogs.com/files/blogs/309023/hls.js?t=1743235461&download=true
ffmpeg
下载压缩包,解压到D盘根目录下,并重命名为ffmpeg
把ffmpeg的bin目录添加到环境变量里,比如:D:\ffmpeg\bin
打开命令行,执行如下命令:
ffmpeg.exe -i rtsp://username:password@ip/LiveMedia/ch1/Media1/trackID=1 -c:v libx264 -c:a aac -hls_time 2 -hls_list_size 5 -hls_flags delete_segments -f flv rtmp://localhost:1935/hls/f1
命令执行后的m3u8文件地址是:http://127.0.0.1/f1.m3u8
打开浏览器播放相机视频
(或者使用vlc客户端,媒体-打开网络串流,输入网络URL地址,输入上一步的m3u8文件地址)