A Python 2.7/3.x module for Amcrest Cameras using the SDK HTTP API.

Overview

Python Amcrest

https://travis-ci.org/tchellomello/python-amcrest.svg?branch=master

A Python 2.7/3.x module for Amcrest Cameras using the SDK HTTP API. Amcrest and Dahua devices share similar firmwares. Dahua Cameras and NVRs also work with this module.

Documentation: http://python-amcrest.readthedocs.io/

Installation

PyPI

/etc/profile.d/amcrest-cli-autocomplete.sh ">
$ pip install amcrest --upgrade
$ eval "$(register-python-argcomplete amcrest-cli)"

# To enable amcrest-cli autocomplete in the system:
$ echo 'eval "$(register-python-argcomplete amcrest-cli)"' >  /etc/profile.d/amcrest-cli-autocomplete.sh

RPM

$ git clone [email protected]:tchellomello/python-amcrest.git
$ ./autogen.sh
$ make rpm
$ dnf/yum install amcrest-cli-NVR.rpm pythonX-amcrest-NVR.rpm

Usage

#Capture audio camera.audio_stream_capture(httptype="singlepart", channel=1, path_file="/home/user/Desktop/audio.aac") CTRL-C to stop the continuous audio flow or use a timer #Move camera down camera.ptz_control_command(action="start", code="Down", arg1=0, arg2=0, arg3=0) #Record realtime stream into a file camera.realtime_stream(path_file="/home/user/Desktop/myvideo") CTRL-C to stop the continuous video flow or use a timer ">
from amcrest import AmcrestCamera
camera = AmcrestCamera('192.168.0.1', 80, 'admin', 'password').camera

#Check software information
camera.software_information
'version=2.420.AC00.15.R\r\nBuildDate=2016-09-08'

#Capture snapshot
camera.snapshot(0, "/home/user/Desktop/snapshot00.jpeg")
<requests.packages.urllib3.response.HTTPResponse object at 0x7f84945083c8>

#Capture audio
camera.audio_stream_capture(httptype="singlepart", channel=1, path_file="/home/user/Desktop/audio.aac")
CTRL-C to stop the continuous audio flow or use a timer

#Move camera down
camera.ptz_control_command(action="start", code="Down", arg1=0, arg2=0, arg3=0)

#Record realtime stream into a file
camera.realtime_stream(path_file="/home/user/Desktop/myvideo")
CTRL-C to stop the continuous video flow or use a timer

Command Line

$ man amcrest-cli
or
$ amcrest-cli --help

# Saving credentials to file.
$ vim ~/.config/amcrest.conf
[patio]
hostname: 192.168.0.20
username: admin
password: 123456
port: 80

[living_room]
hostname: 192.168.0.21
username: admin
password: secret
port: 80

$ amcrest-cli --camera living_room --version-http-api
version=1.40

Text User Interface (TUI)

Configure amcrest.conf and trigger amcrest-tui, make sure the user triggering amcrest-tui have access to framebuffer device or use sudo.

NOTE: Execute it from console logins, like /dev/ttyX (Non X Window). Pseudo-terminals like xterm, ssh, screen and others WONT WORK.

$ vim ~/.config/amcrest.conf
[patio]
hostname: 192.168.0.20
username: admin
password: 123456
port: 80

[living_room]
hostname: 192.168.0.21
username: admin
password: secret
port: 80

$ amcrest-tui

Supportability Matrix

Cameras

Model Tested Status Results/Issues
IPM-721 Yes working  
IPM-HX1 Yes working  
IP2M-841 Yes working  
IP2M-842 Yes working  
IP3M-941 Yes working  
IP3M-943 Yes working  
IP3M-956 Yes working  
IP3M-956E Yes working  
IP3M-HX2 Yes working  
IP4M-1026B Yes working  
IP4M-1051B Yes working  
IP5M-1176EB Yes working  
IP8M-2496EB Yes working  
IP8M-T2499EW-28M Yes working  

Network Video Recorders (NVR)

Model Tested Status Results/Issues
XVR DAHUA 5104S Yes working  

If you have different model, feel fee to contribute and report your results.

Help

If you need any help, please join our community on the Gitter channels available at Gitter.

Comments
  • AttributeError: module 'socket' has no attribute 'TCP_KEEPIDLE'

    AttributeError: module 'socket' has no attribute 'TCP_KEEPIDLE'

    Getting the following error when I try to run anything, even just getting cam info:

    AttributeError: module 'socket' has no attribute 'TCP_KEEPIDLE'

    Process finished with exit code 1

    opened by rtmg2007 20
  • Introducing Unittest

    Introducing Unittest

    Integration with:

    • Tox

    • Coveralls

    • Unittest framework

    • Fixed some lint issues

    • Removed fbida from requirements since it is not a Python package. This should be only present on a SPEC file

    invalid 
    opened by tchellomello 16
  • snapshot() does not work for some cameras (HASS: Amcrest Cameras fail after update to 0.59.1)

    snapshot() does not work for some cameras (HASS: Amcrest Cameras fail after update to 0.59.1)

    **Description of problem:**After upgrade to 0.59.1 only 1 of my Amcrest cameras output appear in both of my Amcrest cameras displays on the Dashboard.

    I upgrade my RPI 3 running on Raspbian Stretch Desktop to HA version 0.60.0. Still having the same trouble with my Amcrest Cameras. I did see this in the Info tab if this helps!! Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))': /cgi-bin/snapshot.cgi?channel=0 2:18 PM components/camera/amcrest.py (WARNING)

    bug help wanted priority 
    opened by akerrichardt 10
  • sending audio

    sending audio

    first off, this is awesome i figured i was going to end up having to put something like this together myself but it's already done!

    on that note; has anyone had any luck sending audio to their cam? this utility appears to send the audio to the cam just fine. However it always sounds completely messed up when outputting through the cam. Initially i started with just curl with the same result until i stumbled upon this.

    im assuming it's some type of audio encoding issue. I have tried sending in most of the available formats with different bit/sample rates with no luck.

    so far this encoding gets me the closest but it's still FAR from correct

    G.711A as content type ffmpeg -i 4353.mp3 -c:a pcm_alaw -ac 1 -ar 16000 -sample_fmt s16 output.wav

    help wanted question 
    opened by josh-- 10
  • Add event_stream() which returns stream of camera events

    Add event_stream() which returns stream of camera events

    Description

    Currently, determining if motion or other events occur requires polling via event_channels_happened() (or one of the other methods or properties that use it.) This can delay response to events, and can even miss events if the event duration is shorter than the polling period.

    This adds a new method -- event_stream() -- which will return events as they happen. It is a generator and will normally not return, so it should be run in a separate thread or process.

    Example use case

    import re
    import threading
    from amcrest import AmcrestCamera, CommError
    
    _START_STOP = re.compile(r"Code=([^;]+);action=(Start|Stop)", flags=re.S)
    
    def process_events(camera, event_codes):
        event_codes = ",".join(event_codes)
        while True:
            try:
                for event_info in camera.event_stream(event_codes, retries=5):
                    print("Event info:", repr(event_info))
                    for code, action in _START_STOP.findall(event_info):
                        print("--->", code, action)
            except CommError as error:
                print("Error while processing events:", repr(error))
    
    camera = AmcrestCamera(...).camera
    thread = threading.Thread(target=process_events, args=(camera, ["VideoMotion"]))
    thread.daemon = True
    thread.start()
    

    To do

    • [x] Make sure comm errors and camera power cycling are handled properly.
    • [x] Get feedback from other users with other camera models and/or firmware versions.
    • [x] Prototype usage in Home Assistant's amcrest integration.
    • [x] Determine if anything can be done about urllib3 warning:
    WARNING:urllib3.connectionpool:Failed to parse headers (url=http://<host>:<port>/cgi-bin/eventManager.cgi?action=attach&codes=%5BVideoMotion%5D): [StartBoundaryNotFoundDefect(), MultipartInvariantViolationDefect()], unparsed data: ''
    Traceback (most recent call last):
      File "/home/phil/test/amcrest/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 441, in _make_request
        assert_header_parsing(httplib_response.msg)
      File "/home/phil/test/amcrest/venv/lib/python3.7/site-packages/urllib3/util/response.py", line 71, in assert_header_parsing
        raise HeaderParsingError(defects=defects, unparsed_data=unparsed_data)
    urllib3.exceptions.HeaderParsingError: [StartBoundaryNotFoundDefect(), MultipartInvariantViolationDefect()], unparsed data: ''
    

    Thanks to @NickWaterton and his groundwork in PR #140!

    opened by pnbruckner 9
  • Add functionality to use additional channels

    Add functionality to use additional channels

    Add additional methods to be able to use specified channels, allowing this to be used with NVRs. Currently, the @property's read from channel 0, change these methods to call into the new functions for the channel it used previously, so these should not change behavior.

    Behavioral changes include:

    • Event.event_channels_happened returns a list of channels where the event has occurred, rather than the request output. This simplifies the is_x_detected methods.
    • The Event.is_x_detected methods return a list of channels where the event occurred, rathen than a bool of if the event occurred or not.
    • Video.video_in_option returns a list of all option configurations for all channels, rather than just returning the first option. All of the properties (eg video_enabled and day_night_color) are unchanged, as they check channel 0.
    • Some of the System properties (eg serial_number) are split with pretty() to give just the value rather than the full message payload.
    opened by flacjacket 6
  • FIX for #75 and #149

    FIX for #75 and #149

    Potential fix for #75 and #149

    I do not have access to the hardware at this time (due to corona virus). So I cannot test this.

    The user using play_wav will now need to know the encoding type for their .wav file.

    The most critical part of this PR is the new optional argument 'encoding'

    It is up to someone who has access to a camera to test out all the encodings and report back to this community their results.

    According to a comment in the same file:

    Supported audio encode type according with documentation: PCM ADPCM G.711A G.711.Mu G.726 G.729 MPEG2 AMR AAC

    G.711A will be the default one as that was the encoding type that was hard coded before.

    Take care, and happy testing!

    opened by AlexKohanim 6
  • Reuse command sessions

    Reuse command sessions

    In command method, don't create a new requests session every time. Rather, keep a session for each value of retries. Also don't try to update default values for retries and timeout for each command because that wasn't thread safe.

    opened by pnbruckner 6
  • Release 1.8.0?

    Release 1.8.0?

    It'd be great to have a new release now that some of the NVR channel related functionality is in. Looking at the commits since 1.7.2, I think this is the set of key changes:

    Breaking changes

    • Python 2 no longer supported, Python 3.6 is the minimum required version
    • PTZ commands (e.g. zoom_in, move_left) require boolean value for start parameter, rather than "start" and "stop" strings for the action parameter

    New features

    • Smart motion detection events added as is_human_detected and is_vehicle_detected properties.
    • Added methods to query separate channels to allow using multiple cameras on NVRs.
    • Type annotations added to allow for type checking.

    I'd say no particular rush to get this out, just curious what sort of things would be needed to get to the point of cutting a release. A couple of the current open PRs seem like they are also close to ready if they can get some minor fixes from authors, so those would probably be good to get in if possible.

    opened by flacjacket 5
  • **Feature Request** Smart Motion Detection

    **Feature Request** Smart Motion Detection

    I recently upgraded a camera to the IPC-HDW5442TM-AS. This has some new events and I was hoping to use the Smart Motion Detection (can detect humans and vehicles). Playing with the API calls I found that the getConfig name is SmartMotionDetect

    The following command:

    http://<IP>/cgi-bin/configManager.cgi?action=getConfig&name=SmartMotionDetect
    

    Returns:

    table.SmartMotionDetect[0].Enable=true
    table.SmartMotionDetect[0].ObjectTypes.Human=true
    table.SmartMotionDetect[0].ObjectTypes.Vehicle=false
    table.SmartMotionDetect[0].Sensitivity=Middle
    

    This shows that the smart detection is enabled, and for humans but not vehicles - and the sensitivity is set to medium (3 options, low/medium/high).

    Using this I expect that the event subscription would be either SmartMotionDetect or SmartVideoMotion based on what is already in this repo. I am a novice, and wasn't sure how to test this. I tried, but was unsuccessful getting a correct response with the existing motion detection (I'm sure I'm doing something wrong though).

    When I tried to test some of the commands in the interpreter I expect I am doing it wrong. I am getting generator objects so I expect it is doing something but I am sending the wrong commands or not telling it how to output... as said earlier, I'm a novice 😄 .

    >>> import amcrest
    >>> camera = amcrest.AmcrestCamera('<IP>',80,'<admin>','<password>').camera
    >>> camera.software_information
    ('version=2.800.0000000.20.R', 'build:2020-09-03\r\n')
    >>> camera.event_stream('All')
    <generator object Event.event_stream at 0x7f307bfe0900>
    >>> camera.event_stream('VideoMotion')
    <generator object Event.event_stream at 0x7f307bfe0820>
    >>> camera.event_actions('All')
    <generator object Event.event_actions at 0x7f307bfe0900>
    

    If this is as simple as adding a new event name, fantastic - but if it needs testing, I would be happy to do it, but might need a little hand holding to start. If I could be told how to test the VideoMotion and get a response correctly, I'm sure I could work from there to get the SmartVideoMotion or SmartMotionDetect whatever they call it figured out.

    Thanks! DeadEnd

    opened by DeadEnded 5
  • Older Firmware Cameras Not Supportive of URL that is Encoded (EventManager)

    Older Firmware Cameras Not Supportive of URL that is Encoded (EventManager)

    On a Dahua 2015 firmware, no upgrade possible (last firmware)

    This URL gets a bad response (400) http://192.168.1.41:80/cgi-bin/eventManager.cgi?action=attach&codes=%5BAll%5D

    This URL works http://192.168.1.41:80/cgi-bin/eventManager.cgi?action=attach&codes=[All]

    A quick fix was detailed here (with Python Amcrest 1.7.0) in HA Reported bug https://github.com/home-assistant/core/issues/33875#issuecomment-615548520

    Which seems to be working for me.

    wontfix 
    opened by jamesarbrown 5
  • Events to trigger HTTP Post to external URL

    Events to trigger HTTP Post to external URL

    I'm still reading through some of the documentation with this camera and the API. I'm hoping that I can set a configuration option that will cause the camera to automatically HTTP Post event data to a server I have running in the cloud. I'm hoping to do this without having to have a listener running actively monitoring those events myself.

    .e.g something like setConfig onMotionDetected POST to https://someexternalsite.com

    I'm not seeing anything like this, but the documentation regarding setting config options is a little tough to grok.

    Regards

    opened by trankin 0
  • Support enabling and disabling the mechanical chime

    Support enabling and disabling the mechanical chime

    One of the biggest reasons I'm dropping my Nest doorbell is because of the lack of API control. The Amcrest AD110 has the required control for me to bring things local, but it's missing one thing: the ability to silence the indoor chime. I have young children and a baby on the way, and we want that doorbell chime SILENCED during nap time.

    It seems it is possible to disable (and likely reenable) the chime using the web api.

    http://<ip address of ad110>/cgi-bin/configManager.cgi?action=setConfig&ExternalDoorBell.Enable=false
    http://<ip address of ad110>/cgi-bin/configManager.cgi?action=setConfig&ExternalDoorBell.Enable=true
    

    You can get the current status like so:

    http://<ip address of ad110>/cgi-bin/configManager.cgi?action=getConfig&name=ExternalDoorBell
    

    Is this something the module can support so it may trickle up the stream and eventually into Home Assistant?

    opened by mririgoyen 1
  • event_actions() causes AttributeError

    event_actions() causes AttributeError

    I am connecting to an Amcrest AD410 Doorbell and utilizing the event_actions() method and after about 100s, I receive the Error: AttributeError: 'NoneType' object has no attribute 'read'.

    More specifically, I am calling: camera.event_actions("All", retries=5, timeout_cmd=(10.00, 3600)) and with DEBUG log level, I get the follow:

    2022-04-30 00:42:29,922 - urllib3.connectionpool [DEBUG] Starting new HTTP connection (1): 192.168.10.20:80
    2022-04-30 00:42:29,970 - urllib3.connectionpool [DEBUG] http://192.168.10.20:80 "GET /cgi-bin/eventManager.cgi?action=attach&codes=%5BAll%5D HTTP/1.1" 401 0
    2022-04-30 00:44:12,784 - urllib3.connectionpool [DEBUG] http://192.168.10.20:80 "GET /cgi-bin/eventManager.cgi?action=attach&codes=%5BAll%5D HTTP/1.1" 200 None
    2022-04-30 00:44:12,785 - amcrest.http [DEBUG] <Doorbell-D66A> Query 9 worked. Exit code: <200>
    2022-04-30 00:44:12,786 - amcrest.event [DEBUG] <Doorbell-D66A> event info: '\r\nCode=VideoMotionInfo;action=State;index=0'
      File "/usr/local/lib/python3.10/site-packages/amcrest/event.py", line 361, in event_actions
        for event_info in self.event_stream(
      File "/usr/local/lib/python3.10/site-packages/amcrest/event.py", line 312, in event_stream
        for line in _event_lines(ret.iter_content(decode_unicode=True)):
      File "/usr/local/lib/python3.10/site-packages/amcrest/event.py", line 39, in _event_lines
        for char in ret:
      File "/usr/local/lib/python3.10/site-packages/requests/utils.py", line 549, in stream_decode_response_unicode
        for chunk in iterator:
      File "/usr/local/lib/python3.10/site-packages/requests/models.py", line 760, in generate
        for chunk in self.raw.stream(chunk_size, decode_content=True):
      File "/usr/local/lib/python3.10/site-packages/urllib3/response.py", line 575, in stream
        for line in self.read_chunked(amt, decode_content=decode_content):
      File "/usr/local/lib/python3.10/site-packages/urllib3/response.py", line 770, in read_chunked
        chunk = self._handle_chunk(amt)
      File "/usr/local/lib/python3.10/site-packages/urllib3/response.py", line 714, in _handle_chunk
        value = self._fp._safe_read(amt)
      File "/usr/local/lib/python3.10/http/client.py", line 630, in _safe_read
        data = self.fp.read(amt)
    AttributeError: 'NoneType' object has no attribute 'read'
    

    What's also odd that I first get a 401 response before eventually getting a 200. Not sure if this is affecting things tho as I see the same behavior with other calls (ie camera.storage_all) and they return fine.

    Environment:

    • OS: Debian GNU/Linux 10 (buster)
    • Python: 3.10
      • amcrest: 1.9.7
    opened by maxirus 0
  • Add github action to automate creating PyPI packages

    Add github action to automate creating PyPI packages

    Add a new github action that will build and upload PyPI packages when new releases are cut. This works by running but not uploading the package when a tag is created, then, when the Github release is published, the build is run and the package is pushed to PyPI.

    To work, this will also require a PyPI API token. I am able to generate a token, but do not have access to add it as a Github Actions secret to this repo.

    opened by flacjacket 0
Releases(1.9.7)
Owner
Marcelo Moreira de Mello
Marcelo Moreira de Mello
An Python SDK for QQ based on mirai-api-http v2.

Argon 一个基于 graia-broadcast 和 mirai-api-http v2 的 Python SDK。 本项目适用于 mirai-api-http 2.0 以上版本。 目前仍处于开发阶段,内部接口可能会有较大的变化。 The Stasis / 停滞 为维持 GraiaProject

BlueGlassBlock 1 Oct 29, 2021
An elegant mirai-api-http v2 Python SDK.

Ariadne 一个适用于 mirai-api-http v2 的 Python SDK。 本项目适用于 mirai-api-http 2.0 以上版本。 目前仍处于开发阶段,内部接口可能会有较大的变化。 安装 poetry add graia-ariadne 或 pip install graia

Graia Project 259 Jan 2, 2023
Graviti-python-sdk - Graviti Data Platform Python SDK

Graviti Python SDK Graviti Python SDK is a python library to access Graviti Data

Graviti 13 Dec 15, 2022
Python module and command line script client for http://urbandictionary.com

py-urbandict py-urbandict is a client for urbandictionary.com. Project page on github: https://github.com/novel/py-urbandict PyPI: https://pypi.org/pr

Roman Bogorodskiy 32 Oct 1, 2022
Python SDK for Facebook's Graph API

Facebook Python SDK This client library is designed to support the Facebook Graph API and the official Facebook JavaScript SDK, which is the canonical

Mobolic 2.7k Jan 7, 2023
The Official Dropbox API V2 SDK for Python

The offical Dropbox SDK for Python. Documentation can be found on Read The Docs. Installation Create an app via the Developer Console. Install via pip

Dropbox 828 Jan 5, 2023
Unofficial Medium Python Flask API and SDK

PyMedium - Unofficial Medium API PyMedium is an unofficial Medium API written in python flask. It provides developers to access to user, post list and

Engine Bai 157 Nov 11, 2022
Python SDK for the Buycoins API.

This library provides easy access to the Buycoins API using the Python programming language. It provides all the feature of the API so that you don't need to interact with the API directly. This library can be used with Python 3.6+

Musa Rasheed 48 May 4, 2022
qualysclient - a python SDK for interacting with the Qualys API

qualysclient - a python SDK for interacting with the Qualys API

null 5 Oct 28, 2022
Python SDK for accessing the Hanko Authentication API

Hanko Authentication SDK for Python This package is maintained by Hanko. Contents Introduction Documentation Installation Usage Prerequisites Create a

Hanko.io 3 Mar 8, 2022
Balsam Python client API & SDK

balsam No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) This Python package is automatically

Darren Govoni 1 Oct 22, 2021
Python SDK for interacting with the Frame.io API.

python-frameio-client Frame.io Frame.io is a cloud-based collaboration hub that allows video professionals to share files, comment on clips real-time,

Frame.io 37 Dec 21, 2022
DongTai API SDK For Python

DongTai-SDK-Python Quick start You need a config file config.json { "DongTai":{ "token":"your token", "url":"http://127.0.0.1:90"

huoxian 50 Nov 24, 2022
Python 3 SDK/Wrapper for Huobi Crypto Exchange Api

This packages intents to be an idiomatic PythonApi wrapper for https://www.huobi.com/ Huobi Api Doc: https://huobiapi.github.io/docs Showcase TODO Con

null 3 Jul 28, 2022
The official Magenta Voice Skill SDK used to develop skills for the Magenta Voice Assistant using Voice Platform!

Magenta Voice Skill SDK Development • Support • Contribute • Contributors • Licensing Magenta Voice Skill SDK for Python is a package that assists in

Telekom Open Source Software 18 Nov 19, 2022
A wrapper for aqquiring Choice Coin directly through a Python Terminal. Leverages the TinyMan Python-SDK.

CHOICE_TinyMan_Wrapper A wrapper that allows users to acquire Choice Coin directly through their Terminal using ALGO and various Algorand Standard Ass

Choice Coin 16 Sep 24, 2022
HTTP Calls to Amazon Web Services Rest API for IoT Core Shadow Actions 💻🌐💡

aws-iot-shadow-rest-api HTTP Calls to Amazon Web Services Rest API for IoT Core Shadow Actions ?? ?? ?? This simple script implements the following aw

AIIIXIII 3 Jun 6, 2022
AWS SDK for Python

Boto3 - The AWS SDK for Python Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python, which allows Python developers to wri

the boto project 7.8k Jan 8, 2023