start_stream.sh 1.53 KB
Newer Older
Nicolas PERNOUD's avatar
Nicolas PERNOUD committed
1
#!/bin/sh
2
3
#RTMP_SERVER="***PUT YOUR STREAMING API SERVER URL HERE***"
#KEY="***PUT YOUR STREAMING API KEY HERE***"
Nicolas PERNOUD's avatar
Nicolas PERNOUD committed
4

5
6
7
8
echo "STREAM BOX STARTING - Press [CTRL+C] to cancel..."
echo "Calculating target bitrate. Please wait..."
# Wait for the speedtest website to be reachable
until ping -c1 www.speedtest.net >/dev/null 2>&1; do : sleep 5; done
9
10
11
12
# Get the upload speed
UPLOAD_SPEED=$(speedtest --csv --no-download | cut -d ',' -f8 | cut -d '.' -f1)
# Take half the speed for bitrate
BITRATE="$(($UPLOAD_SPEED / 2))"
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Set resolution according to bitrate
if [ $BITRATE -gt 3000000 ]; then
    W=1920
    H=1080
elif [ $BITRATE -gt 1500000 ]; then
    W=1280
    H=720
elif [ $BITRATE -gt 500000 ]; then
    W=854
    H=480
else
    W=640
    H=360
fi
Nicolas PERNOUD's avatar
Nicolas PERNOUD committed
27
28

echo "STREAM BOX STARTED - Press [CTRL+C] to stop..."
29
30
31
32
33
34
35
36
37
38
echo "Bitrate is ${BITRATE}. Resolution is ${W}x${H}."

FRAMERATE=25

# Init camera
sudo modprobe bcm2835-v4l2
v4l2-ctl --set-fmt-video=width=${W},height=${H},pixelformat=4
v4l2-ctl --overlay=1
v4l2-ctl -p ${FRAMERATE}
v4l2-ctl --set-ctrl=video_bitrate=${BITRATE}
Nicolas PERNOUD's avatar
Nicolas PERNOUD committed
39

40
41
# Begin streaming
THREAD_QUEUE_SIZE=16384
Nicolas PERNOUD's avatar
Nicolas PERNOUD committed
42
43
while true; do
    #Audio stream
44
    #Video stream
Nicolas PERNOUD's avatar
Nicolas PERNOUD committed
45
46
    #Audio options
    #Video options
47
48
49
50
51
52
53
    #Push to streaming service
    ffmpeg \
        -thread_queue_size ${THREAD_QUEUE_SIZE} -f alsa -i plughw:1,0 \
        -thread_queue_size ${THREAD_QUEUE_SIZE} -f h264 -i /dev/video0 -r ${FRAMERATE} \
        -ac 1 -acodec libmp3lame -ar 44100 -b:a 128k \
        -vcodec copy \
        -g "$(($FRAMERATE * 2))" -strict experimental -f flv ${RTMP_SERVER}/${KEY}
Nicolas PERNOUD's avatar
Nicolas PERNOUD committed
54
done