LoginSignup
3
2

More than 3 years have passed since last update.

webカメラでストリーミング再生

Last updated at Posted at 2021-03-07

概要

手元にあったRaspberry pi 4でストリーミング再生できるカメラを設置しようと思いったので、その手順をまとめます。

準備

手順

まずは、下記の記事を参考にRaspberry piにubuntu server 20.04をセットアップする。
Raspberry Pi 4でUSBブートのubuntu serverを建てよう

無事OSのインストールが完了したらストリーミング再生に必要なライブラリをインストールする。

sudo apt update
sudo apt install nginx
sudo apt install libnginx-mod-rtmp

sudo apt install v4l-utils
sudo apt install alsa-utils
sudo apt install ffmpeg

nignx の設定

/etc/conf.d/rtmpの中身を書く。ファイル名をrtmp.confにするとエラーを吐くので注意。

rtmp {
    server {
        listen 1935;
        chunk_size 4096;
        allow play all;
        access_log /var/log/nginx/rtmp_access.log;

        application live {
            live on;
            hls on;
            record off;
            hls_path /var/www/html/live/hls;
            hls_fragment 1s;
            hls_type live;
        }
    }
}

/etc/nginx/nginx.confに下記の行を追記。

include /etc/nginx/conf.d/rtmp;

htmlファイルの作成

/var/www/html/以下に雛形のindex.htmlを作成する。

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="utf-8"/>
  <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
</head>

<body>
  <video id="video" controls width="100%"></video>
  <script>
    if(Hls.isSupported()) {
      var video = document.getElementById('video');
      var hls = new Hls();
      hls.loadSource('live/hls/stream.m3u8');
      hls.attachMedia(video);
      hls.on(Hls.Events.MANIFEST_PARSED,function() {
      video.play();
    });
   }
  </script>
</body>
</html>

ストリーミング動画の保存場所を作成

また、/var/www/html/以下にliveフォルダを作成しシンボリックリンクを貼る。

mkdir live
cd live
sudo ln -s /dev/shm hls

ストリーミング再生

ffmpeg \
        -f alsa -ac 1 \
        -thread_queue_size 8192 \
        -i hw:2 \
        -i /dev/video0 \
        -vcodec libx264 \
        -vsync 1 -g 16 \
        -c:a aac -ar 44100 -af "volume=30dB" \
        -f flv rtmp://localhost/live/stream

音声デバイスの確認

arecord -l

webカメラの確認

v4l2-ctl --list-device
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2