0
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 1 year has passed since last update.

Raspberry PiでGStreamerを使ってUVCカメラの映像をファイル保存

Posted at

1. 概要

Raspberry PiでGStreamerを使ってUVCカメラの映像をファイル保存(簡易録画)

2. 機材など

  • Raspberri Pi 4B
  • OS : Raspberry Pi OS (Bullseye 64bit)
  • UVCカメラ

3. UVCカメラの映像をファイルとして保存する

3.1. YUVをそのまま保存

解像度(1280と720)は、カメラの性能に合わせて変更可能。
フレームレート(10/1)は、カメラの性能に合わせて変更可能。30fpsなら30/1にする。
「timeout 10」は、10秒間したら停止。

timeout 10 gst-launch-1.0 v4l2src device=/dev/video0 ! video/x-raw,width=1280,height=720,framerate=10/1 ! \
videoconvert ! filesink location=test.yuv

Ctrl+Cで停止させたいときは -e をつける。

gst-launch-1.0 -e v4l2src device=/dev/video0 ! video/x-raw,width=1280,height=720,framerate=10/1 ! \
videoconvert ! filesink location=test.yuv

※これらのファイルはVLC playerとかでは再生できない。画が壊れているわけではない。YUV専用の再生アプリが必要。

3.2. MJPEGをそのまま保存

解像度(640と480)は、カメラの性能に合わせて変更可能。
フレームレート(5/1)は、カメラの性能に合わせて変更可能。30fpsなら30/1にする。
「timeout 10」は、10秒間したら停止。

timeout 10 gst-launch-1.0 v4l2src device=/dev/video0 ! "image/jpeg,width=640,height=480,framerate=5/1" ! \
filesink location=test.mjpeg

Ctrl+Cで停止させたいときは -e をつける。

gst-launch-1.0 -e v4l2src device=/dev/video0 ! "image/jpeg,width=640,height=480,framerate=5/1" ! \
filesink location=test.mjpeg

※これらのファイルはVLC playerとかでは再生できない。画が壊れているわけではない。MJPEG用の再生アプリが必要。

3.3 YUVを変換してH.264(mp4)として保存

解像度(1280と720)は、カメラの性能に合わせて変更可能。但し, H.264のエンコーダに合わない解像度やフレームレートだと期待する結果にならないことがある。
フレームレート(10/1)は、カメラの性能に合わせて変更可能。30fpsなら30/1にする。
「timeout 10」は、10秒間したら停止。

timeout 10 gst-launch-1.0 v4l2src device=/dev/video0 ! video/x-raw,width=1280,height=720,framerate=10/1 ! \
videoconvert ! v4l2h264enc ! 'video/x-h264,level=(string)4' ! h264parse ! mp4mux ! filesink location=test.mp4

Ctrl+Cで停止させたいときは -e をつける。

gst-launch-1.0 -e v4l2src device=/dev/video0 ! video/x-raw,width=1280,height=720,framerate=10/1 ! \
videoconvert ! v4l2h264enc ! 'video/x-h264,level=(string)4' ! h264parse ! mp4mux ! filesink location=test.mp4

3.4. MJPEGを変換してH.264(mp4)として保存

分からない。何やっても再生できないファイルができてしまう。
以下はその失敗コマンド

gst-launch-1.0 -e v4l2src device=/dev/video0 ! image/jpeg,width=1280,height=720,framerate=10/1 ! \
queue ! jpegparse ! v4l2jpegdec ! v4l2h264enc ! 'video/x-h264,level=(string)4.1' ! h264parse ! \
mp4mux ! filesink location=test.mp4

3.5. MJPEGを変換してH.264(mkv)として保存

mp4の代わりにmkvのエレメントを使ったら再生できるファイルができた。
どう違うんだ・・・

gst-launch-1.0 -e v4l2src device=/dev/video0 ! image/jpeg,width=1280,height=720,framerate=10/1 ! \
queue ! jpegparse ! v4l2jpegdec ! v4l2h264enc ! 'video/x-h264,level=(string)4.1' ! h264parse ! \
matroskamux ! filesink location=test.mkv

4. 補足

入力元としての解像度やフレームレートを間違うと分かりづらいエラーとなって現れる。使用できる解像度やフレームレートを以下のコマンドで確認した方がよい。

v4l2-ctl -d /dev/video0 --list-formats-ext

以上

0
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
0
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?