Skip to content

Instantly share code, notes, and snippets.

@mdetweiler
Last active September 14, 2023 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdetweiler/944911aa39e68e1866de089290fcf0d3 to your computer and use it in GitHub Desktop.
Save mdetweiler/944911aa39e68e1866de089290fcf0d3 to your computer and use it in GitHub Desktop.
ZoneMinder real-time audio alert script
#!/usr/bin/python3
# zmspeak.py by Michael Detweiler
#
# For vehicle direction tracking in ZoneMinder monitor (ZM 1.37 and later):
# Just add this script to your "Recording" option "Event Start Command" in your monitor config.
# Create two "Active" zones with space between them and a post event buffer
# of a few seconds or more as needed.
# Note: You can also use this for one zone audio messages.
#
# To play audio mesages over my Sonos speaker...
# pip install -U soco-cli
# Create custom audio voice messages... https://seomagnifier.com/text-to-speech-converter
# wget https://seomagnifier.com/core/audio/ede3499382b46fe06f901f18362debd4.mp3
# Test with:
# sonos "Family Room" play_file ede3499382b46fe06f901f18362debd4.mp3
# Create audio files for each monitor and direction or what ever you like.
# <Monitor Name>_<Direction>.mp3
# Example: Driveway3_Approaching.mp3
#
# chmod 755 zmspeak.py
# chown www-data zmspeak.py
# chgrp www-data zmspeak.py
# Test with a previously recorded event for that monitor:
# sudo su -s/bin/bash -c"/home/pi/zmspeak.py <eventId> <monitorId>" www-data
from datetime import datetime
import subprocess
import sys
import mysql.connector
sonosexe = "/usr/local/bin/sonos"
player = "Family Room"
audio_files = "/home/pi/audios/"
speak_start = 7 #Hour 7am
speak_stop = 21 #Hour 21pm
def main():
myobj = datetime.now()
ch = myobj.hour
if ch not in range(speak_start, speak_stop):
return
eventId = sys.argv[1]
monitorId = sys.argv[2]
mydb = mysql.connector.connect(
host="localhost",
user="zmuser",
password="zmpass",
database="zm"
)
connection = mydb
with connection.cursor() as cursor:
#Need to do processing to figure out real direction (find first alarmed zone)
query = "SELECT Name FROM Monitors WHERE Id = {0}".format(monitorId)
cursor.execute(query)
monitor_name = cursor.fetchone()[0]
query = "SELECT FrameId FROM Frames WHERE EventId = {0} AND Type = 'Alarm' LIMIT 1".format(eventId)
cursor.execute(query)
frame = cursor.fetchone()[0]
query = "SELECT ZoneId FROM Stats WHERE EventId = {0} AND FrameId = {1} ORDER BY Stats.AlarmPixels DESC LIMIT 1".format(eventId, frame)
cursor.execute(query)
zone = cursor.fetchone()[0]
query = "SELECT Name From Zones WHERE MonitorId = {0} and ID = {1}".format(monitorId, zone)
cursor.execute(query)
direction = cursor.fetchone()[0]
try:
subprocess.call("{0} '{1}' play_file {2}{3}_{4}.mp3".format(sonosexe,player,audio_files,monitor_name,direction), shell=True)
except Exception as e:
print("Error: {0}".format(e))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment