| 1 | import asyncio |
| 2 | import logging |
| 3 | |
| 4 | import click |
| 5 | |
| 6 | from haffmpeg.sensor import SensorMotion |
| 7 | |
| 8 | logging.basicConfig(level=logging.DEBUG) |
| 9 | |
| 10 | |
| 11 | @click.command() |
| 12 | @click.option("--ffmpeg", "-f", default="ffmpeg", help="FFmpeg binary") |
| 13 | @click.option("--source", "-s", help="Input file for ffmpeg") |
| 14 | @click.option( |
| 15 | "--reset", |
| 16 | "-r", |
| 17 | default=60, |
| 18 | type=int, |
| 19 | help="Time duration to need no motion before reset state", |
| 20 | ) |
| 21 | @click.option( |
| 22 | "--repeat-time", |
| 23 | "-rt", |
| 24 | default=0, |
| 25 | type=int, |
| 26 | help="Need repeat motion in this time period for trigger state", |
| 27 | ) |
| 28 | @click.option( |
| 29 | "--repeat", |
| 30 | "-rc", |
| 31 | default=0, |
| 32 | type=int, |
| 33 | help="Need repeat motion to trigger state in repeat-time", |
| 34 | ) |
| 35 | @click.option( |
| 36 | "--changes", |
| 37 | "-c", |
| 38 | default=10, |
| 39 | type=float, |
| 40 | help="Scene change settings or percent of image they need change", |
| 41 | ) |
| 42 | @click.option("--extra", "-e", help="Extra ffmpeg command line arguments") |
| 43 | def cli(ffmpeg, source, reset, repeat_time, repeat, changes, extra): |
| 44 | """FFMPEG noise detection.""" |
| 45 | |
| 46 | def callback(state): |
| 47 | print("Motion detection is: %s" % str(state)) |
| 48 | |
| 49 | async def run(): |
| 50 | |
| 51 | sensor = SensorMotion(ffmpeg_bin=ffmpeg, callback=callback) |
| 52 | sensor.set_options( |
| 53 | time_reset=reset, changes=changes, repeat=repeat, time_repeat=repeat_time |
| 54 | ) |
| 55 | await sensor.open_sensor(input_source=source, extra_cmd=extra) |
| 56 | try: |
| 57 | while True: |
| 58 | await asyncio.sleep(0.1) |
| 59 | finally: |
| 60 | await sensor.close() |
| 61 | |
| 62 | asyncio.run(run()) |
| 63 | |
| 64 | |
| 65 | if __name__ == "__main__": |
| 66 | cli() |