Skip to content

Instantly share code, notes, and snippets.

@ajatoledo
Last active April 30, 2023 22:21
Show Gist options
  • Save ajatoledo/48f03ae246b2d4555d1683c86e189846 to your computer and use it in GitHub Desktop.
Save ajatoledo/48f03ae246b2d4555d1683c86e189846 to your computer and use it in GitHub Desktop.
Bash script to configure MainFormat and ExtraFormat streams for Amcrest Cameras; requires curl, URI encode any special characters, e.g. `[` and `]`; review Amcrest API documentation for further options
#!/usr/bin/env bash
# Define default configuration settings
Stream="ExtraFormat"
# Alert message
alert=$(printf "%10s\n""\n Usage: $0 offers the following arguments:\n
-i, --ip camera_ip, REQUIRED (string)
-u, --username camera_username, REQUIRED (string)
-p, --password camera_password, REQUIRED (string)
-S, --stream stream, options are ExtraFormat and MainFormat, default = ExtraFormat (string)
-B, --bitrate bitrate, default = 256 (integer)
-b, --bitrate-control bitrate-control, default = CBR (string)
-C, --compression compression, default = H.264 (string)
-R, --resolution resolution, default = D1 (string)
-F, --fps fps, default = 5, (integer)
-G, --gop frame interval, default = 5 (integer)
-H, --height height, default = 480 (integer)
-W, --width width, default = 704 (integer)
-h, --help show help menu and exit")
# Parse command-line arguments
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-i|--ip)
camera_ip="$2"
shift
shift
;;
-u|--username)
camera_username="$2"
shift
shift
;;
-p|--password)
camera_password="$2"
shift
shift
;;
-S|--stream)
Stream="$2"
shift
shift
;;
-B|--bitrate)
BitRate="$2"
shift
shift
;;
-b|--bitrate-control)
BitRateControl="$2"
shift
shift
;;
-C|--compression)
Compression="$2"
shift
shift
;;
-R|--resolution)
CustomResolutionName="$2"
shift
shift
;;
-F|--fps)
FPS="$2"
shift
shift
;;
-G|--gop)
GOP="$2"
shift
shift
;;
-H|--height)
Height="$2"
shift
shift
;;
-W|--width)
Width="$2"
shift
shift
;;
-h|--help)
echo -e "$alert\n"
exit
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
# Check whether $Stream is MainFormat or ExtraFormat
if [ "$Stream" != "MainFormat" ] && [ "$Stream" != "ExtraFormat" ]
then
alert=$(printf "%10s\n\n Usage: $0 takes Stream Types of either MainFormat or ExtraFormat\n")
echo -e "\nUnknown stream type: $Stream\n$alert\n"
exit 1
fi
# Set configuration settings based on the stream type
if [ "$Stream" == "ExtraFormat" ]
then
BitRate="${BitRate:-256}"
BitRateControl="${BitRateControl:-CBR}"
Compression="${Compression:-H.264}"
CustomResolutionName="${CustomResolutionName:-D1}"
FPS="${FPS:-5}"
GOP="${GOP:-5}"
Height="${Height:-480}"
Width="${Width:-704}"
else
BitRate="${BitRate:-8192}"
BitRateControl="${BitRateControl:-VBR}"
Compression="${Compression:-H.264}"
CustomResolutionName="${CustomResolutionName:-2592x1944}"
FPS="${FPS:-20}"
GOP="${GOP:-40}"
Height="${Height:-1944}"
Width="${Width:-2592}"
# alert message
alert=$(printf "%10s\n""\n Usage: $0 offers the following arguments:\n
-i, --ip camera_ip, REQUIRED (string)
-u, --username camera_username, REQUIRED (string)
-p, --password camera_password, REQUIRED (string)
-S, --stream stream, options are ExtraFormat and MainFormat, default = ExtraFormat (string)
-B, --bitrate) bitrate, default = 8192 (integer)
-b, --bitrate-control bitrate-control, default = VBR (string)
-C, --compression compression, default = H.264 (string)
-R, --resolution resolution, default = 2592x1944 (string)
-F, --fps fps, default = 20, (integer)
-G, --gop frame interval, default = 40 (integer)
-H, --height height, default = 1944 (integer)
-W, --width width, default = 2592 (integer)
-h, --help show help menu and exit")
fi
# Create API call
api_endpoint="http://$camera_ip/cgi-bin/configManager.cgi?action=setConfig&\
Encode%5B0%5D.$Stream%5B0%5D.Video.BitRate=$BitRate&\
Encode%5B0%5D.$Stream%5B0%5D.Video.BitRateControl=$BitRateControl&\
Encode%5B0%5D.$Stream%5B0%5D.Video.Compression=$Compression&\
Encode%5B0%5D.$Stream%5B0%5D.Video.CustomResolutionName=$CustomResolutionName&\
Encode%5B0%5D.$Stream%5B0%5D.Video.FPS=$FPS&\
Encode%5B0%5D.$Stream%5B0%5D.Video.GOP=$GOP&\
Encode%5B0%5D.$Stream%5B0%5D.Video.Height=$Height&\
Encode%5B0%5D.$Stream%5B0%5D.Video.Width=$Width"
# Check that all required arguments are present
if [ -z "$camera_ip" ] || [ -z "$camera_username" ] || [ -z "$camera_password" ]
then
echo -e "$alert\n"
exit 1
fi
# Submit command; authenticate with camera using digest
curl -s -k -X POST --digest -u $camera_username:$camera_password -f "$api_endpoint" > /dev/null
# Check if the configuration settings were set successfully
if [ $? -eq 0 ]
then
echo "$Stream configuration settings set successfully for: $camera_ip"
else
echo -e "\nFailed to set $Stream configuration settings for: $camera_ip\n$alert\n"
fi
@ajatoledo
Copy link
Author

ajatoledo commented Apr 30, 2023

Use Case

I run Frigate NVR with my Amcrest Cameras. Overall, the setup works well; however, when the Amcrest cameras lose power, their settings default back to factory every time, which requires that I reconfigure the cameras so they appear in Frigate again. The cameras are POE, and I don't lose power frequently, but every firmware upgrade on my switch power cycles the cameras. This script solves my problem by running config updates to my cameras to ensure they are configured as expected.

I can't promise this will work for everyone; however, I wanted to share since Amcrest documentation isn't the best.

Amcrest Camera Configuration Script

MainFormat Stream

When MainFormat stream type is specified; it takes the following arguments:

  • -i, --ip: camera IP address (required, string)
  • -u, --username: camera username (required, string)
  • -p, --password: camera password (required, string)
  • -S, --stream: stream type, options are ExtraFormat and MainFormat, default = ExtraFormat (string)
  • -B, --bitrate: bitrate, default = 8192 (integer)
  • -b, --bitrate-control: bitrate control, default = VBR (string)
  • -C, --compression: compression, default = H.264 (string)
  • -R, --resolution: resolution, default = 2592x1944 (string)
  • -F, --fps: frames per second, default = 20 (integer)
  • -G, --gop: frame interval, default = 40 (integer)
  • -H, --height: height, default = 1944 (integer)
  • -W, --width: width, default = 2592 (integer)
  • -h, --help: show help menu and exit

ExtraFormat Stream

When ExtraFormat stream type is specified; it takes the following arguments:

  • -i, --ip: camera IP address (required, string)
  • -u, --username: camera username (required, string)
  • -p, --password: camera password (required, string)
  • -S, --stream: stream type, options are ExtraFormat and MainFormat, default = ExtraFormat (string)
  • -B, --bitrate: bitrate, default = 256 (integer)
  • -b, --bitrate-control: bitrate control, default = CBR (string)
  • -C, --compression: compression, default = H.264 (string)
  • -R, --resolution: resolution, default = D1 (string)
  • -F, --fps: frames per second, default = 5 (integer)
  • -G, --gop: frame interval, default = 5 (integer)
  • -H, --height: height, default = 480 (integer)
  • -W, --width: width, default = 704 (integer)
  • -h, --help: show help menu and exit

Usage

./camera_stream_config -i <camera_ip> -u <camera_username> -p <camera_password> [options]

Example

./camera_stream_config -i 192.168.1.100 -u admin -p password -S ExtraFormat -B 512 -b VBR -C H.265 -R 1080p -F 30 -G 60 -H 1080 -W 1920

Notes

  • The script requires the curl command to be installed.
  • The Stream option determines whether the configuration settings are for the main or extra stream. The default is ExtraFormat.
  • The script will check whether the Stream option is valid. If it is not MainFormat or ExtraFormat, an error message will be displayed.
  • The script will set the configuration settings based on the Stream option
    • If ExtraFormat is selected, the default values will be used.
    • If MainFormat is selected, the values will be set to higher values than the defaults.
  • If any of the required arguments (-i|--ip, -u|--username, -p|--password) are missing, an error message will be displayed.
  • The script will authenticate with the camera using digest authentication.

Amcrest API Materials

Amcrest HTTP API Documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment