Creating URLs for embedding cameras to a website
This section explains how to manually generate URLs for viewing video from cameras, including getting a token for a user session.
The examples below allow you to write code for your website that will display, for example, only active cameras of a certain user. You will also learn how to create links to a stream in the required format.
For an individual camera, there is a faster way to automatically get the URL: click Share next to the camera in the camera list; but in this case the URL is generated for only one camera.
To create a link for embedding video to a webpage:
-
Create a user — this user will be used to generate a token for accessing a camera. For example, let's create the user
webuser
and add it to your Organization. -
Give this user permissions to manage cameras in an Organization. These cameras must be the ones that you want to display on an external website.
In the user properties, on the Access to cameras tab, click the camera icon next to the folder that you want to give the user access to.
-
Get the session ID for this user. Use the following API v2 call:
curl http://watcher-ip/vsaas/api/v2/auth/login -H 'Content-Type: application/json' --data-binary '{"login":"admin_temp", "password":"admin_temp"}'
The server's response looks like the following:
{ "groups_count": 0, "notification_email": null, "session": "Z-aCeqoKapk-DhfnqSGEOI5kVT0", "is_admin": true, "login": "webuser" }
Details about /api/v2/auth/login
You'll need to copy
"session": "Z-aCeqoKapk-DhfnqSGEOI5kVT0"
from the response. -
Get cameras available for the created user:
curl http://WATCHER-IP/vsaas/api/v2/cameras/ -H "x-vsaas-session:Z-aCeqoKapk-DhfnqSGEOI5kVT0"
Alternatively, if you need only certain cameras, get them by their names:
curl http://WATCHER-IP/vsaas/api/v2/cameras/CAMERA_NAME -H "x-vsaas-session:Z-aCeqoKapk-DhfnqSGEOI5kVT"
The server's response looks like the following:
{ 'stream_url': 'fake://fake', 'playback_config': {'token': 'WyIxMzgzIiwyXQ.DrEHcw.h7RL4o83OSbFMrW-wMWJXcdXfgU'}, 'dvr_path': None, 'title': 'test', 'substream_url': '', 'agent_id': None, 'access': 'private', 'static': True, 'onvif_url': None, 'agent_serial': None, 'agent_status': None, 'external_id': None, 'groups': [], 'owner': 'tst', 'agent_model': None, 'comment': '', 'user_attributes': {}, 'dvr_space': None, 'coordinates': '37.768665 55.652579', 'enabled': True, 'server': 'WATCHER-IP', 'postal_address': '', 'thumbnails': True, 'permissions': {'dvr': None, 'ptz': False, 'edit': True, 'view': True}, 'dvr_depth': None, 'stream_status': {'lifetime': 1963704, 'source_error': None, 'alive': True, 'https_port': None, 'http_port': None, 'bitrate': 183, 'rtmp_port': 1935, 'rtsp_port': 8554, 'server': 'WATCHER-IP'}, 'onvif_profile': None, 'name': 'CAMERA_NAME', 'dvr_protected': False, 'thumbnails_url': None, 'onvif_ptz': False, 'agent_key': None}
You'll need
server
,name
, andtoken
. -
Use the values of
server
,name
, andtoken
to form a URL for accessing the camera through embed.html. An example of such a URL:http://WATCHER-IP/vsaas/embed/CAMERA_NAME?token=WyIxMzgzIiwyXQ.DrEHcw.h7RL4o83OSbFMrW-wMWJXcdXfgU
Scripts for getting the URL to embed a camera to websites
After you create a dedicated user (webuser
in the procedure above), you can run the following script that takes the login and password as input data and returns for each camera the code for embedding it to a web page.
Python script for creating RTSP URL with output in Linux CLI
import os
import sys
import requests
server = sys.argv[1]
s = server.split('//')
path = ''
file = str(path) + s[1] + '.txt'
sysargv_auth = {"login": sys.argv[2], "password": sys.argv[3]}
url = sys.argv[1] + '/vsaas/api/v2/'
print(url, sysargv_auth)
print(len(sys.argv))
def get_session():
r = requests.post(url + 'auth/login', json=sysargv_auth)
print(r)
if r.status_code == 200:
session_id = r.json()['session']
file_auth = open(file, 'w+')
file_auth.write(session_id)
file_auth.close()
return session_id
else:
print('Get session: delete file')
os.remove(file)
exit(1)
def get_cams():
if os.path.isfile(file):
print('Is file')
file_auth = open(file, 'r')
session_id = file_auth.read()
else:
print('no file')
session_id = get_session()
headers = {'x-vsaas-session': session_id, 'X-Page-Limit':'99'}
r = requests.get(url + 'cameras', headers=headers)
if r.status_code == 403:
os.remove(file)
exit(1)
cam_list = 'link\n'
for el in r.json():
if el['stream_status']['server']:
server = str(el['stream_status']['server'])
link = 'rtsp://' + server + ':554/' + el['name'] + '?token=' + el['playback_config']['token']
else:
server = 'null'
link = 'null'
cam_list += link + '\n'
return cam_list
print(get_cams())
PHP script for creating an HTTP URL to insert a camera to a website
<?php
$server = $_GET["server"];
$auth = [ 'login' => $_GET['login'], 'password' => $_GET['pass']]; // 1 line instead of 3
if(empty($server)) {
echo "The server address and auth data not provided";
header('HTTP/1.0 204 No Content');
error_log('No server address provided', 4);
die();
}
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $auth ),
'header'=> 'Content-Type: application/json'
)
);
$context = stream_context_create( $options );
$result = file_get_contents( "http://$server/vsaas/api/v2/auth/login", false, $context );
$response = json_decode($result, true);
$session = $response['session'];
$get_cams = array(
'http' => array(
'method' => 'GET',
'header' => 'Content-Type: application/json',
'header' => "x-vsaas-session: $session"
'header' => 'x-page-limit: 99'
)
);
$context_cam = stream_context_create($get_cams);
$result_cam = file_get_contents("http://$server/vsaas/api/v2/cameras", false, $context_cam);
$resp_cam = json_decode($result_cam, true);
foreach($resp_cam as $key => $cam) {
$cam_token = $cam['playback_config']['token'];
$cams_url[] = "http://" .$cam['stream_status']['server']. '/' .$cam['name']."/embed.html?token=$cam_token";
}
foreach($cams_url as $url){
echo "<iframe style=\"width:640px; height:480px;\" allowfullscreen src=\"$url\"></iframe>";
};
?>