#!/bin/bash

# Rechte zum Ausführen des Scrits mit sudo chmod a+x streamfull-instagram.sh
# Man kann diese Script mit :
# sudo nohup streamfull-instagram.sh &
# starten, muss aber nicht.
# Da instagramm Livestream max. 4 Stunden sind
#
# Dieses Script eignet sich auch nach Twitch und oder Youtube
#
# Instagram settings
STREAMKEY="hier kommt ihr temoprärer Stream Key von INstagram rein"
#
# Kontrolle ob die Stream Adresse zu Instagram stimmt
ENDPOINTS=(rtmps://edgetee-upload-dus1-1.xx.fbcdn.net:443/rtmp/)

I=0
while true
do

    # get the current endpoint
    EPIDX=$(( $I % ${#ENDPOINTS[@]} ))
    CURENDPOINT=${ENDPOINTS[$EPIDX]}

    echo "Using $CURENDPOINT as twitch endpoint"


# livestream nach Instagram
# HIer kann man alles für sich selbst anpassen
#Wichtig Uebertragung an Instagramm 720x1280 nicht 1280x720
ffmpeg -i https://localhost.m3u8 -video_size 720x1280 -vcodec libx264 -pix_fmt yuv420p  -c:a copy  -preset ultrafast -strict experimental -use_wallclock_as_timestamps 1 -b:v 1M -maxrate 1.5M -bufsize 3M -metadata title="Beispiel Live aus ...." -metadata copyright="kann man machen, muss aber nicht" -f flv $CURENDPOINT/$STREAMKEY


    # cooldown when stream was aborted
    echo
    echo "Stream exited unexpected. Wait 60 seconds and restart..."
    echo

    sleep 60

    # jump to next endpoint
    I=$(( $I + 1 ))

done
