Skip to content

Instantly share code, notes, and snippets.

@nezuppo
Created February 25, 2024 11:56
Show Gist options
  • Save nezuppo/35f37fe49a1b82586a534e8304e392fa to your computer and use it in GitHub Desktop.
Save nezuppo/35f37fe49a1b82586a534e8304e392fa to your computer and use it in GitHub Desktop.

とりあえずラズパイカメラで動画ストリーミング

はじめに

数年ぶりにラズパイ + ラズパイカメラ + mjpg-streamer で Web ストリーミングカメラを作ろうとしたら手順が若干変わっていたのでやったことを書きとめてみた

環境

ラズパイ 3 Model B

$ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 11 (bullseye)
Release:        11
Codename:       bullseye
$ uname -a
Linux jobpi 6.1.21-v7+ #1642 SMP Mon Apr  3 17:20:52 BST 2023 armv7l GNU/Linux
  • armv7l となっているので 64bit でなく 32bit OS らしい

ラズパイカメラは確か v1.3

以下構築手順っぽいもの

ラズパイにラズパイカメラを挿してラズパイを起動

とりあえず、ラズパイカメラを認識していないこと確認

$ vcgencmd get_camera
supported=0 detected=0, libcamera interfaces=0
  • supported=0 detected=0 なので認識していないっぽい
$ raspistill -o ./image.jpg
ERROR: the system should be configured for the legacy camera stack

$ raspivid -o ./video.h264 -t 10000 -w 960 -h 540 -b 500000 -fps 31
ERROR: the system should be configured for the legacy camera stack
  • カメラからの画像取得も動画取得もエラー

カメラを有効化

$ sudo raspi-config
  • 3 Interface Options を選択
  • I1 Legacy Camera を選択
  • Would you like to enable legacy camera support?Yes

ラズパイをリブート

$ vcgencmd get_camera
supported=1 detected=1, libcamera interfaces=0
  • supported=1 detected=1 になった

試しに静止画を取得

$ raspistill -o ./image.jpg

試しに動画を取得

$ raspivid -o ./video.h264 -t 10000 -w 960 -h 540 -b 500000 -fps 31
  • デフォルトで h264 形式で保存されるらしい
  • 10秒間 (10000ms) の動画を保存
  • サイズ 960 x 540
  • ビットレート 500000 Bits/s
  • フレームレート 31 frames per second
  • Windows ではこのファイルの再生方法がわからなかったが、ラズパイのデスクトップでダブルクリックしたら再生できた

以下実行

$ sudo apt update
$ sudo apt full-upgrade
$ sudo apt autoremove
$ sudo shutdown -r now
$ sudo apt install \
  cmake \
  libv4l-dev \
  libjpeg-dev \
  imagemagick

$ mkdir tmp
$ cd tmp/
$ git clone https://github.com/jacksonliam/mjpg-streamer.git
$ cd mjpg-streamer/mjpg-streamer-experimental
$ make
$ sudo make install
$ /usr/local/bin/mjpg_streamer -i "input_uvc.so -f 15 -r 1280x720 -rot 90" -o "output_http.so -w /usr/local/share/mjpg-streamer/www"
  • 以前は input_raspicam.so を指定していたのですがエラーになるので input_uvc.so を指定してみたらうまくいった
  • -rot 90 で画像を 90° 回転

ブラウザで以下にアクセス

  • http://<ラズパイの IP とかホスト名とか>:8080/?action=stream

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment