Rtsp通過Nginx和FFmpeg轉碼Rtmp,網頁播放rtmp

1、安裝FFmpeg

tar zxvf ffmpeg-4.1.tar.gz
cd ffmpeg-4.1/
./configure
sudo make install

2、安裝Nginx
(1) 安裝相關的依賴包

yum install gcc
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
sudo apt-get install openssl libssl-dev

(2)下載安裝nginx

wget http://nginx.org/download/nginx-1.13.10.tar.gz
#解壓
tar -zxvf nginx-1.13.10.tar.gz
#下載RTMP(到HOME目錄)
git clone https://github.com/arut/nginx-rtmp-module.git
#進入NGINX解壓目錄
cd nginx-1.13.10
#配置
./configure --prefix=/usr/local/nginx --add-module=~/nginx-rtmp-module --with-http_ssl_module
#編譯
make
#安裝
sudo make install

(3)查看是否安裝成功

cd /usr/local/nginx/sbin
sudo ./nginx -t

(4)成功之後配置

#打開nginx配置文件
cd /usr/local/nginx/conf/
sudo gedit nginx.conf
打開文檔下拉至末尾:
rtmp{
    server{
    listen 1935;

        application myapp{
          live on;
          record off;
        }
        application hls{
          live on;
          hls on;
          hls_path nginx-rtmp-module/hls;
          hls_cleanup off;
        }
    }
}

3、使用ffmpeg進行轉碼

#默認爲UDP協議傳輸轉碼
ffmpeg -i "rtsp://xxxxxxx" -f flv -r 25 -s 1080*720 -an "rtmp://localhost:1935/hls/mystream"
#TCP協議轉碼
ffmpeg -re -rtsp_transport tcp -i "rtsp://xxxxxxxxxx" -f flv -r 25 -s 1080*720 -an "rtmp://localhost:1935/hls/mystream"

以上兩個可能再播放的時候流暢度上不太好,畫質也不好

#tcp協議轉碼,插件解碼,但是插件需要安裝
ffmpeg -re -rtsp_transport tcp -i "rtsp://xxxxxxxxxxxx" -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 "rtmp://localhost:1935/myapp/mystream"

3、可以用VLC播放器測試rtmp是否轉碼成功
轉碼地址爲:

rtmp://localhost:1935/hls/mystream
rtmp://localhost:1935/myapp/mystream

ps:以上兩個路徑上面的hls 和 myapp都是在nginx配置的,路徑後面的可以隨意的改動,它的作用只是在爲碼流指定方向
4、網頁播放
將上面得到的rtmp路徑填寫到html文件中,就可以播放視頻了。不過最好用google瀏覽器。videojs-flash.js和videojs-contrib-hls.js有的時候,鏈接訪問不到,所以需要自己去github上查找,並下載下來,加入本地或者使用一個可以使用的遠程 鏈接

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>video.js</title>
    <link href="https://unpkg.com/[email protected]/dist/video-js.min.css" rel="stylesheet">
    <script src="https://unpkg.com/[email protected]/dist/video.min.js"></script>
    <script src="https://unpkg.com/videojs-flash/dist/videojs-flash.js"></script>
    <script src="https://unpkg.com/videojs-contrib-hls/dist/videojs-contrib-hls.js"></script>
  </head>
  <body>
    <video id="my-player" class="video-js" controls>
        <source src="rtmp://localhost:1935/myapp/stream-name" type="rtmp/flv">
        <p class="vjs-no-js">
          not support
        </p>
    </video>
    <script type="text/javascript">
      var player = videojs('my-player',{
        width:400,
        heigh:200
      });
    </script>
  </body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章