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.

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