在Ubuntu下使用nginx-rtmp-module搭建直播系統


直播系統最簡單地包括推流和拉流,在這里先使用nginx-rtmp-module作為流媒體服務器。

流媒體服務器搭建

1. nginx-rtmp-module下載和安裝

源碼地址:https://github.com/arut/nginx-rtmp-module
使用git命令下載:git clone https://github.com/arut/nginx-rtmp-module.git

2.nginx源碼下載及編譯安裝

nginx下載: wget http://nginx.org/download/nginx-1.16.0.tar.gz
最新版本可以查看http://nginx.org/en/download.html 得到。

在編譯nginx之前,需要先下載編譯安裝好zlib、openssl、pure庫


wget http://www.zlib.net/zlib-1.2.11.tar.gz
tar xf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make && make install

wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz
tar xf openssl-1.0.2l.tar.gz
cd openssl-1.0.2l
./config
make && make install


wget https://nchc.dl.sourceforge.net/project/pcre/pcre/8.42/pcre-8.42.tar.gz
tar xf pcre-8.42.tar.gz
cd pcre-8.42
./configure
make && make install

安裝之前可以先檢查下自己的電腦有沒有已經安裝了。

然后就可以開始編譯nginx了:

tar xf nginx-1.16.0.tar.gz
cd nginx-1.16.0/
./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-http_ssl_module  --with-pcre=../pcre-8.42 --with-openssl=../openssl-1.0.2l --with-zlib=../zlib-1.2.11

make && make install

注意:執行./configure命令時的路徑要對應。

執行完上述步驟后,如果沒有錯誤,nginx就會已經部署在/user/local/nginx目錄。

cd /user/local/nginx
tree

# 啟動和關閉nginx
sudo /user/local/nginx/sbin/nginx
sudo /user/local/nginx/sbin/nginx -s  stop

3. 配置rtmp轉發

修改nginx.conf文件,以支持RTMP:

rtmp {  
    server {  
        listen 9999;  
  	#直播
        application live {  
            live on;  
        }
 
	#點播
	application vod {
            play /tmp/video;
    	}
 
    }  
}

修改完成之后重啟nginx:

/usr/local/nginx/sbin/nginx -s reload

通過netstat -ltn命令我們可以看到9999端口正在監聽.這便是我們配置的rtmp的服務端口。

FFmpeg推流

安裝ffmpeg

sudo apt-get install ffmpeg

推流

 ffmpeg -re -i input.mp4 -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://127.0.0.1:9999/live/test

ffplay拉流

ffplay rtmp://127.0.0.1:9999/live/test

如此,一個最簡單的直播系統就搭建完成了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM