main
Raw Download raw file
 1package protect
 2
 3import (
 4	"context"
 5	"fmt"
 6	"io"
 7	"net/url"
 8	"strconv"
 9)
10
11// DownloadClip downloads a video clip for the specified time range.
12// The video is streamed directly to the provided writer.
13func (c *Client) DownloadClip(ctx context.Context, cameraID string, startMs, endMs int64, w io.Writer) error {
14	params := url.Values{}
15	params.Set("camera", cameraID)
16	params.Set("start", strconv.FormatInt(startMs, 10))
17	params.Set("end", strconv.FormatInt(endMs, 10))
18
19	path := "/video/export?" + params.Encode()
20	req, err := c.newRequest(ctx, "GET", path, nil)
21	if err != nil {
22		return fmt.Errorf("creating request: %w", err)
23	}
24
25	// Use longer timeout for downloads
26	resp, err := c.do(req)
27	if err != nil {
28		return fmt.Errorf("requesting video export: %w", err)
29	}
30	defer resp.Body.Close()
31
32	_, err = io.Copy(w, resp.Body)
33	if err != nil {
34		return fmt.Errorf("streaming video: %w", err)
35	}
36
37	return nil
38}