概要
手元にあったRaspberry pi 4でストリーミング再生できるカメラを設置しようと思いったので、その手順をまとめます。
準備
- Raspberry Pi 4 Model B
- Micro SD(初回のみUSBブート設定に必要)
- USB
- webカメラ(C960)
参考:https://www.amazon.co.jp/gp/product/B07MBQ1PT3/ - ubuntu server 20.10(RPI 3/4/400)
手順
まずは、下記の記事を参考に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