<?php
include "config.php";

$lastOnlineTime   =   time();

while(TRUE)
{
    if(($lastOnlineTime + 500) < time())
    {
        $fileName = "output-".time().".jpg";

        system("ffmpeg -i ".$StreamURL." -vframes 1 -q:v 2 -vf scale=535:346 ".$fileName);   

        if(is_file($fileName))
        {
            $lastOnlineTime = time();

            copy($fileName, "tmp/rtmp/".$fileName);
            unlink($fileName);

            $jsonMessage = json_decode(file_get_contents("message.json"));

            $jsonMessage->embeds->description = str_replace("%%adresse%%", $StreamURL, $jsonMessage->embeds->description);
            $jsonMessage->embeds->image = "https://files.clatza.dev/tmp/rtmp/".$fileName;

            $options = [
                'http' => [
                    'method' => 'POST',
                    'header' => 'Content-Type: application/json',
                    'content' => json_encode($jsonMessage)
                ]
            ];

            $result = file_get_contents($DiscordWebhook, false, stream_context_create($options));
        }
    }else{
        sleep(5);
    }
}

?>