Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions motioneye-audio.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ operation=$1
motion_thread_id=$2
file_path=$3
camera_name=$4
audio_codec="acc"
audio_file_extention="acc"

camera_id="$(python -c 'import motioneye.motionctl; print motioneye.motionctl.motion_camera_id_to_camera_id('${motion_thread_id}')')"
motion_config_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
Expand All @@ -19,25 +21,28 @@ case ${operation} in
credentials="$(grep netcam_userpass ${motion_camera_conf} | sed -e 's/netcam_userpass.//')"
stream="$(grep ${netcam} ${motion_camera_conf} | sed -e "s/${netcam}.//")"
full_stream="$(echo ${stream} | sed -e "s/\/\//\/\/${credentials}@/")"
ffmpeg -y -i "${full_stream}" -c:a aac ${file_path}.aac 2>&1 1>/dev/null &
ffmpeg -y -i "${full_stream}" -c:a ${audio_codec} ${file_path}.${audio_file_extention} 2>&1 1>/dev/null &
ffmpeg_pid=$!
echo ${ffmpeg_pid} > /tmp/motion-audio-ffmpeg-camera-${camera_id}
# echo ${ffmpeg_pid} > /tmp/motion-audio-ffmpeg-camera-${camera_name}
;;

stop)
# get motion_gap from camera config
motion_gap="$(grep event_gap ${motion_camera_conf} | sed -e 's/event_gap.//')"
# Kill the ffmpeg audio recording for the clip
kill $(cat /tmp/motion-audio-ffmpeg-camera-${camera_id})
rm -rf $(cat /tmp/motion-audio-ffmpeg-camera-${camera_id})
# kill $(cat /tmp/motion-audio-ffmpeg-camera-${camera_name})
# rm -rf $(cat /tmp/motion-audio-ffmpeg-camera-${camera_name})

# cut the last part of audio file with motion_gap time to avoid sound without image when motion event is over
ffmpeg -i ${file_path}.${audio_file_extention} -t $(( $(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 ${file_path}.${audio_file_extention} | cut -d\. -f1) - ${motion_gap} )) -acodec copy -vcodec copy ${file_path}-audiotemp.${audio_file_extention}
mv -f ${file_path}-audiotemp.${audio_file_extention} ${file_path}.${audio_file_extention};
# Merge the video and audio to a single file, and replace the original video file
ffmpeg -y -i ${file_path} -i ${file_path}.aac -c:v copy -c:a copy ${file_path}.temp.${extension};
ffmpeg -y -i ${file_path} -i ${file_path}.${audio_file_extention} -c:v copy -c:a copy ${file_path}.temp.${extension};
mv -f ${file_path}.temp.${extension} ${file_path};

# Remove audio file after merging
rm -f ${file_path}.aac;
rm -f ${file_path}.${audio_file_extention};
;;

*)
Expand Down