透過Nginx + RTMP module搭建HLS直播系統
環境架構介紹 :
Nginx 主機透過youtube-dl擷取YouTube直播串流 → ffmpeg轉檔成flv → 發送至本地端RTMP server
1. Nginx 安裝 (CentOS 7為例)
安裝相依性套件
yum update
yum groupinstall "Development Tools" -y
yum install git -y
yum install pcre-devel zlib-devel openssl-devel -y
安裝編譯Nginx
cd /usr/local/src/
git clone https://github.com/arut/nginx-rtmp-module.git
git clone https://github.com/nginx/nginx.git
cd nginx
./auto/configure --add-module=../nginx-rtmp-module
make
make install
2.Nginx nginx.conf 編寫(/usr/local/nginx/conf/nginx.conf)
worker_processes 1;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
application live {
live on;
interleave on;
hls on;
hls_path /tmp/hls;
hls_playlist_length 3s;
hls_fragment 1s;
}
}
}
http {
default_type application/octet-stream;
server {
listen 80;
location / {
root /tmp/hls;
}
}
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
text/html html;
}
}
3.啟動Nginx
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
/usr/local/nginx/sbin/nginx
4.確認一下1935和80 port有帶起來
netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:1935 0.0.0.0:* LISTEN 29768/nginx: master
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 29768/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1031/sshd
5. ffmpeg 開始推送YouTube串流進來
找到YouTube相關URL,透過youtube-dl查詢所有格式,選擇一個所需的格式來轉檔
youtube-dl --list-format https://www.youtube.com/watch?v=GmnBAjLQFic
[youtube] GmnBAjLQFic: Downloading webpage
[youtube] GmnBAjLQFic: Downloading m3u8 information
[youtube] GmnBAjLQFic: Downloading MPD manifest
[info] Available formats for GmnBAjLQFic:
format code extension resolution note
91 mp4 256x144 269k , avc1.4d400c, 30.0fps, mp4a.40.5
92 mp4 426x240 507k , avc1.4d4015, 30.0fps, mp4a.40.5
93 mp4 640x360 962k , avc1.4d401e, 30.0fps, mp4a.40.2
94 mp4 854x480 1282k , avc1.4d401f, 30.0fps, mp4a.40.2
95 mp4 1280x720 2447k , avc1.4d401f, 30.0fps, mp4a.40.2
96 mp4 1920x1080 4561k , avc1.4d4028, 30.0fps, mp4a.40.2 (best)
假設我們要選用解析度640*368的格式來轉檔,記得format欄位的93這個數字,然後透過ffmpeg來轉檔
ffmpeg -re -i $(youtube-dl -f 93 -g https://www.youtube.com/watch?v=GmnBAjLQFic) -c:v copy -c:a copy -f flv rtmp://10.140.15.225/live/yt
ffmpeg轉檔指令介紹 :
-re : 讀取輸入來源的原生frame rate
-i : 輸入來源(這邊是透過youtube-dl來擷取YouTube直播串流)
youtube-dl -f 93 -g https://www.youtube.com/watch?v=GmnBAjLQFic : -g 透過youtube-dl去下載該URL,-f 格式選擇93 (640*368的格式)
-c:v : video格式,copy代表跟來源相同
-c:a : 音源格式,copy代表跟來源相同
-f flv : 轉檔成flv格式
rtmp://10.140.15.225/live/yt : 將檔案送至 rtmp server中,live是定義在nginx.conf rtmp 中 application,yt自行定義一個串流名稱
rtmp {
server {
listen 1935;
application live {
live on;
interleave on;
hls on;
hls_path /tmp/hls;
hls_playlist_length 3s;
hls_fragment 1s;
}
}
}
然後會在/tmp/hls目錄當中產生yt.m3u8的檔案,以及多個.ts檔案的分割片段
ll /tmp/hls/
total 408
-rw-r--r-- 1 nobody nobody 215448 Jul 20 14:47 yt-1.ts
-rw-r--r-- 1 nobody nobody 181608 Jul 20 14:47 yt-2.ts
-rw-r--r-- 1 nobody nobody 142 Jul 20 14:47 yt.m3u8
6.驗證直播
安裝VLC Player,輸入串流URL : http://your-ip/yt.m3u8