Nginx+ffmpeg+java+hls+videojs實現RTSP轉HLS


安裝Nginx和nginx-rtmp-module

安裝make等命令

yum -y install gcc automake autoconf libtool make
yum install wget
yum install unzip

下載nginx-rtmp-module

mkdir ~/module     # 創建下載module的文件夾
cd ~/module
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip        # 下載模塊
unzip master.zip      # 解壓

安裝nginx

cd /
yum -y install pcre-devel openssl openssl-devel  #安裝依賴包
wget http://nginx.org/download/nginx-1.12.2.tar.gz   #下載nginx包
tar -zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --prefix=/opt/nginx-1.12.2 --with-http_stub_status_module --add-module=/root/module/nginx-rtmp-module-master --with-http_ssl_module    #--prefix=/opt/nginx-1.12.2為安裝位置
make
make install

修改nginx配置

rtsp轉rtmp配置

vi /opt/nginx-1.12.2/conf/nginx.conf

rtmp {
   # 轉流服務,可以存在多個,每個服務端口要不同
   server {
     listen 1935;
     application myapp{    #應用名,可以存在多個
        live on;
        record off;
        allow play all;
     }
   }
}

image
rtmp協議和http協議是同為應用層的不同協議,rtmp配置需要寫在http外面同級。

rtsp轉hls配置

在http配置中添加如下,直接把m3u8和切片文件放入nginx/hls文件夾下即可。

server {
  listen       8002;
  server_name  server_hls;

  location / {
    types {
       application/vnd.apple.mpegusr m3u8;
       video/mp2t ts;
    }
    root hls;    #8002端口服務的根目錄,nginx目錄下的hls目錄
    add_header Cache-Control no-cache;
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Headers "Origin, X-Requested-With,      Content-Type, Accept";
    add_header Access-Control-Methods "GET, POST, OPTIONS";
  }
}

image

重新載入nginx

/opt/nginx-1.9.5/sbin/nginx -s reload

出現如下問題,沒有nginx.pid
nginx: [error] open() "/opt/nginx-1.12.2/logs/nginx.pid" failed (2: No such file or directory)

/opt/nginx-1.9.5/sbin/nginx -c /opt/nginx-1.9.5/conf/nginx.conf

image

啟動nginx

/opt/nginx-1.9.5/sbin/nginx
/opt/nginx-1.9.5/sbin/nginx -s stop

image

安裝FFmpeg

yum install yasm -y   #安裝依賴,自動不行就手動
cd /opt
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg  #下載ffmpeg
cd ffmpeg
./configure --prefix=/usr/local/ffmpeg
make
make install

手動安裝依賴yasm

wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar zxvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure
make && make install

拷貝命令到/usr/bin

ls /usr/local/ffmpeg/
bin    include    lib    share
cp /usr/local/ffmpeg/bin/*   /usr/bin/

image

測試

RTSP轉RTMP

rtsp地址為源地址,rtmp后面為Nginx視頻服務器的ip+Nginx配置的1935端口/application名/視頻名

ffmpeg -rtsp_transport tcp  -i rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov -acodec aac -strict experimental -ar 44100 -ac 2 -b:a 96k -r 25 -b:v 500k -s 640*480 -f flv rtmp://222.222.221.188:1935/myapp/23

image

在vlc中打開轉換后的rtmp地址
rtmp://222.222.221.188:1935/myapp/23

image

image

RTSP轉HLS

HLS (HTTP Live Streaming)是Apple的動態碼率自適應技術。主要用於PC和Apple終端的音視頻服務。包括一個m3u(8)的索引文件,TS媒體分片文件和key加密串文件。
在上文nginx中/hls的root部分配置了html路徑,對應nginx目錄下的html文件夾,用於存儲m3u8文件及ts文件
此時ffmpeg的命令使用,將在nginx/hls文件夾生成test.m3u8文及切片視頻。

ffmpeg -f rtsp -rtsp_transport tcp -i rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov -codec copy -f hls -hls_list_size 10 -hls_wrap 20 -hls_time 15  /opt/nginx-1.9.5/hls/test.m3u8

image

vlc中輸入http://222.222.221.188:8002/test.m3u8
vlc不播放時,在瀏覽器輸入地址看看是否可以直接下載m3u8文件,如果不能下載檢查nginx根目錄下hls文件夾內是否生成文件,如果已經生成檢查conf/nginx.conf配置文件是否配置正確。

image


免責聲明!

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



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