LoginSignup
54
57

More than 5 years have passed since last update.

raspberry pi で USB camera から h.264 動画

Last updated at Posted at 2015-01-07

野暮用があって、監視カメラ的なものをでっち上げることにした。手元に転がっている機材で。

  • raspberry pi model B
  • USB camera (microsoft LifeCam HD-5000)

前準備

まず raspbian のイメージを SD カードに書き込む。

基本アップデートする。

sudo apt-get update -y
sudo apt-get dist-upgrade -y

keyboard を jp106 に。

sudo apt-get install console-data
vi /etc/default/keyboard

gstreamer を入れる。raspberry pi では openmax plugin を使ってハードウェアエンコードしないと CPU が辛いらしい。gstreamer1.0 向けにビルドされたものがあるので、それを使う。

sudo apt-get install gstreamer1.0 gstreamer1.0-tools

gstreamer あれこれ

omx が入っているかどうかは次のようにして確認する。

gst-inspect-1.0 | grep omx

USB camera を差し込んでおいてから、動画取り込みモードでサポートされている形式をあらかじめ確認しておく。

v4l2-ctl --list-formats-ext

とりあえず画面に出して確認する。gstreamer0.10 で ffmpegcolorspace となっていた部分は、gstreamer1.0 では videoconvert を使う。

gst-launch-1.0 v4l2src ! videoconvert ! ximagesink

USB camera 側の動作モードを指定したければ、次のようにする。

gst-launch-1.0 v4l2src ! video/x-raw,width=640,height=480,framerate=15/2 ! videoconvert ! ximagesink

最終的にはこうした。omxh264enc を使うのがポイント。framerate=30 にハックしているのは、一般的なプレーヤで再生できるようにするため。timeout を使って一日間だけ録画するようにしてみた。crontab で毎日起動すればいいかな。あとはファイル容量を見て調整しよう。

timeout 86100 gst-launch-1.0 -v v4l2src ! videorate ! video/x-raw,framerate=30/1 ! clockoverlay ! omxh264enc ! h264parse ! mpegtsmux ! filesink location=`date -I`.ts

TIPS

たまたま手元に model B と model B+ の両方があるので、SD カードにバリバリ書き込む作業のときは B+ でやって、一通り終わったら B に差し替えて使っています。書き込み途中で電源不安定になって失敗するリスクが減ります。

54
57
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
54
57