需求
本文介紹的是利用nginx和ffmpeg搭建流媒體服務器的過程。例如這種場景:公司內部需要同時觀看在線直播時,如果每個人直接觀看必然給出口帶寬帶來壓力,影響正常訪問外網的同事。所以可以在內網通過nginx+ffmpeg拉一路直播流,然后內網的用戶訪問內網的這臺流媒體服務器即可。通過nginx+ffmpeg還可以實現推流、拉流、轉推甚至利用FFmpeg實時切片、視頻處理等,實現一套直播服務模型。
環境
系統環境:CentOS release 6.7 (Final)
步驟
安裝ffmpeg
安裝過程參考官方文檔:https://trac.ffmpeg.org/wiki/CompilationGuide
安裝Nginx
這里采用了編譯安裝的方式,需要注意的是:一定要添加nginx-rtmp-module模塊
1
2
3
|
git clone https://github.com/arut/nginx-rtmp-module.git #編譯的時候添加nginx-rtmp-module模塊 --add-module=path_of_/nginx-rtmp-module |
我的nginx編譯參數
1
|
./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre=/opt/software/pcre-8.35 --with-zlib=/opt/software/zlib-1.2.8 --with-openssl=/opt/software/openssl-1.0.1i --add-module=/opt/software/nginx-1.8.1/modules/nginx-rtmp-module |
修改nginx配置文件nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
user nginx; worker_processes 2; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { use epoll; worker_connections 1024; } #切換自動推送(多 worker 直播流)模式。默認為 off rtmp_auto_push on; #當 worker 被干掉時設置自動推送連接超時時間。默認為 100 毫秒 rtmp_auto_push_reconnect 1s; rtmp { server { listen 1935; #直播流配置 application myapp { live on; } # hls切片 application hls { live on; hls on; hls_path /tmp/hls; } # 拉流 application qiniu { live on; push rtmp://拉流地址; } # 轉推 application pull { live on; pull rtmp://轉推地址; } # rtmp日志設置 access_log logs/rtmp_access.log ; } } 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 65; gzip on; server { listen 80; server_name localhost; charset utf-8; access_log logs/host.access.log main; location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /opt/software/nginx-rtmp-module/; } location / { root /opt/www/html; index index.html index.htm; } location /hls { types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /tmp; add_header Cache-Control no-cache; } #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; } } include vhosts/*.conf; } |
這是一個較簡單的配置, rtmp監聽1935端口,如果是hls的話用hls on開啟hls,并且為hls設置一個臨時文件目錄hls_path /tmp/hls; 其它更高級的配置可以參看nginx-rtmp-module的readme,里面有比較詳細的介紹其它的配置。
重啟Nginx
1
2
|
nginx -t nginx -s reload |
查看Nginx已監聽1935端口。
使用ffmpeg推流到nginx
推一個本地的mp4到上面配置的myapp上:
1
|
ffmpeg -re -i /tmp/ffmpeg_test.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.0.0.1:1935/myapp/test1" |
流播放地址為(10.0.0.6是我本地的IP):rtmp://10.0.0.6:1935/myapp/test1
推一個本地的mp4到hls上:
1
|
ffmpeg -re -i /tmp/ffmpeg_test.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.0.0.1:1935/hls/test2" |
流播放地址為: http://10.0.0.6/hls/test2.m3u8
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。謝謝大家對服務器之家的支持。