1.配置nginx-rtmp
獲取nginx-rtmp源碼,并且在編譯nginx時使用。
(1)安裝依賴
sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev
(2)下載nginx-rtmp源碼包
cd /home/zhou/software
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
(3)解壓nginx-rtmp源碼包
/home/zhou/software/nginx-rtmp-module-master
2.安裝 nginx
(1)切換到管理員用戶root
#切換到管理員用戶root(很重要)
sudo su
(2)安裝依賴(不在管理員模式下要加sudo)
#安裝依賴 (不在管理員模式下要加sudo)
apt-get update
apt-get install build-essential
apt-get install libtool
apt-get install libpcre3 libpcre3-dev
apt-get install zlib1g-dev
apt-get install libssl-dev
(3)下載nginx安裝包
下載地址:http://nginx.org/download
選擇最新版本下載:nginx-1.23.3.tar.gz,并將其放到路徑:/home/zhou/software
(4)解壓下載的壓縮包
cd /home/zhou/software
tar -xvf nginx-1.23.3.tar.gz
(5)進入nginx包目錄下
cd /home/zhou/software/nginx-1.23.3
(6)創(chuàng)建nginx用戶(執(zhí)行編譯命令前先創(chuàng)建Nginx用戶不然后面Nginx運行時會報錯)
useradd nginx
(7)執(zhí)行命令編譯安裝
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-http_realip_module --with-http_v2_module --with-http_sub_module --with-http_mp4_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
# 配置nginx-rtmp
./configure --with-http_ssl_module --add-module=/home/zhou/software/nginx-rtmp-module-master
make
sudo make install
(8)進入安裝目錄
cd /usr/local/nginx
(9)配置nginx軟連接
ln -s /usr/local/nginx/sbin/nginx /usr/bin/
(10)軟連接配置完成即以成功開啟nginx服務即可,在瀏覽器上訪問對應ip即可
ll /usr/bin/nginx
# 驗證配置文件
sudo nginx -t
nginx -v

(11)啟動并驗證效果
- 啟動
nginx -s reload
- 驗證
瀏覽器中數(shù)據(jù)ip地址(對應配置文件中的server_name)

3.配置 nginx
配置文件路徑:/usr/local/nginx/conf/nginx.conf
3.1 文件結構
全局塊:配置影響nginx全局的指令。一般有運行nginx服務器的用戶組,nginx進程pid存放路徑,日志存放路徑,配置文件引入,允許生成worker process數(shù)等。
events塊:配置影響nginx服務器或與用戶的網(wǎng)絡連接。有每個進程的最大連接數(shù),選取哪種事件驅動模型處理連接請求,是否允許同時接受多個網(wǎng)路連接,開啟多個網(wǎng)絡連接序列化等。
http****塊:可以嵌套多個server,配置代理,緩存,日志定義等絕大多數(shù)功能和第三方模塊的配置。如文件引入,mime-type定義,日志自定義,是否使用sendfile傳輸文件,連接超時時間,單連接請求數(shù)等。
server塊:配置虛擬主機的相關參數(shù),一個http中可以有多個server。
location塊:配置請求的路由,以及各種頁面的處理情況。
3.2 默認配置
### 全局塊
#user nobody; #配置用戶或者組,默認為nobody nobody。
worker_processes 1; #允許生成的進程數(shù),默認為1
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#制定日志路徑,級別。這個設置可以放入全局塊,http塊,server塊,級別以此為:debug|info|notice|warn|error|crit|alert|emerg
#pid logs/nginx.pid; #指定nginx進程運行文件存放地址
### events塊
events {
worker_connections 1024; #最大連接數(shù),默認為512
}
http {
include mime.types; #文件擴展名與文件類型映射表
default_type application/octet-stream; #默認文件類型,默認為text/plain
#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; #允許sendfile方式傳輸文件,默認為off,可以在http塊,server塊,location塊。
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #連接超時時間,默認為75s,可以在http,server,location塊。
#gzip on;
server {
listen 80; #監(jiān)聽端口
server_name localhost; #監(jiān)聽地址
#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;
# }
#}
}
4.推流/拉流測試
需要準備FFmpeg和VLC軟件
4.1 配置文件
新建配置文件:/usr/local/nginx/conf/nginx.test.1.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;
}
rtmp_auto_push on;
#RTMP服務
rtmp{
server{
listen 8112; #服務端口
chunk_size 4096; #數(shù)據(jù)傳輸塊的大小
#rtmp點播配置,訪問方式:rtmp://127.0.0.1:8112/vod/xxx.mp4
application vod {
play /home/zhou/視頻/nginx; #點播視頻存放目錄,里面放一個xxx.mp4可以直接播放
}
#rtmp協(xié)議直播推流
application live {
live on;
record all;
record_path /home/zhou/視頻/nginx/rtmp; # 保存路徑
#record_max_size 1K;
#append current timestamp to each flv
record_unique on;
#publish only from localhost
#allow publish 127.0.0.1;
#deny publish all;
}
# HLS協(xié)議直播推流
application hls {
live on; #開啟rtmp直播
hls on; #開啟hls支持
wait_video on; #第一個視頻幀發(fā)送前禁用音頻
hls_path /home/zhou/視頻/nginx/hls; #文件保存路徑,需要先創(chuàng)建,不然執(zhí)行推流會報錯
hls_fragment 5s; #用來設置每一個塊的大小。默認是5秒。只能為整數(shù)
hls_playlist_length 30s; #設置播放列表的長度,單位是秒,聽說設置成3秒延遲低點
hls_nested off; #默認是off。打開后的作用是每條流自己有一個文件夾
hls_cleanup off; #不清理ts, on|off 默認是開著的,是否刪除列表中已經(jīng)沒有的媒體塊
#hls_continuous: #on|off 設置連續(xù)模式,是從停止播放的點開始還是直接跳過
}
}
}
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 8111;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#hls點播地址
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
#或 application/x-mpegURL
video/mp2t ts;
}
alias /home/zhou/視頻/nginx/vod/; #點播視頻文件(.ts;.m3u8)存放位置
expires -1;
add_header Cache-Control no-cache; #跨域支持,不然網(wǎng)頁播放不了
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
autoindex on;# 顯示目錄
autoindex_exact_size on;# 顯示文件大小
autoindex_localtime on;# 顯示文件時間
}
點播的概念有些模糊,目前理解就是:
方式1:直接把分片好的m3u8文件作為視頻:然后使用http訪問
訪問方式:在VLC工具里面輸入 http://127.0.0.1:8111/hls/xxx.m3u8
方式2:直接使用rtmp協(xié)議訪問mp4
訪問方式:在VLC工具里面輸入 rtmp://127.0.0.1:8112/vod/xxx.mp4
4.2 FFMPEG命令
#使用rtmp協(xié)議推流
ffmpeg -re -i test.mp4 -vcodec libx264 -acodec aac -f flv rtmp://localhost:8112/live/test
#說明:test.mp4是待推的視頻,rtmp://localhost:8112/live/test是推送視頻流和拉取視頻流的地址,其中test是自己命名的視頻名稱,可以使用這個地址在VLC中觀看直播(拉取直播流)。
#使用hls協(xié)議推流(會產生test視頻流文件)
ffmpeg -re -i 11.mp4 -vcodec libx264 -acodec aac -f flv rtmp://localhost:8112/hls/test
#說明:11.mp4是待推的視頻,rtmp://localhost:8112/live/test是推送視頻流和拉取視頻流的地址,其中test是自己命名的視頻名稱,可以使用這個地址在VLC中觀看直播(拉取直播流)。
#將mp4切割成m3u8分片
ffmpeg -i 11.mp4 -profile:v baseline -level 3.0 -start_number 0 -force_key_frames "expr:gte(t,n_forced*1)" -hls_time 10 -hls_list_size 0 -f hls test.m3u8
#說明: 11.mp4是等待切割的視頻,test.m3u8是切割完成的文件,還有很多切割好的testxx.ts文件。
#-force_key_frames "expr:gte(t,n_forced*1)" 代表每1秒1個關鍵幀,方便-hls_time 10切割每10秒一個ts文件
#-f hls test.m3u8 輸出文件名叫test.m3u8
4.3 調試
在使用FFmpeg推送視頻流的同時可以打開VLC工具測試是否有流輸出,打開VLC,媒體、打開網(wǎng)絡串流、網(wǎng)絡、輸入網(wǎng)絡URL。

(1)rtmp協(xié)議推流
#FFMPEG用rtmp協(xié)議推流:libx264無法使用
ffmpeg -re -i /home/zhou/視頻/test.mp4 -acodec aac -f flv rtmp://localhost:8112/live/test
#VLC拉取trmp協(xié)議的視頻流:
rtmp://localhost:8112/live/test
(2)hls協(xié)議推流
#FFMPEG用hls協(xié)議推流(注意會產生m3u8和ts文件):
ffmpeg -re -i /home/zhou/視頻/test.mp4 -acodec aac -f flv rtmp://localhost:8112/hls/test
#VLC拉取hls協(xié)議的視頻流:
rtmp://127.0.0.1:8112/hls/test
5.問題
5.1 啟動異常
問題:執(zhí)行nginx -s reload

解決:鏈接
cd /usr/local/nginx/sbin/
killall -9 nginx
nginx -t
nginx -c /usr/local/nginx/conf/nginx.conf
5.2 推流錯誤
問題:執(zhí)如下代碼出現(xiàn)異常
ffmpeg -re -i /home/zhou/視頻/test.mp4 -vcodec libx264 -acodec aac -f flv rtmp://localhost:8112/live/test

解決:
ffmpeg推流時報錯 Unknown encoder 'libx264'
ffmpeg推流時,可能出現(xiàn)錯誤:Unknown encoder 'libx264'
cd /home/zhou/code/ffmpeg/
git clone https://code.videolan.org/videolan/x264.git
cd x264
./configure --prefix=/usr/softinstall/x264/ --includedir=/usr/local/include --libdir=/usr/local/lib --enable-shared
出現(xiàn)如下提示,需要安裝nasm
If you really want to compile without asm, configure with --disable-asm.
安裝方法
https://www.nasm.us/pub/nasm/releasebuilds/2.14/
選擇:nasm-2.14.tar.gz
tar xzvf nasm-2.14.tar.gz
cd nasm-2.14
#需要進入root下編譯,否則會出現(xiàn)權限問題
sudo su
./configure
make
make install
編譯安裝x264:
make -j128
make install
編譯成功后查看本版:x264 -V
重新編譯ffmpeg:
(1)先卸載ffmpeg
cd /home/zhou/code/ffmpeg/ffmpeg
make uninstall
(2)編譯
sudo ./configure \
--extra-libs="-lpthread -lm" \
--ld="g++" \
--enable-gpl \
--enable-gnutls \
--enable-libass \
--enable-libfdk-aac \
--enable-libfreetype \
--enable-libmp3lame \
--enable-libopus \
--enable-libvorbis \
--enable-libvpx \
--enable-libx264 \
--enable-libx265 \
--enable-libxml2 \
--enable-nonfree
sudo ./configure --enable-gpl --enable-libx264
sudo ./configure --enable-shared
export LD_LIBRARY_PATH=/usr/local/lib/
make -j256
sudo make install