Common Gateway Interface (CGI) With IP Cameras

Who uses this frequently? Who uses this beyond just pulling up an image/stream? I used it not long ago to auto power-down (restart) a PTZ at a particular time at night (In the Schedule area of the settings) to keep it "fresh" for website users because at the end of the day it slowed down.

Possible to come out with an in-depth tutorial on CGI and what it is/does when it comes to IP cameras?

  • When it comes to pulling images/streams should you use RTSP links versus CGI links?
  • What kind of scripting can you do with CGI links?

Web references are pretty weak when it comes to CGI.

On another note, RTSP links, for streaming purposes, vary a lot also.


CGI commands are used commonly with Axis VAPIX API (see this manual, e.g.). I am sure lots of traditional / older camera APIs use them.

"When it comes to pulling images/streams should you use RTSP links versus CGI links?"

An RTSP link just pulls video. A CGI command / URL can change settings / 'do things'.

"What kind of scripting can you do with CGI links?"

It depends on what commands are supported via CGI. For example, you might use it to control a PTZ camera (see Axis VAPIX PTZ manual). Here's an excerpt from that:

CGI is a way to invoke a command on a host in response to an action on that hosts web server. It goes way back to the early days of the web (mid to early 1990’s). It can be inefficient because it may require a new process fork every time a command is invoked via this interface. And resource leaks were common. Today many web services do not use CGI at all but rather invoke code directly in the web server via extension modules and frameworks designed to support this behavior more efficiently.

I suspect when you say CGI (which is a specific technology used to implement a web service) what you mean is the more generalized usage of a web interface to an IP camera for command and control or configuration of the camera.

Most IP cameras have a web service based API, be that implemented via CGI or otherwise. These APIs have historically been proprietary to individual camera manufacturer’s product lines. Over the past several years a particular standard of API has emerged that is supported by most popular cameras that is called ONVIF. Looking at the commands supported by ONFIV might be a good way to get an overview of the myriad of things one might ask of an IP camera’s API. Another would be to look at the original VAPIX command interface offered by AXIS.

I have used it for years to script cameras (Panasonic, Samsung) to do almost anything. The last thing i want to do on a 400 camera system is to have to keep track and go camera by camera to set the NTP function on, set the server address, change WDR, set the shutter speed, enable motion detection, focus the camera, flip the image, change frame rate, bit rate, etc. I can write a quick script that I can run once, or use it later if needed to run again, or change settings.

I have used this before the various companies had utilities to provide these functions. Even with the utilities, they don't always do every function. Or, if I want a script to refocus the cameras twice a year. I used to use this to enable the manual DST function before it was automated. I would create a scheduled job on a windows or linux server to run the command using WGET.

Very easy to do, but you need access to the CGI API document and a camera to test. Preferrable one of each model. Also, if the firmware changes, you should retest the command.

WGET also keeps a log, so you have a backup of what was done and when.

I would use MS Excel and the concatenate function to assembe the parts of the command, so I could insert a 0 or 1 to enable or disable a command, etc., so I could easily change my template to build a command.

I could in 5 minutes do all of the above commands to hundreds of cameras. If I kept my scripts, the next site is even easier, as I have tested the nuiances of the command.

Aaron, neat.

Do you maintain a list of IP addresses and then iterate over each one, calling whatever CGI command you need for that specific time (e.g., auto-focus might be http://ip_address_cam_1/cgi?auto-focus-now, http://ip_address_cam_2/cgi?auto-focus-now, etc.)?

In my excel template, I would simply enter the starting IP address and then auto fill it. If some addresses weren't cameras, wget would fail that IP after a certain number of retries. Or, simply copy and paste your IP address list.

I would then enter the command and the value.

One neat example, was a camera where instead of it always doing a full auto back focus, where it could get confused if something moved and it took longer, it could save the color and BW focus position. I would run a script to set the mode, change the camera to color, do a focus, change to BW, do a focus.

Below is a screenshot of my Excel template. Yellow are the instructions. Grey are the commands. Enter the username and password in A2. Put the command in C2. Some templates I made would have the On or Off or 1 or 0 in another field. Then, copy and past A3 down to a text file, save it as a .bat file.

Now, the Samsung iPOLiS Device Manager has an Advanced menu to send these commands right to the camera, without needing the script. I simply keep a list of common commands that I have tested to work.

motion /cgi-bin/event.cgi?msubmenu=motion&action=apply&device=1&sens=3
Face detect /cgi-bin/event.cgi?msubmenu=face&action=apply&device=1
PW /cgi-bin/basic.cgi?msubmenu=user&action=chgpwd&command=ok&name=admin&pass=1111
Flip /cgi-bin/system.cgi?msubmenu=camera&action=apply&vflip=1
Alarm /cgi-bin/system.cgi?msubmenu=input1&action=apply&device=0
Schedule /cgi-bin/system.cgi?msubmenu=camera&action=apply&active=always

See screenshots below...

Maybe this calls for a webinar (I know just the guy). Show the CGI document on how to get the command, then testing the command, then sending the command....

If anyone wants any of my script files from the Panasonic or Samsung cameras, please let me know. Script away. Stop manually configuring the same settings on your cameras....

Very cool!

This is exactly the kind of thing I'm getting at. This information is not well known and finding quality documentation is tough to find.

Just as an aside - this is the type of stuff that VMS manufacturers include in their user interface to allow users to control the camera. It's essentially doing the job of Aaron's spreadsheet, but probably not the way he wants. It's a good solution to help maintain a large number of cameras fairly easily.

Hans, one benefit of you doing this yourself is that it may allow you to do things the VMS does not support. For example, Aaron's use case of automatically refocusing x number of cameras. This is rarely supported inside a VMS (and often not even within the camera's managment tool) but something you could do with a homemade script.

John -

I agree 100%. I think it's great, and I think that VMS systems should do more stuff like this.

Great stuff, more of this would be great. .