我想设置一个守护进程,在检测到噪音或运动时开始录制有声视频。有一些工具可以单独执行此操作,但它们可以同时执行吗?我可以设置运动,使其在检测到运动时执行脚本吗?我可以对 SOX 做同样的事情吗?
答案1
我知道这是一个非常古老的问题,但我也许可以帮助那些在搜索中偶然发现这个问题的人(像我,几年前)...当我想要录制和流式传输音频时,我从 Ridgy 那里找到了上述答案来自 Motion,它让我开始开发一个成功的解决方案,至少对我来说是这样。我的用例是录制巢箱中的声音。我在平移/倾斜安装座上安装了一个 MS LifeCam 网络摄像头,连接到 Pi 3B+ 跑步运动。解决方案确实是在触发运动以开始录制电影时运行脚本,但我还有一个连续运行的脚本,它从网络摄像头传输音频。然后,我使用 VLC 查看网络视频流并播放音频流。详情如下:
我设置了三个目录来处理音频和视频文件:
/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 会运行一个名为 merge_sv.sh 的脚本,该脚本会将 .mkv 和 .mp3 文件合并到 中/home/pi/motion/videos
,并将合并的文件保存到/home/pi/motion/pre-mergevideos
.然后删除.mkv和.mp3源文件,合并文件的权限更改为777,所有者从root更改为motion,最后,合并文件移动到/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.sh
当 Motion 录制视频时,录制来自网络摄像头的音频。视频录制完成后,merge_sv.sh
将音频和视频文件合并在一起并删除源文件。该脚本对音频应用偏移量(通过反复试验确定)以使文件同步在一起。如果网络共享不可用,还有一些抖动可以阻止运动崩溃。
音频流:我通过 crontab -e 运行一个在启动时调用的简单脚本sound_on.sh
来生成 rtp 流:
#!/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
然后按“播放”
请注意,这仅(在撰写本文时)适用于 Ubuntu 版本的 VLC - 每次我尝试此操作时,Windows 10 版本都会崩溃。