@toc
最近看了下FFMPEG的推流相關(guān)資料,有點(diǎn)雜亂也有點(diǎn)老辕羽,首先第一步吧蔽介,準(zhǔn)備相關(guān)工具地技。Nginx在linux端使用配置應(yīng)該沒多大問題丐箩,在windows端的編譯可能繁瑣一點(diǎn)减俏,那么就在windows上測(cè)試吧将饺。
成功樣式
前提條件
目錄路徑不要有中文矢渊,不然編譯會(huì)有問題,特別是定義prefix
時(shí)路徑設(shè)置不要有中文
下載所需庫(kù)
打包下載FFMPEG4.2.3庫(kù)文件和Nginx1.19.6源碼及編譯文件
百度搜索躬审,即可找到對(duì)應(yīng)下載地址棘街,如遇下載速度慢,可以前往github搜索下載
Nginx1.19.6
openssl-1.1.1g
pcre-8.44
zlib-1.2.11
下載地址
nginx-http-flv-module has all features that nginx-rtmp-module provides, so DON'T compile nginx-http-flv-module along with nginx-rtmp-module.
nginx-http-flv-module
下載地址
打開 MSYS
進(jìn)入源碼目錄承边,執(zhí)行以下遭殉,或者手動(dòng)解壓皆可
mkdir -p objs/lib
cd objs/lib
tar -xf ../../pcre-8.44.tar.bz2
tar -xf ../../zlib-1.2.11.tar.gz
tar -xf ../../openssl-1.1.1g.tar.gz
編譯依賴庫(kù)文件
安裝相關(guān)工具
安裝visual studio
下載地址
要它的集成工具,有些Makefile就是使用nmake去編譯博助,Windows端就用微軟的工具吧险污,能減少很多錯(cuò)誤
安裝perl
下載地址
如遇以下問題處理:Can't locate Win32/Console.pm in @INC (you may need to install the Win32::Console module)
參考
安裝sed for windows
下載地址
安裝后將路徑C:\Program Files (x86)\GnuWin32\bin
加入到環(huán)境變量Path中
編譯Nginx
打開MSYS
執(zhí)行以下命令進(jìn)入Nginx源碼目錄:
auto/configure --with-cc=cl --with-debug --prefix=install_dir --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/pcre-8.44 --with-zlib=objs/lib/zlib-1.2.11 --with-openssl=objs/lib/openssl-1.1.1g --with-openssl-opt=no-asm --with-http_ssl_module --with-http_sub_module --add-module=objs/lib/nginx-http-flv-module-1.2.8
生成Makefile后,打開vs的命令行工具富岳,再次進(jìn)入到Nginx目錄執(zhí)行以下編譯命令:
nmake
打開vs的命令行工具
我選擇64位的蛔糯,輸入以下
powershell
進(jìn)入powershell工具里伯,命令可以像linux終端一樣用了進(jìn)入Makefile文件所在目錄執(zhí)行
nmake
將自動(dòng)編譯openssl zlib pcre
,靜態(tài)鏈接渤闷,生成Nginx目標(biāo)。
安裝
建立腳本install.bash
放在objs目錄下
#!/bin/bash
mkdir -p install_dir
mkdir -p .\install_dir\conf
mkdir -p .\install_dir\logs
cp .\nginx.exe .\install_dir\
cp ..\conf\koi-win .\install_dir\conf\
cp ..\conf\koi-utf .\install_dir\conf\
cp ..\conf\win-utf .\install_dir\conf\
cp ..\conf\mime.types .\install_dir\conf\
cp ..\conf\fastcgi.conf .\install_dir\conf\
cp ..\conf\fastcgi_params .\install_dir\conf\
cp ..\conf\uwsgi_params .\install_dir\conf\
cp ..\conf\scgi_params .\install_dir\conf\
cp ..\conf\nginx.conf .\install_dir\conf\
cp -R ..\docs\html .\install_dir\
打開MSYS執(zhí)行此腳本
運(yùn)行測(cè)試
./nginx -t
對(duì)于可能出現(xiàn)的問題脖镀,端口占用飒箭,確認(rèn)取消勾選,IIS服務(wù)會(huì)暫用80端口蜒灰,導(dǎo)致啟動(dòng)出錯(cuò)弦蹂。
處理參考
瀏覽器輸入
localhost
,見如下頁(yè)面說(shuō)明啟動(dòng)成功强窖。nmake編譯選項(xiàng)
nmake /?
nmake編譯報(bào)錯(cuò)處理
轉(zhuǎn)推流測(cè)試
配置Nginx
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 = /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;
# }
#}
server {
listen 8080;
server_name localhost;
location /live {
flv_live on; #打開 HTTP 播放 FLV 直播流功能
chunked_transfer_encoding on; #支持 'Transfer-Encoding: chunked' 方式回復(fù)
add_header 'Access-Control-Allow-Credentials' 'true'; #添加額外的 HTTP 頭
add_header 'Access-Control-Allow-Origin' '*'; #添加額外的 HTTP 頭
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header 'Cache-Control' 'no-cache';
}
}
}
rtmp {
out_queue 4096;
out_cork 8;
max_streams 128;
timeout 15s;
drop_idle_publisher 15s;
log_interval 5s; #log 模塊在 access.log 中記錄日志的間隔時(shí)間凸椿,對(duì)調(diào)試非常有用
log_size 1m; #log 模塊用來(lái)記錄日志的緩沖區(qū)大小
server {
listen 1985;
server_name aron566; #用于虛擬主機(jī)名后綴通配
application live {
live on;
}
}
}
重新啟動(dòng)Nginx
./nginx.exe -s reload
安裝FFMPEG,執(zhí)行以下命令
ffmpeg -i rtmp://58.200.131.2:1935/livetv/jstv -vcodec copy -acodec copy -f flv rtmp://localhost:1985/live/mystream
使用VLC播放器,輸入拉流地址播放
http://192.168.50.61:8080/live?port=1985&app=live&stream=mystream
192.168.50.61為服務(wù)器IP