1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Raspberry Pi Zero W】FFmpegでハードウェアエンコーダーを使う+動体検知カメラを動かす

Last updated at Posted at 2020-12-24

概要

Raspberry Pi Zero WでOSがbusterという組み合わせでの参考サイトが見つからなかったので覚え書きとして記録
明確な記載は見つからなかったが最新のFFmpegはRaspberryPiのハードウェアエンコーダに対応済み?
motionからFFmpegに渡す際、h264_omxを指定してもエラーが起きず。今後調査。

作業環境

  • ボード:Raspberry Pi Zero W
  • OS:Raspberry Pi OS Lite v5.4(buster 2020-8-20)
  • ストレージ:32GB
  • カメラ:Raspberry Pi Camera Module V1.3

参考サイト

https://ki-chi.jp/?p=769
http://akahana-1.hatenablog.jp/entry/2017/10/29/132822
https://www.kaoriya.net/blog/2017/10/07/
https://hombre-nuevo.com/microcomputer/raspberrypi/raspberrypi0030/

Raspbianのインストール、初期設定

※詳細は省略

  1. Raspberry Pi Imagerを使用してSDカードにOSを書き込み
  2. Raspberry Pi Zero WにSDカードを挿入して起動
  3. 初期設定
  4. SSHの有効化
  5. Windows PCからSSH接続
  6. sudo apt update & sudo apt upgrade

カメラの有効化

raspi-configで設定

$ sudo raspi-config
~ 別画面が開く ~
3.Interface Options -> P1.Camera -> yes

FFmpegのインストール

$ sudo apt install ffmpeg
~ 省略 ~
$ ffmpeg -encoders 2> /dev/null | grep omx
 V..... h264_omx             OpenMAX IL H.264 video encoder (codec h264)

motionのインストール

$ sudo apt install motion

motionの設定

録画条件等の設定

録画ファイルの保存先ディレクトリのパーミッションは適宜変更
今回はテストのため制限なしとする

$ mkdir motion
$ chmod -R 777 motion
$ sudo nano /etc/motion/motion.conf

今回は以下の内容で設定

daemon on  # サービスとして実行
norm 1     # NTSC
width 640  # 記録する映像の幅
height 480  # 記録する映像の高さ
framerate 30  # フレームレート(性能が足りなければこれより低くなる)
threshold 1500  # 動体検知の閾値。変化のあったピクセル数。適宜調整しつつ運用。

ffmpeg_video_codec mp4 # mp4で出力

use_extpipe on  # 記録した動画を外部プログラムに渡す
extpipe ffmpeg -y -f rawvideo -pix_fmt yuv420p -video_size %wx%h -framerate %fps -i pipe:0 -vcodec h264_omx -preset ultrafast -f mp4 %f.mp4  #FFmpegにrawvideoを投げつけてh264_omxでエンコード。

locate_motion_mode on  # 認識した動体の位置を示す。デフォルトでは長方形で図示。

target_dir /home/pi/motion  # 保存先ディレクトリ

stream_maxrate 30  # ストリーミングで滑らかに動くため(性能が足りなければこれより低くなる)
stream_localhost off  # localhost以外からでもストリーミングにアクセスできるようにする
stream_auth_method 1  # ストリーミングにBasic認証をかける
stream_authentication username:password  # 認証時のIDとPass

motionの動作テスト

$ sudo motion -n
[10995336:motion] [NTC] [ALL] conf_load: Processing thread 0 - config file /etc/motion/motion.conf
[10995336:motion] [NTC] [ALL] motion_startup: Motion 4.1.1 Started
[10995336:motion] [NTC] [ALL] create_path: creating directory /var/log/motion
[10995336:motion] [NTC] [ALL] motion_startup: Logging to file (/var/log/motion/motion.log)
~ 以下省略 ~

ブラウザでhttp://[RaspberryPiのIPアドレス or ホスト名]:8081 にアクセス、
事前に設定した username password を入力するとカメラの映像が表示される。
ターミナルでCtrl+Cを押すと終了。

動作確認

今回の環境では4fps前後しか出なかった。
ググった結果、近い環境で画質とフレームレートが高い状態で撮影している事例が見つかったため、
設定の追い込みが足りないと思われる。引き続き調査。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?