summaryrefslogtreecommitdiff
path: root/tools/ipcam_rtsp2hls.sh
blob: a4687158caa798b00a9a38056e27ed0967216bb8 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash

PROGNAME="$0"
OUTDIR=/var/ipcamfs # should be tmpfs
PORT=554
NAME=
IP=
USER=
PASSWORD=
DEBUG=0
CHANNEL=1
FORCE_UDP=0
FORCE_TCP=0

die() {
  echo >&2 "error: $@"
  exit 1
}

usage() {
  cat <<EOF
usage: $PROGNAME [OPTIONS] COMMAND

Options:
  --ip    camera IP
  --port  RTSP port (default: 554)
  --name  camera name (chunks will be stored under $OUTDIR/{name}/)
  --user
  --password
  --debug
  --force-tcp
  --force-udp
  --channel 1|2

EOF
  exit
}

validate_channel() {
  local c="$1"
  case "$c" in
    1|2)
      :
      ;;
    *)
      die "Invalid channel"
      ;;
    esac
}

[ -z "$1" ] && usage

while [[ $# -gt 0 ]]; do
  case "$1" in
    --ip|--port|--name|--user|--password)
      _var=${1:2}
      _var=${_var^^}
      printf -v "$_var" '%s' "$2"
      shift
      ;;

    --debug)
      DEBUG=1
      ;;

    --force-tcp)
      FORCE_TCP=1
      ;;

    --force-udp)
      FORCE_UDP=1
      ;;

    --channel)
      CHANNEL="$2"
      shift
      ;;

    *)
      die "Unrecognized argument: $1"
      ;;
  esac
  shift
done

[ -z "$IP" ] && die "You must specify camera IP address (--ip)."
[ -z "$PORT" ] && die "Port can't be empty."
[ -z "$NAME" ] && die "You must specify camera name (--name)."
[ -z "$USER" ] && die "You must specify username (--user)."
[ -z "$PASSWORD" ] && die "You must specify username (--password)."
validate_channel "$CHANNEL"

if [ ! -d "${OUTDIR}/${NAME}" ]; then
  mkdir "${OUTDIR}/${NAME}" || die "Failed to create ${OUTDIR}/${NAME}!"
fi

args=
if [ "$DEBUG" = "1" ]; then
  args="-v info"
else
  args="-nostats -loglevel error"
fi

if [ "$FORCE_TCP" = "1" ]; then
  args="$args -rtsp_transport tcp"
elif [ "$FORCE_UDP" = "1" ]; then
  args="$args -rtsp_transport udp"
fi

ffmpeg $args -i rtsp://${USER}:${PASSWORD}@${IP}:${PORT}/Streaming/Channels/${CHANNEL} \
  -c:v copy -c:a copy -bufsize 1835k \
  -pix_fmt yuv420p \
  -flags -global_header -hls_time 5 -hls_list_size 6 -hls_wrap 5 \
  ${OUTDIR}/${NAME}/live.m3u8