Raspberry Pi 3 + Camera Module 2 :Video Streaming


測試1:利用Youtube或者Facebook (來源:國外教學)

Step 1 :先開啟樹梅派鏡頭使用(raspi-config)

Step 2 :安裝套件
  1. sudo apt-get install libav-tools

Step 3 :到Youtube或Facebook直播設定的地方取得KEY

Step 4 :Youtube的話打開樹梅派終端機,輸入
  1. raspivid -o - -t 0 -vf -hf -fps 20 -b 600000 -w 1280 -h 720 | ffmpeg -re -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f h264 -i - -vcodec copy -acodec aac -ab 128k -g 10 -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/你的直播KEY

Step 5 :Facebook的話一樣打開樹梅派終端機,輸入
  1. raspivid -o - -t 0 -vf -hf -fps 10 -b 500000 | ffmpeg -re -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f h264 -i - -vcodec copy -acodec aac -ab 128k -g 50 -strict experimental -f flv rtmp://rtmp-api.facebook.com:80/rtmp/你的直播KEY

Step 6 :實際測試都可以使用,但Youtube延遲較嚴重,可透過參數改變來改善,像是-fps後面的偵數、-b後面的bit rate、-g後面接的是關鍵Frame每隔幾偵加入一個(因為是H.264,如果影像一直變動,建議改小,讓畫面更新快一點)、可在bit rate後面加入-w和-h,這是畫面解析度,不更改的話預設是1920*1080


測試2:利用樹梅派開啟HTTP SERVER來串流(除非樹梅派有實體IP,不然只有區網下可見)

Step 1 :創建一個python檔

Step 2 :輸入以下程式 (來源:國外教學)
  1. import io
  2. import picamera
  3. import logging
  4. import socketserver
  5. from threading import Condition
  6. from http import server
  7.  
  8. PAGE="""\
  9. <html>
  10. <head>
  11. <title>Raspberry Pi - Surveillance Camera</title>
  12. </head>
  13. <body>
  14. <center><h1>Raspberry Pi - Surveillance Camera</h1></center>
  15. <center><img src="stream.mjpg" width="640" height="480"></center>
  16. </body>
  17. </html>
  18.  
  19. """
  20.  
  21. class StreamingOutput(object):
  22.     def __init__(self):
  23.         self.frame = None
  24.         self.buffer = io.BytesIO()
  25.         self.condition = Condition()
  26.     def write(self, buf):
  27.         if buf.startswith(b'\xff\xd8'):
  28.             # New frame, copy the existing buffer's content and notify all
  29.             # clients it's available
  30.             self.buffer.truncate()
  31.             with self.condition:
  32.                 self.frame = self.buffer.getvalue()
  33.                 self.condition.notify_all()
  34.             self.buffer.seek(0)
  35.         return self.buffer.write(buf)
  36. class StreamingHandler(server.BaseHTTPRequestHandler):
  37.     def do_GET(self):
  38.         if self.path == '/':
  39.             self.send_response(301)
  40.             self.send_header('Location', '/index.html')
  41.             self.end_headers()
  42.         elif self.path == '/index.html':
  43.             content = PAGE.encode('utf-8')
  44.             self.send_response(200)
  45.             self.send_header('Content-Type', 'text/html')
  46.             self.send_header('Content-Length', len(content))
  47.             self.end_headers()
  48.             self.wfile.write(content)
  49.         elif self.path == '/stream.mjpg':
  50.             self.send_response(200)
  51.             self.send_header('Age', 0)
  52.             self.send_header('Cache-Control', 'no-cache, private')
  53.             self.send_header('Pragma', 'no-cache')
  54.             self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
  55.             self.end_headers()
  56.             try:
  57.                 while True:
  58.                     with output.condition:
  59.                         output.condition.wait()
  60.                         frame = output.frame
  61.                     self.wfile.write(b'--FRAME\r\n')
  62.                     self.send_header('Content-Type', 'image/jpeg')
  63.                     self.send_header('Content-Length', len(frame))
  64.                     self.end_headers()
  65.                     self.wfile.write(frame)
  66.                     self.wfile.write(b'\r\n')
  67.             except Exception as e:
  68.                 logging.warning(
  69.                     'Removed streaming client %s: %s',
  70.                     self.client_address, str(e))
  71.         else:
  72.             self.send_error(404)
  73.             self.end_headers()
  74. class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
  75.     allow_reuse_address = True
  76.     daemon_threads = True
  77. with picamera.PiCamera(resolution='640x480', framerate=24) as camera:
  78.     output = StreamingOutput()
  79.     camera.start_recording(output, format='mjpeg')
  80.     try:
  81.         address = ('', 8000)
  82.         server = StreamingServer(address, StreamingHandler)
  83.         server.serve_forever()
  84.     finally:
  85.         camera.stop_recording()

Step 3 :同個網段下的裝置,只要在瀏覽器輸入"樹梅派IP:8000",就可以瀏覽到即時影像(幾乎0延遲)

留言