mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-05-05 21:17:43 +03:00
137 lines
4.5 KiB
Plaintext
Executable File
137 lines
4.5 KiB
Plaintext
Executable File
#!/command/with-contenv bash
|
|
# shellcheck shell=bash
|
|
# Start the go2rtc service
|
|
|
|
set -o errexit -o nounset -o pipefail
|
|
|
|
# Logs should be sent to stdout so that s6 can collect them
|
|
|
|
function get_ip_and_port_from_supervisor() {
|
|
local ip_address
|
|
# Example: 192.168.1.10/24
|
|
local ip_regex='^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/[0-9]{1,2}$'
|
|
if ip_address=$(
|
|
curl -fsSL \
|
|
-H "Authorization: Bearer ${SUPERVISOR_TOKEN}" \
|
|
-H "Content-Type: application/json" \
|
|
http://supervisor/network/interface/default/info |
|
|
jq --exit-status --raw-output '.data.ipv4.address[0]'
|
|
) && [[ "${ip_address}" =~ ${ip_regex} ]]; then
|
|
ip_address="${BASH_REMATCH[1]}"
|
|
echo "[INFO] Got IP address from supervisor: ${ip_address}"
|
|
else
|
|
echo "[WARN] Failed to get IP address from supervisor"
|
|
return 0
|
|
fi
|
|
|
|
local webrtc_port
|
|
local port_regex='^([0-9]{1,5})$'
|
|
if webrtc_port=$(
|
|
curl -fsSL \
|
|
-H "Authorization: Bearer ${SUPERVISOR_TOKEN}" \
|
|
-H "Content-Type: application/json" \
|
|
http://supervisor/addons/self/info |
|
|
jq --exit-status --raw-output '.data.network["8555/tcp"]'
|
|
) && [[ "${webrtc_port}" =~ ${port_regex} ]]; then
|
|
webrtc_port="${BASH_REMATCH[1]}"
|
|
echo "[INFO] Got WebRTC port from supervisor: ${webrtc_port}"
|
|
else
|
|
echo "[WARN] Failed to get WebRTC port from supervisor"
|
|
return 0
|
|
fi
|
|
|
|
export FRIGATE_GO2RTC_WEBRTC_CANDIDATE_INTERNAL="${ip_address}:${webrtc_port}"
|
|
}
|
|
|
|
function set_libva_version() {
|
|
local ffmpeg_path
|
|
ffmpeg_path=$(python3 /usr/local/ffmpeg/get_ffmpeg_path.py)
|
|
LIBAVFORMAT_VERSION_MAJOR=$("$ffmpeg_path" -version | grep -Po "libavformat\W+\K\d+")
|
|
export LIBAVFORMAT_VERSION_MAJOR
|
|
}
|
|
|
|
function migrate_addon_config_dir() {
|
|
if ! mountpoint --quiet /homeassistant_config; then
|
|
# Not running as a Home Assistant add-on
|
|
return 0
|
|
fi
|
|
|
|
local new_config_file="/config/config.yml"
|
|
local new_config_file_yaml="${new_config_file//.yml/.yaml}"
|
|
if [[ -f "${new_config_file_yaml}" || -f "${new_config_file}" ]]; then
|
|
# Already migrated
|
|
return 0
|
|
fi
|
|
unset new_config_file new_config_file_yaml
|
|
|
|
local old_config_file="/homeassistant_config/frigate.yml"
|
|
local old_config_file_yaml="${old_config_file//.yml/.yaml}"
|
|
local new_config_file="/config/config.yml"
|
|
if [[ -f "${old_config_file_yaml}" ]]; then
|
|
old_config_file="${old_config_file_yaml}"
|
|
new_config_file="/config/config.yaml"
|
|
elif [[ ! -f "${old_config_file}" ]]; then
|
|
# Nothing to migrate
|
|
return 0
|
|
fi
|
|
unset old_config_file_yaml
|
|
|
|
local db_path
|
|
db_path=$(yq eval '.database.path' "${old_config_file}")
|
|
|
|
if [[ "${db_path}" == "null" ]]; then
|
|
db_path="/config/frigate.db"
|
|
fi
|
|
|
|
if [[ "${db_path}" == /config/* ]]; then
|
|
# replace /config/ prefix with /homeassistant_config/
|
|
old_db_path="/homeassistant_config/${db_path:8}"
|
|
|
|
if [[ -f "${old_db_path}" ]]; then
|
|
new_db_dir="$(dirname "${db_path}")"
|
|
echo "[INFO] Migrating database from '${old_db_path}' to '${new_db_dir}' dir..."
|
|
mv -vf "${old_db_path}"* "${new_db_dir}"
|
|
fi
|
|
fi
|
|
|
|
echo "[INFO] Migrating config from '${old_config_file}' to '${new_config_file}'..."
|
|
mv -vf "${old_config_file}" "${new_config_file}"
|
|
}
|
|
|
|
migrate_addon_config_dir
|
|
|
|
set_libva_version
|
|
|
|
if [[ -f "/dev/shm/go2rtc.yaml" ]]; then
|
|
echo "[INFO] Removing stale config from last run..."
|
|
rm /dev/shm/go2rtc.yaml
|
|
fi
|
|
|
|
if [[ ! -f "/dev/shm/go2rtc.yaml" ]]; then
|
|
echo "[INFO] Preparing new go2rtc config..."
|
|
|
|
if [[ -n "${SUPERVISOR_TOKEN:-}" ]]; then
|
|
# Running as a Home Assistant add-on, infer the IP address and port
|
|
get_ip_and_port_from_supervisor
|
|
fi
|
|
|
|
python3 /usr/local/go2rtc/create_config.py
|
|
else
|
|
echo "[WARNING] Unable to remove existing go2rtc config. Changes made to your frigate config file may not be recognized. Please remove the /dev/shm/go2rtc.yaml from your docker host manually."
|
|
fi
|
|
|
|
readonly config_path="/config"
|
|
|
|
if [[ -x "${config_path}/go2rtc" ]]; then
|
|
readonly binary_path="${config_path}/go2rtc"
|
|
echo "[WARN] Using go2rtc binary from '${binary_path}' instead of the embedded one"
|
|
else
|
|
readonly binary_path="/usr/local/go2rtc/bin/go2rtc"
|
|
fi
|
|
|
|
echo "[INFO] Starting go2rtc..."
|
|
|
|
# Replace the bash process with the go2rtc process, redirecting stderr to stdout
|
|
exec 2>&1
|
|
exec "${binary_path}" -config=/dev/shm/go2rtc.yaml
|