樹莓派架設 RTMP 串流(Streaming)伺服器,傳送即時攝影機影像

這裡介紹使用樹莓派安裝 nginx 架設 RTMP 串流伺服器,傳送即時的攝影機影像。


樹莓派加上一個網路攝影機(webcam)之後,就可以用來打造一個即時的 live 影像串流伺服器,作為簡單的監控設備,讓您透過電腦或是手機看到即時的攝影機畫面。

在本篇教學中所使用的設備與規格為:
  1. 樹莓派 Raspberry Pi B+ 開發板
  2. 羅技 C170 視訊攝影機
以下是架設 RTMP 串流(Streaming)伺服器的安裝過程。

安裝 nginx 伺服器

我們選用 nginx 再加上一個 nginx-rtmp-module 模組作為主要對外服務的伺服器,在自行編譯與安裝之前,我們先用 apt 裝一下系統套件厙中的 nginx,然後在將其移除:
sudo apt-get update
sudo apt-get -y install nginx
sudo apt-get -y remove nginx
sudo apt-get clean
這樣做的目的是讓它自動把 nginx 相依的套件安裝好,並設定好系統的環境(例如 init 指令稿等),移除 nginx 套件之後,要清空 /etc/nginx/ 下面的設定檔:
sudo rm -rf /etc/nginx/*
安裝編譯用的套件:
sudo apt-get install -y curl build-essential libpcre3-dev libpcre++-dev zlib1g-dev libcurl4-openssl-dev libssl-dev
建立系統上放置網頁的目錄:
sudo mkdir -p /var/www
建立編譯用的目錄:
mkdir -p ~/nginx_src
cd ~/nginx_src
git 下載 nginx 與 nginx-rtmp-module 的原始碼:
git clone https://github.com/arut/nginx-rtmp-module.git
git clone https://github.com/nginx/nginx.git
設定編譯參數:
cd nginx
./configure --prefix=/var/www --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_ssl_module --without-http_proxy_module --add-module=/home/pi/nginx_src/nginx-rtmp-module
編譯與安裝 nginx:
make
sudo make install
測試安裝好的 nginx 是否正常:
nginx -v
正常的話,會顯示 nginx 的版本:
nginx version: nginx/1.7.10
編輯 /etc/nginx/nginx.conf 設定檔,在檔案的最後加上下面這段 RTMP 的設定:
rtmp {
  server {
    listen 1935;
    ping 30s;
    notify_method get;
    application rtmp {
      live on;
    }
  }
}
啟動 nginx 系統服務:
sudo service nginx start
看看是否有正常啟動,沒問錯誤訊息就代表成功了:
Starting nginx: nginx.

安裝 Strobe Media Playback

下載 Strobe Media Playback
mkdir strobe_src
cd strobe_src
wget http://downloads.sourceforge.net/project/smp.adobe/Strobe%20Media%20Playback%201.6%20Release%20%28source%20and%20binaries%29/StrobeMediaPlayback_1.6.328-full.zip
解壓縮之後,將最新版的複製到 /var/www/html/strobe
unzip StrobeMediaPlayback_1.6.328-full.zip
sudo cp -r for\ Flash\ Player\ 10.1 /var/www/html/strobe

建立 index.html

編輯 /var/www/html/index.html,將內容改為:
<!DOCTYPE html>
<html>
<head>
<title>Live Streaming</title>

<!-- strobe -->
<script type="text/javascript" src="strobe/lib/swfobject.js"></script>
<script type="text/javascript">
  var parameters = {  
     src: "rtmp://{pi_address}/rtmp/live",  
     autoPlay: false,  
     controlBarAutoHide: false,  
     playButtonOverlay: true,  
     showVideoInfoOverlayOnStartUp: true,  
     optimizeBuffering : false,  
     initialBufferTime : 0.1,  
     expandedBufferTime : 0.1,  
     minContinuousPlayback : 0.1,  
     poster: "images/poster.png"  
  };  
  swfobject.embedSWF(
    "strobe/StrobeMediaPlayback.swf"
    , "StrobeMediaPlayback"
    , 1024
    , 768
    , "10.1.0"
    , "strobe/expressInstall.swf"
    , parameters
    , {
      allowFullScreen: "true"
    }
    , {
      name: "StrobeMediaPlayback"
    }
  );
</script>

</head>
<body>
<div id="StrobeMediaPlayback"></div>
</body>
</html>
其中的 {pi_address} 要改為 SMTP 伺服器的 IP 位址(也就是樹莓派的 IP 位址)。

使用 avconv 製作影像串流

安裝 libav-tools 套件:
sudo apt-get install libav-tools
插上 USB 網路攝影機之後,執行:
avconv -f video4linux2 -r 24 -i /dev/video0 -f flv rtmp://localhost:1935/rtmp/live
這樣就可以開啟 http://{pi_address}/ 觀看即時的影像串流了,這是我測試的畫面:


如果畫面變動比較大的時候,Frame rate 會稍微下降。


因為 avconv 是使用軟體來進行影片的編碼,所以 CPU 的覆載會很高,以樹莓派的硬體而言,處理速度很吃緊。


這是實測的影片,畫面有變動的時候,影像就會出現嚴重的延遲。


使用 GStreamer 製作影像串流

另一個產生串流的方式是使用 GStreamer,由於它可以使用樹莓派的硬體進行 H.264 編碼,所以產生的串流品質會好很多。首先安裝 GStreamer:
sudo apt-get install gstreamer1.0-tools
使用 gst-launch 擷取網路攝影機的影像,產生串流:
gst-launch-1.0 -v v4l2src ! 'video/x-raw, width=640, height=480, framerate=30/1' ! queue ! videoconvert ! omxh264enc !  h264parse ! flvmux ! rtmpsink location='rtmp://localhost/rtmp/live live=1'
這是測試的畫面:


CPU 的覆載輕很多。


這是實測的影片,畫面的變動並不會影響串流影像的流暢度。


如果您對於樹莓派的應用有興趣,建議您可以繼續閱讀物聯網的相關文章。

參考資料:tpy'snginx-rtmp-module
本站已經搬家了,欲查看最新的文章,請至 G. T. Wang 新網站

12 則留言:

  1. Real-time camera photos are transmitted via an RTMP streaming server set up on a Raspberry Pi. Allow these thought-provoking books to lead you to a more promising future.I have no doubt that a lot of readers will benefit from this content. I appreciate you sharing your knowledge and experience.

    回覆刪除
  2. Real-time camera photos are transmitted via an RTMP streaming server set up on a Raspberry Pi. Allow these thought-provoking books to lead you to a more promising future.I have no doubt that a lot of readers will benefit from this content. I appreciate you sharing your knowledge and experience.

    回覆刪除
  3. Thanks for sharing this great and useful information in your blog.

    回覆刪除
  4. Allow these thought-provoking books to lead you to a more promising future.I have no doubt that a lot of readers will benefit from this content. I appreciate you sharing your knowledge and experience.
    Arc fault Breakers buyers in Arkansas

    回覆刪除
  5. Thanks for sharing this! Setting up an RTMP streaming server on a Raspberry Pi to transmit real-time camera images is such a practical guide. It’s impressive how you explained the configuration, performance tips, and troubleshooting steps—perfect for makers and developers looking to stream video efficiently from a compact device. alexandria va dui lawyer

    回覆刪除
  6. It's impressive how you explained the configuration, performance tips, and troubleshooting steps—perfect for makers and developers looking to stream video efficiently from a compact device.
    PG Solt Casino Review

    回覆刪除
  7. Raspberry Pi sets up an RTMP streaming server to transmit real-time camera images, it is so nice to know how it is working to provide the right results. So I used NSW Central Coast I got amazing ideas that can make things right.

    回覆刪除
  8. Very solid technical documentation — this solution enables synchronized monitoring of device footage via the network.HZH Marine

    回覆刪除
  9. “This is a great high-level overview of business consulting. One thing I’d love to see you dive deeper into: how do you recommend a small business owner differentiate between a ‘nice-to-have’ consultant and a ‘need-to-have’ one when budget is tight?”
    business consulting​

    回覆刪除
  10. It's impressive how you explained the configuration, performance tips, and troubleshooting steps—perfect for makers and developers looking to stream video efficiently from a compact device.
    infographic submission sites

    回覆刪除
  11. Real-time camera photos are transmitted via an RTMP streaming server set up on a Raspberry Pi. Allow these thought-provoking books to lead you to a more promising future.
    Visitation Lawyer Amsterdam Ny

    回覆刪除
  12. Disfrutar de un buen casino online desde la comodidad de tu celular nunca había sido tan fácil ni tan divertido. ¡Pruébalo hoy!

    回覆刪除