fronzbot / blinkpy

A Python library for the Blink Camera system
MIT License
574 stars 119 forks source link

Unable to retrieve recent video #185

Closed vapuri closed 5 years ago

vapuri commented 5 years ago

@fronzbot thanks for your contribution with this code - I'm trying to save the latest video clip from my blink camera (Blink XT2) in home assistant. I started with the documented example here: https://www.home-assistant.io/components/blink/. In my log I got the following error:

No saved video exist for [My Camera Name]. NoneType: None

In Blink App I can verify that there are 3 videos from today. So, I started to go down and debug using blinkpy 0.14.0 to isolate the issue since upon searching I found there were some recent fixes regarding the end point changes.

To Reproduce Here's my debug code:

from blinkpy import blinkpy

blink = blinkpy.Blink(username='user', password='password')
blink.start()

sync = blink.sync['My Sync Module']
camera = sync.cameras['My Camera']

blink.refresh()
print(camera.attributes) # this successfully prints the attributes of the camera
camera.image_to_file('image.jpg') # successfully saves the camera still image
camera.video_to_file('video.mp4') # don't see any video saved

I get the latest image.jpg but no video.mp4. I also notice that the video attribute in the camera object is None.

If I call blink.download_videos('.', since='6/1/2014'), the video successfully downloads.

Expected behavior I would expect that upon refresh, the camera attribute 'video' points to the latest video clip and the camera.video_to_file('video.mp4') successfully saves it locally. Not sure if that is indeed the expected behavior or if I'm missing something.

Home Assistant version (if applicable): NA although I first tested with 0.93.2 and also with the latest version of blinkpy 0.14.0

blinkpy version (not needed if filling out Home Assistant version): 0.14.0

Happy to share additional information to help debug. Thanks again!

fronzbot commented 5 years ago

This should be fixed in 0.14.0 but you said you tested with that, which is odd (but that doesn't preclude the possibility of another api issue). Did you test 0.14.0 directly in python or in home-assistant?

EDIT- nvm, you obviously tested in python based on the debug code. Give me a bit, I'll give you some more debug code to try

vapuri commented 5 years ago

I tested with 0.14.0.dev2 directly in python. Also, there is something off with the time zones - here's my debug output where I use:

currentTime = time.time()
since = currentTime - 3600  # get the videos from the last one hour
sinceStr = time.strftime('%Y-%m-%d %H:%M %Z', time.localtime(since))
print ("since:"+sinceStr)
blink.download_videos('.', sinceStr, camera=cameraName)

since: 2019-06-03 08:33 Pacific Daylight Time 2019-06-03 09:33:33,533 - INFO - Retrieving videos since 2019-06-03T15:33:00Pacific Standard Time 2019-06-03 09:33:35,696 - INFO - Downloaded video to .\front-door-2019-06-03t15-37-01-00-00.mp4

Notice the debug output where it says retrieving videos since 15:33:00 Pacific Standard Time - that time is in GMT (+7 hours from what it should be 8:33 pacific) - it is pulling the video based on the right time, but the debug message and timestamp on the video are in GMT.

fronzbot commented 5 years ago

EDIT - revised a bit since you commented with more info as I was commenting

The timezone info stuff is ok (or should be) since the TZ info is sent to blink when requesting videos. AS long as they handle that properly, it shouldn't matter.

Now, I want to make sure I'm clear on one thing I'm noticing with your initial script: there's no indication that you triggered your camera between startup and calling blink.refresh(). By default, only videos between two refresh events are grabbed (since they would be videos that were triggered by a motion event).

For debug, you should start blink and prior to calling blink.refresh(), manually trigger your camera. Then, after about 10 seconds, call blink.refresh() and try the camera.video_to_file() method. This should work.

Now, if that doesn't work, I have other things you could try, so please let me know 👍

vapuri commented 5 years ago

Thanks! - I think that's it - "By default, only videos between two refresh events are grabbed (since they would be videos that were triggered by a motion event)".

Here's what I'm trying to accomplish - when motion happens outside my front door, I want to grab the frames from the video, run a person detection algorithm on it and alert me if there is a person in the frame to avoid false positives like squirrels dancing, leaves shaking or light conditions changing. I tried playing with sensitivity but if I make it too low, then it doesn't catch even someone walking up to the front door. I'm using https://github.com/Nekmo/amazon-dash to capture the motion and send an event to HA as it happens based on the MAC address of the blink XT camera, so I already know the motion happened in real time - I just need to grab the latest video. So, perhaps using the download_videos() method is what I should be doing in the first place through a custom python script.

Either way, I'll try what you suggested just to see if it works later tonight and report back.

vapuri commented 5 years ago

I tried what you suggested and the following code works:

from blinkpy import blinkpy

blink = blinkpy.Blink(username='user', password='pass')
blink.start()

sync = blink.sync['My Sync Module']
camera = sync.cameras['My Camera']

blink.refresh()                    # Get new information from server
print(camera.attributes)    # put a break point here and manually triggered the camera
blink.refresh()                    # Get new information from server

camera.video_to_file('video.mp4')

Is my conclusion correct then that for my use case, I should just be using download_videos() method to grab the latest video if I have already detected motion through a custom event? If so, I'll just close out this issue since the way I was using the camera.video_to_file() method isn't the right way. Also, by the same token it would not work if I call blink.save_video service in HA since I'm not using the polling to detect motion.

fronzbot commented 5 years ago

Yeah, I think the download_videos() method is a better option in your case. The video_to_file() was added in as a way to speed up the home-assistant configuration so the video is pre-fetched when motion is detected, making the process of writing to a file quick (rather than needed to wait for an http request).

On a related note: I think I will add in a service to the HA component to allow you to just call the download_videos() method directly from there. Probably a useful thing to include 🤔

vapuri commented 5 years ago

Thanks - yes, good idea to include that functionality of downloading latest videos in a future release.