2019-03-21 00:13:55 +00:00
|
|
|
<?php
|
|
|
|
$globalData['ClientID'] = "";
|
|
|
|
$globalData['Channel'] = "";
|
|
|
|
$globalData['Folder'] = "./images/";
|
2019-03-22 20:52:31 +00:00
|
|
|
$globalData['DayString'] = date("Ydm");
|
2019-03-21 00:13:55 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-03-22 20:52:31 +00:00
|
|
|
if (!file_exists($globalData['Folder'].$globalData['DayString']."/"))
|
|
|
|
mkdir($globalData['Folder'].$globalData['DayString']."/", 0777, true);
|
|
|
|
|
2019-03-21 00:13:55 +00:00
|
|
|
$files = array();
|
|
|
|
|
2019-03-22 20:52:31 +00:00
|
|
|
if ($handle = opendir($globalData['Folder'].$globalData['DayString']."/")) {
|
2019-03-21 00:13:55 +00:00
|
|
|
while (false !== ($entry = readdir($handle))) {
|
|
|
|
if ($entry != "." && $entry != "..")
|
|
|
|
{
|
2019-03-21 22:43:24 +00:00
|
|
|
if(getFileExtention($entry) != "mp4")
|
|
|
|
{
|
|
|
|
array_push($files, $entry);
|
|
|
|
}
|
2019-03-21 00:13:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir($handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count($files);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFileExtention($uri)
|
|
|
|
{
|
|
|
|
$parts = explode(".", $uri);
|
|
|
|
return $parts[(count($parts)-1)];
|
|
|
|
}
|
|
|
|
|
2019-03-21 00:21:27 +00:00
|
|
|
function getFullNumberString($number)
|
|
|
|
{
|
|
|
|
while(strlen($number) <= 6)
|
|
|
|
{
|
|
|
|
$number = "0".$number;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $number;
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:42:01 +00:00
|
|
|
$imageDataHash = "";
|
|
|
|
|
2019-03-21 00:13:55 +00:00
|
|
|
while(true)
|
|
|
|
{
|
2019-03-23 11:57:28 +00:00
|
|
|
$globalData['DayString'] = date("Ydm");
|
|
|
|
|
2019-03-22 20:42:01 +00:00
|
|
|
$streamInfo = getStreamInfo($globalData['Channel']);
|
|
|
|
$previewURL = getPreviewURL($streamInfo, "1920", "1080");
|
|
|
|
|
2019-03-21 00:13:55 +00:00
|
|
|
if($previewURL == "")
|
|
|
|
{
|
|
|
|
print_r($globalData);
|
|
|
|
print_r($argv);
|
|
|
|
print_r($streamInfo);
|
|
|
|
echo "URL == ".$previewURL."\n";
|
|
|
|
die("fatal error");
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:52:31 +00:00
|
|
|
|
|
|
|
$currentDayString =
|
|
|
|
$imageFileName = $globalData['Folder'].$globalData['DayString']."/".getFullNumberString((getFileCountInFolder() + 1)).".".getFileExtention($previewURL);
|
2019-03-21 00:13:55 +00:00
|
|
|
|
|
|
|
echo "Copy preview url '".$previewURL."' to '".$imageFileName."' ...";
|
2019-03-22 20:42:01 +00:00
|
|
|
|
|
|
|
$imageRawData = file_get_contents($previewURL, false, $globalData['sslContext']);
|
|
|
|
|
|
|
|
if(md5($imageRawData) != $imageDataHash)
|
|
|
|
{
|
2019-03-22 20:46:14 +00:00
|
|
|
$imageDataHash = md5($imageRawData);
|
2019-03-22 20:42:01 +00:00
|
|
|
file_put_contents($imageFileName, $imageRawData);
|
|
|
|
echo "done\n";
|
|
|
|
}else{
|
|
|
|
echo "canceled\n";
|
|
|
|
}
|
2019-03-21 00:13:55 +00:00
|
|
|
|
2019-03-22 20:41:37 +00:00
|
|
|
$imageRawData = "";
|
2019-03-22 20:52:31 +00:00
|
|
|
system("ffmpeg -f image2 -framerate 3 -i /data/images/".$globalData['DayString']."/%07d.jpg -r 25 -vcodec libx264 -y /data/images/".$globalData['DayString'].".mp4");
|
2019-03-22 20:41:37 +00:00
|
|
|
sleep(180);
|
2019-03-21 00:13:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|