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.

126 lines
3.3 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <?php
  2. $globalData['ClientID'] = "";
  3. $globalData['Channel'] = "";
  4. $globalData['Folder'] = "./images/";
  5. $globalData['DayString'] = date("Ydm");
  6. if (isset($argv[1]))
  7. $globalData['ClientID'] = @$argv[1];
  8. if (isset($argv[2]))
  9. $globalData['Channel'] = @$argv[2];
  10. $globalData['sslContext'] = stream_context_create(array("ssl"=>array("verify_peer"=>false,"verify_peer_name"=>false)));
  11. function getTokens($channelName)
  12. {
  13. global $globalData;
  14. $url = "https://api.twitch.tv/api/channels/".$channelName."/access_token?client_id=".$globalData['ClientID'];
  15. $json = json_decode(file_get_contents($url));
  16. $json->token = json_decode($json->token);
  17. return $json;
  18. }
  19. function getStreamInfo($channelName)
  20. {
  21. global $globalData;
  22. $url = "https://api.twitch.tv/kraken/streams/".$channelName."?client_id=".$globalData['ClientID'];
  23. $json = json_decode(file_get_contents($url, false, $globalData['sslContext']));
  24. return $json;
  25. }
  26. function getPreviewURL($infos, $sizeX, $sizeY)
  27. {
  28. $url = @$infos->stream->preview->template;
  29. $url = str_replace("{width}", $sizeX, $url);
  30. $url = str_replace("{height}", $sizeY, $url);
  31. return $url;
  32. }
  33. function getFileCountInFolder()
  34. {
  35. global $globalData;
  36. if (!file_exists($globalData['Folder']))
  37. mkdir($globalData['Folder'] , 0777, true);
  38. if (!file_exists($globalData['Folder'].$globalData['DayString']."/"))
  39. mkdir($globalData['Folder'].$globalData['DayString']."/", 0777, true);
  40. $files = array();
  41. if ($handle = opendir($globalData['Folder'].$globalData['DayString']."/")) {
  42. while (false !== ($entry = readdir($handle))) {
  43. if ($entry != "." && $entry != "..")
  44. {
  45. if(getFileExtention($entry) != "mp4")
  46. {
  47. array_push($files, $entry);
  48. }
  49. }
  50. }
  51. closedir($handle);
  52. }
  53. return count($files);
  54. }
  55. function getFileExtention($uri)
  56. {
  57. $parts = explode(".", $uri);
  58. return $parts[(count($parts)-1)];
  59. }
  60. function getFullNumberString($number)
  61. {
  62. while(strlen($number) <= 6)
  63. {
  64. $number = "0".$number;
  65. }
  66. return $number;
  67. }
  68. $imageDataHash = "";
  69. while(true)
  70. {
  71. $streamInfo = getStreamInfo($globalData['Channel']);
  72. $previewURL = getPreviewURL($streamInfo, "1920", "1080");
  73. if($previewURL == "")
  74. {
  75. print_r($globalData);
  76. print_r($argv);
  77. print_r($streamInfo);
  78. echo "URL == ".$previewURL."\n";
  79. die("fatal error");
  80. }
  81. $currentDayString =
  82. $imageFileName = $globalData['Folder'].$globalData['DayString']."/".getFullNumberString((getFileCountInFolder() + 1)).".".getFileExtention($previewURL);
  83. echo "Copy preview url '".$previewURL."' to '".$imageFileName."' ...";
  84. $imageRawData = file_get_contents($previewURL, false, $globalData['sslContext']);
  85. if(md5($imageRawData) != $imageDataHash)
  86. {
  87. $imageDataHash = md5($imageRawData);
  88. file_put_contents($imageFileName, $imageRawData);
  89. echo "done\n";
  90. }else{
  91. echo "canceled\n";
  92. }
  93. $imageRawData = "";
  94. system("ffmpeg -f image2 -framerate 3 -i /data/images/".$globalData['DayString']."/%07d.jpg -r 25 -vcodec libx264 -y /data/images/".$globalData['DayString'].".mp4");
  95. sleep(180);
  96. }
  97. ?>