Basic tutorial 2: Live preview with recording

This tutorial will walk you through steps that are required to get preview working both over RTSP as well as via HDMI and record at the same time. We will see how to start/stop recording using set_recording_state API.

Warning

We assume you have already checked Basic tutorial 1: Live preview. If not, please go through it before proceeding as this tutorial only explains the additional code i.e. start/stop of recording.

Code

test-preview-record.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import argparse
import sys
import time

from sdk.camera import CameraClient

def main(protocol=None):
    print("\nPython %s\n" % sys.version)
    parser = argparse.ArgumentParser()
    parser.add_argument('--ip', help='ip address of the camera', default='127.0.0.1')
    parser.add_argument('--username', help='username of the camera', default='admin')
    parser.add_argument('--password', help='password of the camera', default='admin')
    args = parser.parse_args()
    ip_addr = args.ip
    username = args.username
    password = args.password

    with CameraClient.connect(ip_address=ip_addr, username=username, password=password) as camera_client:
        print('supported resolutions: ' + str(camera_client.resolutions))
        print('supported encodetype: ' + str(camera_client.encodetype))
        print('supported bitrates: ' + str(camera_client.bitrates))
        print('supported framerates: ' + str(camera_client.framerates))
        print(camera_client.configure_preview(resolution="1080P", display_out=1))

        camera_client.set_preview_state("on")

        print(camera_client.preview_url)

        print("Running for 60 seconds")
        time.sleep(60)
        print("Start recording")
        camera_client.set_recording_state("on")

        print("Running for 60 seconds")
        time.sleep(60)

        camera_client.set_recording_state("off")
        camera_client.set_preview_state("off")
        camera_client.logout()


if __name__ == '__main__':
    main()

Note

This script is present in the tests/ directory of the SDK.

Walkthrough

As you would have noticed most of the code is the same as the Basic tutorial 1: Live preview. So in this walkthrough we will just focus on the add-ons i.e. start/stop record.

You can start/stop recording using the set_recording_state API anytime after obtaining the camera client handle. In this tutorial we start recording after 60 seconds of preview.

print("Start recording")
camera_client.set_recording_state("on")

We keep recording for 60 seconds.

print("Running for 60 seconds")
time.sleep(60)

Then stop recording using the set_recording_state API.

camera_client.set_recording_state("off")

It’s time to test

Once you have the application/script ready. You can test it by following the steps mentioned in Test section of the Getting started page.