ラズパイの見守りモニタでタイムラプス動画を同時に撮影する

student taro
見習いタロー

作った見守りモニターで何か面白い機能作れないですか?

koala
プロコアラ

じゃあ、タイムラプス動画を撮影できるようにしよう!

この記事を書いた人
koala
プロコアラ@エンジニアブロガー
10年間エンジニアをしており、副業でWebサイトやWebサービスを作っています。
一時期資格取得にハマりTOEIC860点オーバー、応用情報処理は取得。休日はラズパイをいじるコアラ好きです。

 

概要

前回、ラズベリーパイ3にUSBカメラを接続して、MJPG-streamer経由でリアルタイムにカメラの映像をブラウザ上に表示できるようにしました。

MJPG-Streamerで動画を撮影しながらタイムラプス動画が撮影できるようにしましょう。

 

タイムラプスとは?

タイムラプスは静止画を繋ぎ合わせてコマ送り動画のように撮影された動画です。

1秒や1分など、ある一定の間隔で撮影した静止画を繋ぎ合わせることで不思議な映像を作ることができます。

iPhoneでもタイムラプス動画を撮影する機能がついています。

動画はキヤノンのカメラで撮影したものです。ここまで綺麗なものは撮影できませんが、このような動画が作れるようにします。

 

 

使用するもの

ラズパイ

Webカメラ

 

静止画の撮影スクリプト作成

MJPG-streamerのsnapshot機能を使用して静止画を取得します。

cd ~
mkdir timelapse
cd timelapse
mkdir img
nano timelapse.sh

 

timelapse.shの中身

 #!/bin/bash
FILENAME=`date +%Y%m%d%H%M`
wget -O /home/pi/timelapse/img/$FILENAME.jpg http://yourdomain.clear-net.jp:8080/?action=snapshot

“で囲むとコマンドの実行結果を取得できます。

前回作成したMJPG-StreamerのDynamic DNSのドメインを指定しています。?action=snapshotで静止画を取得できます

wgetでファイルにします

 

このスクリプトをcronに登録し、自動で撮影するようにします。
下記コマンドは1分おきに撮影するものです。1日おきに撮影しても良いかなと思います。

crontab -e

# m h  dom mon dow   command
*/1 * * * * /home/pi/timelapse/timelapse.sh

 

chmodで実行権限を付けておきましょう

pi@raspberrypi:~/timelapse $ ls -la
合計 16
drwxr-xr-x  3 pi pi 4096  6月 11 04:07 .
drwxr-xr-x 33 pi pi 4096  6月 11 04:09 ..
drwxr-xr-x  2 pi pi 4096  6月 11 04:07 img
-rw-r--r--  1 pi pi  120  6月 11 04:07 timelapse.sh
pi@raspberrypi:~/timelapse $ chmod +x timelapse.sh 
pi@raspberrypi:~/timelapse $ ls -la
合計 16
drwxr-xr-x  3 pi pi 4096  6月 11 04:07 .
drwxr-xr-x 33 pi pi 4096  6月 11 04:09 ..
drwxr-xr-x  2 pi pi 4096  6月 11 04:07 img
-rwxr-xr-x  1 pi pi  120  6月 11 04:07 timelapse.sh

 

imgディレクトリに画像が保存されていきます

1分おきの場合はどんどん容量が大きくなってしまうため、時間は調整しましょう

pi@raspberrypi:~/timelapse/img $ ls
201906110420.jpg  201906110421.jpg  201906110422.jpg  201906110423.jpg

 

crontab -eコマンドで調整します。1時間おきの場合は以下のようになります。

# m h dom mon dow command
* */1 * * * /home/pi/timelapse/timelapse.sh

 

 

ファイル名のリネーム

作成した静止画を一気にリネームします。lsで作成したjpgファイル一覧をパイプでawkコマンドに渡します。$0には渡されたjpgファイルが入り、NRが連番になります。

ls *.jpg | awk '{ printf "mv %s timelapse%03d.jpg\n", $0, NR }' | sh

 

awkでmvコマンドを作ります

pi@raspberrypi:~/timelapse/img $ ls *.jpg | awk '{printf "mv %s timelapse%03d.jpg\n", $0, NR}'
mv 201906110420.jpg timelapse001.jpg
mv 201906110421.jpg timelapse002.jpg
mv 201906110422.jpg timelapse003.jpg
mv 201906110423.jpg timelapse004.jpg
mv 201906110424.jpg timelapse005.jpg
mv 201906110425.jpg timelapse006.jpg
mv 201906110426.jpg timelapse007.jpg
mv 201906110427.jpg timelapse008.jpg
mv 201906110428.jpg timelapse009.jpg

 

さらにそれをshに渡してコマンドを実行します。

pi@raspberrypi:~/timelapse/img $ ls *.jpg | awk '{printf "mv %s timelapse%03d.jpg\n", $0, NR}' | sh
pi@raspberrypi:~/timelapse/img $ ls
timelapse001.jpg  timelapse003.jpg  timelapse005.jpg  timelapse007.jpg  timelapse009.jpg  timelapse011.jpg
timelapse002.jpg  timelapse004.jpg  timelapse006.jpg  timelapse008.jpg  timelapse010.jpg  timelapse012.jpg

 

タイムラプス動画の作成

ツールのインストール

Raspbian Jessieでは、mencoderがインストールできないため、avconvを使用します。
下記コマンドでインストールできます。

sudo apt-get update
sudo apt-get install libav-tools

 

動画の作成

下記コマンドを実行します。

avconv -y -f image2 -r 10 -i timelapse%03d.jpg -aspect 4:3 -s '640x480' timelapse.mp4

 

avconvのオプションを載せておきます

Print help / information / capabilities:
-L                  show license
-h topic            show help
-? topic            show help
-help topic         show help
--help topic        show help
-version            show version
-buildconf          show build configuration
-formats            show available formats
-devices            show available devices
-codecs             show available codecs
-decoders           show available decoders
-encoders           show available encoders
-bsfs               show available bit stream filters
-protocols          show available protocols
-filters            show available filters
-pix_fmts           show available pixel formats
-layouts            show standard channel layouts
-sample_fmts        show available audio sample formats
-colors             show available color names
-sources device     list sources of the input device
-sinks device       list sinks of the output device
-hwaccels           show available HW acceleration methods

Global options (affect whole program instead of just one file:
-loglevel loglevel  set logging level
-v loglevel         set logging level
-report             generate a report
-max_alloc bytes    set maximum size of a single allocated block
-y                  overwrite output files
-n                  never overwrite output files
-ignore_unknown     Ignore unknown stream types
-stats              print progress report during encoding
-max_error_rate ratio of errors (0.0: no errors, 1.0: 100% error  maximum error rate
-bits_per_raw_sample number  set the number of bits per raw sample
-vol volume         change audio volume (256=normal)

Per-file main options:
-f fmt              force format
-c codec            codec name
-codec codec        codec name
-pre preset         preset name
-map_metadata outfile[,metadata]:infile[,metadata]  set metadata information of outfile from infile
-t duration         record or transcode "duration" seconds of audio/video
-to time_stop       record or transcode stop time
-fs limit_size      set the limit file size in bytes
-ss time_off        set the start time offset
-sseof time_off     set the start time offset relative to EOF
-seek_timestamp     enable/disable seeking by timestamp with -ss
-timestamp time     set the recording timestamp ('now' to set the current time)
-metadata string=string  add metadata
-program title=string:st=number...  add program with specified streams
-target type        specify target file type ("vcd", "svcd", "dvd", "dv" or "dv50" with optional prefixes "pal-", "ntsc-" or "film-")
-apad               audio pad
-frames number      set the number of frames to output
-filter filter_graph  set stream filtergraph
-filter_script filename  read stream filtergraph description from a file
-reinit_filter      reinit filtergraph on input parameter changes
-discard            discard
-disposition        disposition

Video options:
-vframes number     set the number of video frames to output
-r rate             set frame rate (Hz value, fraction or abbreviation)
-s size             set frame size (WxH or abbreviation)
-aspect aspect      set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
-bits_per_raw_sample number  set the number of bits per raw sample
-vn                 disable video
-vcodec codec       force video codec ('copy' to copy stream)
-timecode hh:mm:ss[:;.]ff  set initial TimeCode value.
-pass n             select the pass number (1 to 3)
-vf filter_graph    set video filters
-ab bitrate         audio bitrate (please use -b:a)
-b bitrate          video bitrate (please use -b:v)
-dn                 disable data

Audio options:
-aframes number     set the number of audio frames to output
-aq quality         set audio quality (codec-specific)
-ar rate            set audio sampling rate (in Hz)
-ac channels        set number of audio channels
-an                 disable audio
-acodec codec       force audio codec ('copy' to copy stream)
-vol volume         change audio volume (256=normal)
-af filter_graph    set audio filters

Subtitle options:
-s size             set frame size (WxH or abbreviation)
-sn                 disable subtitle
-scodec codec       force subtitle codec ('copy' to copy stream)
-stag fourcc/tag    force subtitle tag/fourcc
-fix_sub_duration   fix subtitles duration
-canvas_size size   set canvas size (WxH or abbreviation)
-spre preset        set the subtitle options to the indicated preset

 

 

まとめ

タイムラプス動画を作成しました。

MJPG-streamerでモニタリング中でも静止画を撮影してタイムラプスを作成できるんですね。

赤ちゃんのタイムラプス動画を作ることで、どんくらい動いているか分かりますね。

成長動画を作る場合は、1日に1回撮影くらいでいいかもしれませんね。

その場合は、顔認識を追加してカメラに顔を向けている時に撮影するなど、工夫が必要かもしれませんね。

 

では!(^^)/

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です