安裝nginx的依賴庫html
sudo apt-get update sudo apt-get install libpcre3 libpcre3-dev sudo apt-get install openssl libssl-dev
配置並編譯nginx
使用nginx的默認配置,添加nginx的rtmp模塊。
nginx
./configure --add-module=../nginx-rtmp-module-master make sudo make install
運行測試nginx
進入安裝目錄/usr/local/nginx,運行命令./sbin/nginxgit
注意:之後全部的命令都在/usr/local/nginx目錄運行,也nginx配置文件的相對目錄。github
打開瀏覽器在地址欄輸入:localhost。若是,以下圖顯示那樣就證實您的nginx服務器搭建成功了。
ubuntu
經過上一步nginx服務器已經搭建完成,而後咱們就能夠開啓一個視頻點播的服務了。打開配置文件nginx.conf,添加RTMP的配置。瀏覽器
worker_processes 1; events { worker_connections 1024; } rtmp { #RTMP服務 server { listen 1935; #//服務端口 chunk_size 4096; #//數據傳輸塊的大小 application vod { play /opt/vide/vod; #//視頻文件存放位置。 } } } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
配置目錄/opt/video/vod爲存放視頻文件的位置了,那咱們就往裏面放一個文件吧。我放了一個qq.mp4文件。
文件放好以後,那就讓咱們從新啓動一下nginx服務器
sudo ./sbin/nginx -s reloadapp
打開視頻播放軟件選用的是VLC media-> open network stream….
如圖填寫咱們要點播的節目地址rtmp://localhost/vod/qq.mp4 如圖:
點擊play就能夠播放了。
固然點播不使用RTMP插件nginx自身也是能夠實現點播服務的。那就是配置location部分,因爲下面咱們要配置直播和回看功能因此選用了RTMP服務。ide
接着咱們就在點播服務器配置文件的基礎之上添加直播服務器的配置。一共2個位置,第一處就是給RTMP服務添加一個application這個名字能夠任意起,也能夠起多個名字,因爲是直播我就叫作它live吧,若是打算弄多個頻道的直播就能夠live_cctv一、live_cctv2名字任意。第二處就是添加兩個location字段,字段的內容請直接看文件吧。測試
worker_processes 1; events { worker_connections 1024; } rtmp { server { listen 1935; chunk_size 4096; application vod { play /opt/video/vod; } application live{ #第一處添加的直播字段 live on; } } } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location /stat { #第二處添加的location字段。 rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { #第二處添加的location字段。 root /usr/local/nginx/nginx-rtmp-module/; } location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
添加完這兩處以後,從新啓動nginx打開瀏覽器看看,是否有以下圖顯示:
有沒有看到紅框框的live字樣呢?若是能夠顯示出來,證實你的配置生效了。
還等什麼讓咱們推送一個節目看看(其實專業詞叫錄製,後面將會使用錄製這個詞。)吧。
此次推送我使用的是OBS(Open Broadcaster Software)有關它的安裝請參考先前我寫的Ubuntu安裝OBS(Open Broadcaster Software)後面的博客裏我會給你們介紹如何使用手機採集視頻而且推送到服務器上。
咱們想想若是直播服務可以把節目錄制在本地,咱們不就能夠直接進行回看先前的節目了嗎?回看一分鐘、一小時甚至一天的。想一想就興奮不用寫代碼有現成的可使用。怎麼用呢?繼續看nginx的配置吧。
worker_processes 1; events { worker_connections 1024; } rtmp { server { listen 1935; chunk_size 4096; application vod { play /opt/video/vod; } application live { live on; hls on; #這個參數把直播服務器改形成實時回放服務器。 wait_key on; #對視頻切片進行保護,這樣就不會產生馬賽克了。 hls_path /opt/video/hls; #切片視頻文件存放位置。 hls_fragment 10s; #每一個視頻切片的時長。 hls_playlist_length 60s; #總共能夠回看的事件,這裏設置的是1分鐘。 hls_continuous on; #連續模式。 hls_cleanup on; #對多餘的切片進行刪除。 hls_nested on; #嵌套模式。 } } } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /usr/local/nginx/nginx-rtmp-module/; } location /live { #這裏也是須要添加的字段。 types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } alias /opt/video/hls; expires -1; add_header Cache-Control no-cache; } location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
1.添加完成後須要從新啓動nginx,因爲此次nginx須要向服務器寫切片視頻文件,但nginx我又沒有給nginx指定用戶名只能走默認的nobody用戶和nogroup用戶組,其實就是沒有組。因此我對須要寫入的目錄作了增大權限的修改。
以下圖,這樣作就是爲了不因爲權限問題而沒法寫文件。
2.如何給服務器錄製視頻,在上一節已經說過,這裏就再也不說了。
3.查看視頻文件是否真的錄製上沒有,繼續看圖:
已經產生切片視頻文件了。其中還有一個index.m3u8。
4.播放視頻,此次但是http開頭的了,「http://localhost/live/test/index.m3u8」。
5.已經能夠播放了,如何回看呢?其實這個index.m3u8文件僅僅是目錄。想回看那個就播放那個.ts文件就能夠了。
到此已經結束。若有問題歡迎留言。