aboutsummaryrefslogtreecommitdiffstats
path: root/encode-libvlc.sh
blob: 361a743cce8c65c1a42c9e7621bb32079b335759 (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
115
116
#!/bin/bash
#
# Encode programme using libVLC input from
# dabplus-enc or toolame
#
# monitor processes, and restart if necessary
# Optionally send an email when restart happens

printerr() {
    echo -e "\033[01;31m$1\033[0m"
    logger -p local0.error -t "$ID" "$1"
}

printmsg() {
    echo -e "\033[01;32m$1\033[0m"
    logger -p local0.notice -t "$ID" "$1"
}

set -u

# check number of arguments
if [[ "$#" -lt 3 ]] ; then
    echo "Usage $0 url id destination [encoder]"
    echo "Encoder shall be 'dabplus-enc' or 'toolame'"
    exit 1
fi

URL=$1
ID=$2
DST=$3

if [[ "$#" -gt 3 ]] ; then
    ENC=$4
else
    ENC="dabplus-enc"
fi

if [[ "$#" -gt 4 ]]; then
    shift 4
    OPTIONS=$@
else
   if [[ "$ENC" == "dabplus-enc" ]]; then
       OPTIONS="-b 80 -r 32000"
   else
       OPTIONS="-b 128 -s 48 -L"
   fi
fi

running=1

encoderpid=0

# The trap for Ctrl-C
sigint_trap() {
    printerr "Got Ctrl-C, killing mplayer and encoder"
    running=0

    if [[ "$encoderpid" != "0" ]] ; then
        kill -TERM $encoderpid
        sleep 2
        kill -KILL $encoderpid
    fi

    printmsg "quitting"
    exit
}

trap sigint_trap SIGTERM
trap sigint_trap SIGINT

while [[ "$running" == "1" ]]
do

    printmsg "Launching encoder"
    if [[ "$ENC" == "dabplus-enc" ]] ; then
        dabplus-enc -v "$URL" $OPTIONS -o "$DST" -l &
        encoderpid=$!
    elif [[ "$ENC" == "toolame" ]] ; then
        toolame $OPTIONS -V "$URL" "$DST" &
        encoderpid=$!
    fi
    printerr "Detected crash of encoder!"

    sleep 5

    checkloop=1
    while [[ "$checkloop" == "1" ]]
    do
        sleep 2

        kill -s 0 $encoderpid
        if [[ "$?" != "0" ]] ; then
            printerr "the encoder died"

            encoderpid=0
            checkloop=0
        fi
    done

    MAILTO=$(cat site/mail-warning.txt)

    if [[ "$MAILTO" != "" ]] ; then
        NOW=$(date)

        mail -s "Encoder $ID restart $URL" "$MAILTO" << EOF
The encoder id:$ID
encoding $URL -> $DST using encode-libvlc was restarted at
$NOW

EOF

    fi
    sleep 5

done