Technologieaustausch

Nginx-Installations- und Konfigurationsvideoserver-Windows

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Kompilieren und installieren Sie Nginx

1. Installieren Sie Perl

Installationsadresse: https://strawberryperl.com, wählen Sie einfach das MSI-Installationsprogramm aus

2. Installieren Sie sed für Windows

Download-Adresse: https://sourceforge.net/projects/gnuwin32/files/sed/ Konfigurieren Sie nach der Ausführung des Installationsprogramms das bin-Verzeichnis des Installationspakets für die Umgebungsvariable.

3. Installieren Sie die Community-Version von Visual Studio 2022

4. Vonhttps://github.com/nginx/nginxLaden Sie den Quellcode der Version 1.26.1 herunter und erstellen Sie im Quellcode ein neues Verzeichnis objslib

Laden Sie das Nginx-http-flv-Modul herunter:https://github.com/winshining/nginx-http-flv-module(Version 1.2.10)

PCRE herunterladen:https://github.com/PCRE2Project/pcre2/tags(Version 10.2.39)

zlib herunterladen:https://github.com/madler/zlib(Version 1.3.1)

OpenSSL herunterladen:https://www.openssl.org/source/index.html(Version 3.0.13)

Kopieren Sie nginx-http-flv-module, openssl, pcre und zlib in dieses Verzeichnis und entpacken Sie es

5. Nginx-Kompilierungsparameter anzeigen

Laden Sie die kompilierte Version von nginx-1.26.1 herunter, führen Sie nginx.exe -V aus und erhalten Sie die folgenden Ergebnisse:

configure arguments: --with-cc=cl --builddir=objs.msvc8 --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.msvc8/lib/pcre2-10.39 --with-zlib=objs.msvc8/lib/zlib-1.3.1 --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_slice_module --with-mail --with-stream --with-stream_realip_module --with-stream_ssl_preread_module --with-openssl=objs.msvc8/lib/openssl-3.0.13 --with-openssl-opt='no-asm no-tests -D_WIN32_WINNT=0x0501' --with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module

Ändern Sie einige Parameter:

--with-pcre=objs/lib/pcre2-10.39 --with-zlib=objs/lib/zlib-1.3.1 --with-openssl=objs/lib/openssl-3.0.13 --add-module=objs/lib/nginx-http-flv-module-1.2.10

7. Konfigurieren

Führen Sie MSYS2 oder MINGW64 aus, geben Sie den Nginx-Quellcode nginx-release-1.26.1 ein und führen Sie den Befehl aus:

auto/configure --with-cc=cl --builddir=objs.msvc8 --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-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_stub_status_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_slice_module --with-mail --with-stream --with-stream_realip_module --with-stream_ssl_preread_module --with-openssl-opt='no-asm no-tests -D_WIN32_WINNT=0x0501' --with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module --with-pcre=objs/lib/pcre2-10.39 --with-zlib=objs/lib/zlib-1.3.1 --with-openssl=objs/lib/openssl-3.0.13 --add-module=objs/lib/nginx-http-flv-module-1.2.10

Ändern Sie objs.msvc8/Makefile und ändern Sie die dritte Zeile:

CFLAGS =  -O2  -W4 -WX -nologo -MT -Zi -Fdobjs.msvc8/nginx.pdb -DFD_SETSIZE=1024 -DNO_SYS_TYPES_H

Entfernen Sie -WX und fügen Sie -MP hinzu

CFLAGS =  -O2  -W4 -MP -nologo -MT -Zi -Fdobjs.msvc8/nginx.pdb -DFD_SETSIZE=1024 -DNO_SYS_TYPES_H

8. Kompilieren
Öffnen Sie die Developer-Eingabeaufforderung für VS 2022, geben Sie das Nginx-Quellverzeichnis ein, geben Sie den Befehl nmake ein und drücken Sie die Eingabetaste

Die Kompilierungszeit beträgt 10 bis 30 Minuten. Kopieren Sie nach Abschluss nginx.exe unter objs.msvc8 in das Nginx-Quellcodeverzeichnis.

Erstellen Sie neue Verzeichnisse „logs“, „temp“ und „html“.

9. Installieren Sie ffmpeg

Laden Sie es herunter, installieren Sie es und legen Sie das Verzeichnis ffmpeg bin als Umgebungsvariable fest

Konfigurieren Sie Nginx

Ändern Sie conf/nginx.conf

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. sendfile on;
  9. keepalive_timeout 65;
  10. server {
  11. listen 8553;
  12. server_name localhost;
  13. location / {
  14. root html;
  15. index index.html index.htm;
  16. }
  17. location /live {
  18. flv_live on;
  19. chunked_transfer_encoding on;
  20. add_header 'Access-Control-Allow-Origin' '*';
  21. add_header 'Access-Control-Allow-Credentials' 'true';
  22. add_header 'Access-Control-Allow-Methods' '*';
  23. }
  24. location /hls {
  25. add_header 'Access-Control-Allow-Origin' '*';
  26. types {
  27. application/vnd.apple.mpegurl m3u8;
  28. video/mp2t ts;
  29. }
  30. alias /test;
  31. expires -1;
  32. }
  33. location /stat {
  34. rtmp_stat all;
  35. rtmp_stat_stylesheet stat.xsl;
  36. }
  37. location /stat.xsl {
  38. root /usr/local/nginx/nginx-http-flv-module;
  39. }
  40. }
  41. }
  42. rtmp {
  43. server {
  44. listen 1938; #nginx监听的rtmp推流/拉流端口
  45. application myapp {
  46. live on; #当推流时,rtmp路径中的app(rtmp中的一个概念)匹配myapp时,开始直播
  47. meta off;
  48. gop_cache on;
  49. allow play all;
  50. record off;
  51. hls on;
  52. hls_path /test;
  53. hls_fragment 1s;
  54. }
  55. }
  56. }

ffmpeg-Push-Stream (Haikang-Kamera)

  1. ffmpeg -i rtsp://用户名:密码@ip:554/H.264/ch1/main/av_stream -c:v libx264 -an -f
  2. flv rtmp://127.0.0.1:1938/myapp/main

prüfen

flv.html

  1. <html>
  2. <head>
  3. <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  4. <title>flv.js demo</title>
  5. <style>
  6. .mainContainer {
  7. display: block;
  8. width: 1024px;
  9. margin-left: auto;
  10. margin-right: auto;
  11. }
  12. .urlInput {
  13. display: block;
  14. width: 100%;
  15. margin-left: auto;
  16. margin-right: auto;
  17. margin-top: 8px;
  18. margin-bottom: 8px;
  19. }
  20. .centeredVideo {
  21. display: block;
  22. width: 100%;
  23. height: 576px;
  24. margin-left: auto;
  25. margin-right: auto;
  26. margin-bottom: auto;
  27. }
  28. .controls {
  29. display: block;
  30. width: 100%;
  31. text-align: center;
  32. margin-left: auto;
  33. margin-right: auto;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <p class="mainContainer">
  39. <video name="videoElement" id="videoElement" class="centeredVideo" controls muted autoplay width="1024"
  40. height="576">
  41. Your browser is too old which doesn't support HTML5 video.
  42. </video>
  43. </p>
  44. <script src="flv.min.js"></script>
  45. <script>
  46. function start() {
  47. if (flvjs.isSupported()) {
  48. var videoElement = document.getElementById('videoElement');
  49. var flvPlayer = flvjs.createPlayer({
  50. type: 'flv',
  51. url: 'http://ip:8553/live?port=1938&app=myapp&stream=main'
  52. });
  53. flvPlayer.attachMediaElement(videoElement);
  54. flvPlayer.load();
  55. flvPlayer.play();
  56. }
  57. }
  58. document.addEventListener('DOMContentLoaded', function () {
  59. start();
  60. });
  61. </script>
  62. </body>
  63. </html>

Download-Adresse von flv.min.js:http://flv.jnyzh.cn/flv.min.js

Der Effekt ist wie folgt: