diff --git a/DOCUMENTATION_FR.md b/DOCUMENTATION_FR.md new file mode 100644 index 0000000000000000000000000000000000000000..dc4c208d093f3f9a91c9d5cafa962d6eb69b7376 --- /dev/null +++ b/DOCUMENTATION_FR.md @@ -0,0 +1,26 @@ +# Système de streaming automatique de la Métropole de Lyon + +## Objectif + +L'objectif de ce système est de permettre de diffuser un flux vidéo et audio en direct sur le service Youtube. + +## Diffusion d'un flux + +1. Allumer le boitier 4G en appuyant sur le bouton marche (laisser appuyer quelques secondes). Si le boitier 4G ne s'allume pas ou si le voyant d'alimentation clignote, le brancher à l'aide de d'adaptateur secteur joint. +2. Vérifier avec un terminal WiFi (smartphone, PC ou tablette), que le réseau "SFR-E26714" est bien disponible. **Pour autant, il n'est pas utile de vous y connecter**. Si le réseau n'est pas visible, reprenez l'étape 1. +3. Brancher le micro sur le boitier caméra à l'aide du cable USB fourni. +4. Brancher le boitier caméra au secteur électrique à l'aide de l'adaptateur (de couleur blanche) joint. + +## Consultation d'un flux en direct + +1. Demander le mot de passe du compte streaminggrandlyon aux personnes habilitées. +2. Ouvrez un navigateur web récent (Firefox ou Chrome à jour). +3. Aller sur www.youtube.com. +4. Connecter vous avec le compte streaminggrandlyon. +5. Pour voir le flux en direct, aller sur https://www.youtube.com/live_dashboard . +6. Pour voir les rediffusions (disponibles pendant 14 jours), aller sur https://studio.youtube.com, puis cliquez sur "Vidéos" dans le menu de droite et sur l'onglet "Diffusions en direct". +7. Il est possible de créer un lien de partage en cliquant sur le bouton "3 points verticaux" à côté de la vidéo souhaitée. + +## Arrêter la diffusion + +Il suffit de tout débrancher... Et de tout ranger **très soigneusement** dans la boite de transport. diff --git a/README.md b/README.md index a829c6e54c29e080a6f881d321d023f17f6712ef..65dec22b11231eee55d05c2fe0365a91f9585bcb 100644 --- a/README.md +++ b/README.md @@ -4,14 +4,15 @@ A simple script to turn an headless raspberry pi 4 (with a **good** microphone) ## Setup +Put all the scripts in `/home/pi/` and make them executable with `chmod +x *.sh`. + Install speedtest-cli : `sudo apt install speedtest-cli`. -Set up the pi to boot in logged in (pi user) console mode with `sudo raspi-config`. -Copy the **start_stream.sh** script into `/home/pi` and make it executable with `chmod +x ./start_stream.sh`. +Set up the pi to boot in auto logged in (pi user) console mode with `sudo raspi-config`. -Put your youtube API key into the script. +Put your streaming service URL and API key into the script. -Add `./start_stream.sh` at the end of `/home/pi/.bashrc`. +Add `./autostart_stream.sh` at the end of `/home/pi/.bashrc`. ## Usage diff --git a/autostart_stream.sh b/autostart_stream.sh new file mode 100644 index 0000000000000000000000000000000000000000..d0639351dfb2829b0ea1978897ec2b40b69e81fa --- /dev/null +++ b/autostart_stream.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +function autoStartStreamIfTTY() { + isTTY=$(tty | awk '{if ($0=="/dev/tty1") {print "true"}else{print "false"}}') + + if [ $isTTY == "true" ]; then + printf "Starting streaming\n" + ./start_stream.sh + fi +} + +autoStartStreamIfTTY diff --git a/start_stream.sh b/start_stream.sh index aac87b863557d12a8382e501c02670e0e73f4601..2e304e9cdbf13fc476b4a23f6627e8ad1d189d8e 100644 --- a/start_stream.sh +++ b/start_stream.sh @@ -1,34 +1,54 @@ #!/bin/sh -KEY="***PUT YOUR YOUTUBE API KEY HERE***" +#RTMP_SERVER="***PUT YOUR STREAMING API SERVER URL HERE***" +#KEY="***PUT YOUR STREAMING API KEY HERE***" -# Wait for the network to be up -while [ $(cat /sys/class/net/wlan0/operstate) != "up" ]; do - echo "Wifi is down. Waiting..." - sleep 5 -done +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 # 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))" +# 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 echo "STREAM BOX STARTED - Press [CTRL+C] to stop..." -echo "Bitrate is ${BITRATE}" +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} +# Begin streaming +THREAD_QUEUE_SIZE=16384 while true; do - #Pipe to ffmpeg #Audio stream - #Video stream (from pipe) + #Video stream #Audio options #Video options - #Push to youtube - raspivid --nopreview --output - -t 0 \ - -w 1280 -h 720 -fps 25 -b ${BITRATE} | - ffmpeg -re \ - -thread_queue_size 65536 \ - -f alsa -i plughw:1,0 \ - -thread_queue_size 65536 \ - -f h264 -i - -r 25 \ - -ac 1 -acodec libmp3lame -ar 44100 -b:a 128k \ - -vcodec copy \ - -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/$KEY + #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} done diff --git a/stop_stream.sh b/stop_stream.sh new file mode 100644 index 0000000000000000000000000000000000000000..96b4195400985ea05ae3a1291457dce75aee65a6 --- /dev/null +++ b/stop_stream.sh @@ -0,0 +1,3 @@ +#!/bin/sh +pkill start_stream.sh +pkill ffmpeg