Skip to content
Snippets Groups Projects
Commit e7187049 authored by Nicolas Pernoud's avatar Nicolas Pernoud
Browse files

feat : switched to ffmpeg with v4l2 driver for better performance and audio sync

parent aaebaa54
Branches master
No related tags found
No related merge requests found
# 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.
...@@ -4,14 +4,15 @@ A simple script to turn an headless raspberry pi 4 (with a **good** microphone) ...@@ -4,14 +4,15 @@ A simple script to turn an headless raspberry pi 4 (with a **good** microphone)
## Setup ## Setup
Put all the scripts in `/home/pi/` and make them executable with `chmod +x *.sh`.
Install speedtest-cli : `sudo apt install speedtest-cli`. 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`. Set up the pi to boot in auto 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`.
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 ## Usage
......
#!/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
#!/bin/sh #!/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 echo "STREAM BOX STARTING - Press [CTRL+C] to cancel..."
while [ $(cat /sys/class/net/wlan0/operstate) != "up" ]; do echo "Calculating target bitrate. Please wait..."
echo "Wifi is down. Waiting..." # Wait for the speedtest website to be reachable
sleep 5 until ping -c1 www.speedtest.net >/dev/null 2>&1; do : sleep 5; done
done
# Get the upload speed # Get the upload speed
UPLOAD_SPEED=$(speedtest --csv --no-download | cut -d ',' -f8 | cut -d '.' -f1) UPLOAD_SPEED=$(speedtest --csv --no-download | cut -d ',' -f8 | cut -d '.' -f1)
# Take half the speed for bitrate # Take half the speed for bitrate
BITRATE="$(($UPLOAD_SPEED / 2))" 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 "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 while true; do
#Pipe to ffmpeg
#Audio stream #Audio stream
#Video stream (from pipe) #Video stream
#Audio options #Audio options
#Video options #Video options
#Push to youtube #Push to streaming service
raspivid --nopreview --output - -t 0 \ ffmpeg \
-w 1280 -h 720 -fps 25 -b ${BITRATE} | -thread_queue_size ${THREAD_QUEUE_SIZE} -f alsa -i plughw:1,0 \
ffmpeg -re \ -thread_queue_size ${THREAD_QUEUE_SIZE} -f h264 -i /dev/video0 -r ${FRAMERATE} \
-thread_queue_size 65536 \ -ac 1 -acodec libmp3lame -ar 44100 -b:a 128k \
-f alsa -i plughw:1,0 \ -vcodec copy \
-thread_queue_size 65536 \ -g "$(($FRAMERATE * 2))" -strict experimental -f flv ${RTMP_SERVER}/${KEY}
-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
done done
#!/bin/sh
pkill start_stream.sh
pkill ffmpeg
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment