テープアクション?

テープアクション?

騒音や動きが検出されたら、音とともに動画の録画を開始するデーモンを設定したいと思います。これを個別に実行できるツールがありますが、同時に実行できますか?モーションが検出されたら、スクリプトを実行するようにモーションを設定できますか? SOXでも同じことができますか?

ベストアンサー1

これは非常に古い質問であることを知っていますが、検索中にこの質問を誤って見つけた人に役立ちます(例:数年前)...オーディオを録音してストリーミングするにはRidgyで始めます。 Motionで上記の回答を見つけ、少なくとも私には成功したソリューションの開発を始めることができました。私のユースケースは、巣箱から音を録音することです。 Pi 3B+ランニングモーションに接続されているパン/チルトマウントにMS LifeCam Webカメラを取り付けました。解決策は、実際にビデオの録画を開始するためにモーションがトリガーされたときにスクリプトを実行することですが、Webカメラからオーディオをストリーミングする継続的に実行されるスクリプトもあります。次にVLCを使用してネットワークビデオストリームを表示し、オーディオストリームを再生します。詳細は次のとおりです。

オーディオファイルとビデオファイルを処理するために3つのディレクトリが設定されています /home/pi/motion/videos/home/pi/motion/videosB/home/pi/videos/pre-mergevideos

motion.conf.mkv 動画を保存するように設定/home/pi/motion/videos

ムービーが起動すると、MotionはRecord_s.shというスクリプトを実行します。このスクリプトは、.mkvビデオと同じ名前で60秒のオーディオファイルを録音し、/home/pi/motion/videos

ムービーが閉じると、Motionは.mkvと.mp3ファイルをマージし、マージされたファイルを/home/pi/motion/videos.mkv/home/pi/motion/pre-mergevideosと.mp3ソースファイルを削除し、ファイルをマージするmerge_sv.shというスクリプトを実行します。権限は777に変更され、所有者はルートからモーションに変更され、最後にマージされたファイルは次に移動されました。/home/pi/motion/videosB

crontabは2分ごとにrsyncを実行し、「videosB」のコンテンツをNAS共有(/home/pi/motion/media)に移動します。

後者は、何らかの理由でNAS共有が利用できなくなった場合に「motion」がクラッシュするのを防ぐためです。 「motion」はそのファイルをローカルディレクトリに書き込み、crontabはNAS共有への出力を担当します。

#!/bin/bash
# record_s.sh
# 15/4/18: Created
# 25/11/20: libav-tools no longer available in 'Buster', 'avconv' replaced by 'ffmpeg'
filename="$1"
echo "$1" &> /home/pi/motion/filename.txt  # filename.txt will contain /home/pi/motion/videos/xxxxxxxxxxxxxx.mkv
#remove ".mkv" file extension - "${filename%.*}  (parameter expansion)
#Use of 'sudo' is only possible if user motion is in the sudoers file and in the sudo group
sudo ffmpeg -y -f alsa -ac 1 -i default -acodec mp3 -b:a 64k -t 60 "${filename%.*}".mp3 &> /home/pi/motion/record_s.txt
#!/bin/bash
# merge_sv.sh
# 19/04/18: Created

# Call arguments: %f %Y%H%d%T where %f is filename with full path (eg) /home/pi/motion/videos/9403-20180511053500.mkv
# Allow record_s.sh to finish writing the .mp3 sound file (which will have the same name & path as above except .mp3)

sleep 10
filename="$1"
# The 'if' statement below will reject timelapse files '*.avi'
if [[ ${filename##*\.} != avi ]] # See answer 5 in https://stackoverflow.com/questions/407184/how-to-check-the-extension-of-a-filename-in-a-bash-script
then
    #Change output file address from /home/pi/motion/videos to /home/pi/motion/pre-mergevideos (/home/pi/motion/pre-mergevideos is a directory for temporarily holding the unmerged files)
    tempfolder="pre-mergevideos"
    outputfolder="videosB" # This is the folder containing the merged mkv & mp3 video and the jpg files from 'motion'. This folder is rsync'd to network share 'media'.
    # Replaces 'videos' in $filename with the text in $tempfolder (ie) 'pre-mergevideos' so $output becomes (eg) /home/pi/motion/pre-mergevideos/9403-20180511053500.mkv, whilst
    # $filename remains (eg) /home/pi/motion/videos/9403-20180511053500.mkv
    temp_locn="${filename/videos/$tempfolder}"
    final_locn="${filename/videos/$outputfolder}"
    #
    #
    # Merge video and audio into one file. Note the mp3 file has to be converted to aac for an mkv container.  Error and stdout messages are redirected to merge_sv.txt.  For
    # 'sudo' to work user 'motion' has to be added to the sudoers file and to the sudo group.
    # The expression "${filename%.*} removes ".mkv" file extension (parameter expansion)
    # -itsoffset option offsets the timestamps of all streams by (in this case) 0.8 seconds in the input file following the option (without it the audio leads the video by about 0.8s).
    sudo ffmpeg -y -i $filename -itsoffset 0.80 -i "${filename%.*}".mp3 -c:v copy -c:a aac $temp_locn &> /home/pi/motion/merge_sv.txt
    #Delete the source files
    sudo rm $filename
    sudo rm "${filename%.*}".mp3
    # change the file permissions on the merged file (otherwise it's root 644 rw.r..r..)
    sudo chmod 777 $temp_locn
    # change the owner to 'motion'
    sudo chown motion:motion $temp_locn
    #move the recently merged file into the 'videosB' folder. Remember both $temp_locn and $final_locn contain path and filename
    sudo mv $temp_locn $final_locn 
fi

tldr バージョン:

record_s.shMotionがビデオを録画すると、Webカメラのオーディオも録音されます。ビデオ録画が完了したら、merge_sv.shオーディオとビデオファイルを一緒にマージしてソースファイルを削除します。スクリプトは、オーディオにオフセットを適用して(試行錯誤によって決定されます)、ファイルを一緒に同期したままにします。テザリングが利用できない場合、動きの衝突を防ぐためのいくつかのディザリングもあります。

オーディオストリーミング:起動時に呼び出される単純なスクリプトを実行して、crontab -eを介してsound_on.shrtpストリームを作成します。

#!/bin/bash
# sound_on.sh
# 13/1/18
# 
# killall -9 avconv
#Pre Buster version:
#avconv -f alsa -ac 1 -re -i default -acodec mp3 -ac 1 -f rtp rtp://234.5.5.5:5004 2> /tmp/mylog.log
# default was hw:1,0

#Buster version onwards:
ffmpeg -f alsa -channels 1 -i hw:1,0 -acodec mp3 -f rtp rtp://234.5.5.5:5004 2> /home/pi/motion/sound_on.log

VLCで次の設定で再生できます。

メディア/オープンネットワークストリーミング:ネットワークURL:http://192.168.1.122:8081(ラズベリーパイがモーションを実行するアドレス)

その他のオプションの表示/追加メディアの同時再生:「追加メディア」rtp://234.5.5.5:5004

次に「再生」をタップ

これはVLCのUbuntuバージョンでのみ機能します。これを試みるたびに、Windows 10のバージョンがクラッシュします。

おすすめ記事