You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

128 lines
3.4 KiB

<?php
$globalData['ClientID'] = "";
$globalData['Channel'] = "";
$globalData['Folder'] = "./images/";
$globalData['DayString'] = date("Ydm");
if (isset($argv[1]))
$globalData['ClientID'] = @$argv[1];
if (isset($argv[2]))
$globalData['Channel'] = @$argv[2];
$globalData['sslContext'] = stream_context_create(array("ssl"=>array("verify_peer"=>false,"verify_peer_name"=>false)));
function getTokens($channelName)
{
global $globalData;
$url = "https://api.twitch.tv/api/channels/".$channelName."/access_token?client_id=".$globalData['ClientID'];
$json = json_decode(file_get_contents($url));
$json->token = json_decode($json->token);
return $json;
}
function getStreamInfo($channelName)
{
global $globalData;
$url = "https://api.twitch.tv/kraken/streams/".$channelName."?client_id=".$globalData['ClientID'];
$json = json_decode(file_get_contents($url, false, $globalData['sslContext']));
return $json;
}
function getPreviewURL($infos, $sizeX, $sizeY)
{
$url = @$infos->stream->preview->template;
$url = str_replace("{width}", $sizeX, $url);
$url = str_replace("{height}", $sizeY, $url);
return $url;
}
function getFileCountInFolder()
{
global $globalData;
if (!file_exists($globalData['Folder']))
mkdir($globalData['Folder'] , 0777, true);
if (!file_exists($globalData['Folder'].$globalData['DayString']."/"))
mkdir($globalData['Folder'].$globalData['DayString']."/", 0777, true);
$files = array();
if ($handle = opendir($globalData['Folder'].$globalData['DayString']."/")) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..")
{
if(getFileExtention($entry) != "mp4")
{
array_push($files, $entry);
}
}
}
closedir($handle);
}
return count($files);
}
function getFileExtention($uri)
{
$parts = explode(".", $uri);
return $parts[(count($parts)-1)];
}
function getFullNumberString($number)
{
while(strlen($number) <= 6)
{
$number = "0".$number;
}
return $number;
}
$imageDataHash = "";
while(true)
{
$globalData['DayString'] = date("Ydm");
$streamInfo = getStreamInfo($globalData['Channel']);
$previewURL = getPreviewURL($streamInfo, "1920", "1080");
if($previewURL == "")
{
print_r($globalData);
print_r($argv);
print_r($streamInfo);
echo "URL == ".$previewURL."\n";
die("fatal error");
}
$currentDayString =
$imageFileName = $globalData['Folder'].$globalData['DayString']."/".getFullNumberString((getFileCountInFolder() + 1)).".".getFileExtention($previewURL);
echo "Copy preview url '".$previewURL."' to '".$imageFileName."' ...";
$imageRawData = file_get_contents($previewURL, false, $globalData['sslContext']);
if(md5($imageRawData) != $imageDataHash)
{
$imageDataHash = md5($imageRawData);
file_put_contents($imageFileName, $imageRawData);
echo "done\n";
}else{
echo "canceled\n";
}
$imageRawData = "";
system("ffmpeg -f image2 -framerate 3 -i /data/images/".$globalData['DayString']."/%07d.jpg -r 25 -vcodec libx264 -y /data/images/".$globalData['DayString'].".mp4");
sleep(180);
}
?>