nestsdm

package module
v0.0.0-...-60fdef6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

README

Nest Smart Device Management (and video capture!)

This repository implements a Go client for the Nest Smart Device Management API for Cameras, as well as some tools for capturing video streams using those APIs.

Device Access Setup

  • Create (or choose an existing Cloud project).
  • Enable the Smart Device Management API
  • Create an OAuth Token. Be sure to create a "Desktop App" token type.
  • Go to the Device Access Console. You may need to register as a developer.
  • Create a project. (You'll need the Project ID later to find your Device IDs.) Use the OAuth Client ID you created above.
  • Use Partner Connection Manager to connect devices to the project. Enable the Allow PROJECTNAME to see and display your camera’s livestream permission for the devices you want to interact with.

Use Cases

Common setup

Use cmd/list/list.go to get the device id.

go run ./cmd/list --projectid=$PROJECTID --oauth_clientid=... --oauth_secret=...

This will also create a token.json in the current directory (if it doesn't exist) with authentication tokens.

Set the DEVICE environment variable to your Device ID.

DEVICE=enterprises/81b04b74-.../devices/...
Quick Capture

Capture 15 seconds into `/tmp/15s.mp4'

go run ./cmd/capture --device=$DEVICE \
  --once --file_spec=/tmp/15s.mp4 --duration 15s
Long Capture

Capture until cancelled, writing to /tmp/B-YYYYMMDD-HHMMSS.mp4, rotating new files every 5 minutes.

go run cmd/capture/capture.go --device=$DEVICE \
  --oauth_clientid=... --oauth_secret=... \
  --file_spec=/tmp/B-%Y%m%d-%H%M%S.mp4 \
  --segment_time=5m

Doing this with short segments makes it easy to process chunks later without using a video editor. as you can just ignore/remove the files you're not interested in.

Time Lapse

Here's how to create a time lapse video from the segments recorded above.

cd /tmp/
# create list of all segments
rm segments
for f in B-*.mp4; do echo "file '$f'" >> segments; done
# concatanate segments into one big mp4 files.
ffmpeg -f concat -safe 0 -i segments  -c copy all.mp4
# create time lapse at 24x speed without audio
ffmpeg -i all.mp4 -filter:v "setpts=0.041*PTS" -an timelapse.041.mp4

To compute the setpts value, use 1/multiplier . (i.e. 10x speedup would be 1/10 => 0.1 )

-an drops audio.

Alternatively, you can skip the explicit concatenation step and generate the time lapse from multiple files with one ffmpeg command:

ffmpeg -f concat -safe 0 -i segments -filter:v "setpts=0.041*PTS" -an timelapse.mp4
Stream To YouTube

If your YouTube channel (or Twitch.tv or similar) supports livestreaming, you can...

go run cmd/capture/capture.go --device=$DEVICE \
  --oauth_clientid=... --oauth_secret=... \
  --stream_to=rtmps://a.rtmp.youtube.com/live2/$STRTEAMKEY
Config Options

Passing the OAuth information every time is annoying.

You can create a file that looks like this:

oauth_clientid=123456787-...something.apps.googleusercontent.com
oauth_secret=Xaqs05W_k9K-...
projectid=enterprises/81b04b74-...

And then pass it with the --config argument to any of the tools.

(This functionality brought to you by https://github.com/namsral/flag. You can also use environment variables.)

Docker

The Dockerfile can be used to simplify deployment of the capture tool.

Required environment variables:

  • DEVICE - device identify for the camera to stream from.
  • OAUTH_CLIENTID - OAuth ClientID
  • OAUTH_SECRET - OAuth Secret
  • TOKEN_FILE - Token file with access and refresh tokens. Run CLI manually once to create.

By default, it writes to files to /out inside the container.

docker-run.sh is an example of a shell script to launch the container.

Disclaimer

This is not an official Google project.

Documentation

Overview

Package nestsdm implements a client for the Nest Smart Device Management APIs (for cameras)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Extender

func Extender(ctx context.Context, sdm *smartdevicemanagement.Service, r *GenerateRtspStreamResults, device string)

func OAuthClient

func OAuthClient(clientID, secret, tokFile string) *http.Client

Types

type CameraImageTrait

type CameraImageTrait struct {
	MaxVideoResolution VideoResolution `json:"maxVideoResolution"`
}

type CameraLiveStreamTrait

type CameraLiveStreamTrait struct {
	MaxVideoResolution VideoResolution `json:"maxVideoResolution"`
	VideoCodecs        []string        `json:"videoCodecs"`
	AudioCodecs        []string        `json:"audioCodecs"`
}

type ExtendRtspStreamResults

type ExtendRtspStreamResults struct {
	StreamToken          string    `json:"streamToken"`
	StreamExtensionToken string    `json:"streamExtensionToken"`
	ExpiresAt            time.Time `json:"expiresAt"`
}

func ExtendRTSPStream

func ExtendRTSPStream(ctx context.Context, sdm *smartdevicemanagement.Service, device, set string) (*ExtendRtspStreamResults, error)

type GenerateRtspStreamResults

type GenerateRtspStreamResults struct {
	StreamURLs           ResultStreamURLs `json:"streamUrls"`
	StreamToken          string           `json:"streamToken"`
	StreamExtensionToken string           `json:"streamExtensionToken"`
	ExpiresAt            time.Time        `json:"expiresAt"`
}

func GenerateRTSPStream

func GenerateRTSPStream(ctx context.Context, sdm *smartdevicemanagement.Service, device string) (*GenerateRtspStreamResults, error)

type InfoTrait

type InfoTrait struct {
	CustomName string `json:"customName"`
}

type ResultStreamURLs

type ResultStreamURLs struct {
	RTSPURL string `json:"rtspUrl"`
}

type Traits

type Traits struct {
	Info             InfoTrait              `json:"sdm.devices.traits.Info"`
	CameraLiveStream *CameraLiveStreamTrait `json:"sdm.devices.traits.CameraLiveStream"`
	CameraImage      *CameraImageTrait      `json:"sdm.devices.traits.CameraImage"`
}

type VideoResolution

type VideoResolution struct {
	Width  int `json:"width"`
	Height int `json:"height"`
}

Directories

Path Synopsis
cmd
capture
capture retrieves video streams from Nest cameras and feeds them to ffmpeg.
capture retrieves video streams from Nest cameras and feeds them to ffmpeg.
list
lists all video capable devices exposed in the Nest Smart Device Management API for the specified project.
lists all video capable devices exposed in the Nest Smart Device Management API for the specified project.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL