Advertisement
Guest User

Untitled

a guest
Sep 15th, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. <?php
  2.  
  3. set_time_limit(0);
  4. date_default_timezone_set('UTC');
  5.  
  6. require __DIR__.'/../vendor/autoload.php';
  7.  
  8. /////// CONFIG ///////
  9. $username = '';
  10. $password = '';
  11. $debug = true;
  12. $truncatedDebug = false;
  13. //////////////////////
  14.  
  15. /////// MEDIA ////////
  16. $videoFilename = '';
  17. //////////////////////
  18.  
  19. $ig = new \InstagramAPI\Instagram($debug, $truncatedDebug);
  20.  
  21. try {
  22. $ig->login($username, $password);
  23. } catch (\Exception $e) {
  24. echo 'Something went wrong: '.$e->getMessage()."\n";
  25. exit(0);
  26. }
  27.  
  28. try {
  29. // NOTE: This code will create a broadcast, which will give us an RTMP url
  30. // where we are supposed to stream-upload the media we want to broadcast.
  31. //
  32. // The following code is using FFMPEG to broadcast, although other
  33. // alternatives are valid too, like OBS (Open Broadcaster Software,
  34. // https://obsproject.com).
  35. //
  36. // For more information on FFMPEG, see:
  37. // https://github.com/mgp25/Instagram-API/issues/1488#issuecomment-324271177
  38. // and for OBS, see:
  39. // https://github.com/mgp25/Instagram-API/issues/1488#issuecomment-333365636
  40.  
  41. // Get FFmpeg handler and ensure that the application exists on this system.
  42. // NOTE: You can supply custom path to the ffmpeg binary, or just leave NULL
  43. // to autodetect it.
  44. $ffmpegPath = null;
  45. $ffmpeg = \InstagramAPI\Media\Video\FFmpeg::factory($ffmpegPath);
  46.  
  47. // Tell Instagram that we want to perform a livestream.
  48. $stream = $ig->live->create();
  49. $broadcastId = $stream->getBroadcastId();
  50. $ig->live->start($broadcastId);
  51.  
  52. // Switch from RTMPS to RTMP upload URL, since RTMPS doesn't work well.
  53. $streamUploadUrl = preg_replace(
  54. '#^rtmps://([^/]+?):443/#ui',
  55. 'rtmp://\1:80/',
  56. $stream->getUploadUrl()
  57. );
  58.  
  59. // Broadcast the entire video file.
  60. // NOTE: The video is broadcasted asynchronously (in the background).
  61. $broadcastProcess = $ffmpeg->runAsync(sprintf(
  62. '-rtbufsize 256M -re -i %s -acodec libmp3lame -ar 44100 -b:a 128k -pix_fmt yuv420p -profile:v baseline -s 720x1280 -bufsize 6000k -vb 400k -maxrate 1500k -deinterlace -vcodec libx264 -preset veryfast -g 30 -r 30 -f flv %s',
  63. \Winbox\Args::escape($videoFilename),
  64. \Winbox\Args::escape($streamUploadUrl)
  65. ));
  66.  
  67. // The following loop performs important requests to obtain information
  68. // about the broadcast while it is ongoing.
  69. // NOTE: This is REQUIRED if you want the comments and likes to appear
  70. // in your saved post-live feed.
  71. // NOTE: These requests are sent *while* the video is being broadcasted.
  72. $lastCommentTs = 0;
  73. $lastLikeTs = 0;
  74. do {
  75. // Get broadcast comments.
  76. // - The latest comment timestamp will be required for the next
  77. // getComments() request.
  78. // - There are two types of comments: System comments and user comments.
  79. // We compare both and keep the newest (most recent) timestamp.
  80. $commentsResponse = $ig->live->getComments($broadcastId, $lastCommentTs);
  81. $systemComments = $commentsResponse->getSystemComments();
  82. $comments = $commentsResponse->getComments();
  83. if (!empty($systemComments)) {
  84. $lastCommentTs = end($systemComments)->getCreatedAt();
  85. }
  86. if (!empty($comments) && end($comments)->getCreatedAt() > $lastCommentTs) {
  87. $lastCommentTs = end($comments)->getCreatedAt();
  88. }
  89.  
  90. // Get broadcast heartbeat and viewer count.
  91. $ig->live->getHeartbeatAndViewerCount($broadcastId);
  92.  
  93. // Get broadcast like count.
  94. // - The latest like timestamp will be required for the next
  95. // getLikeCount() request.
  96. $likeCountResponse = $ig->live->getLikeCount($broadcastId, $lastLikeTs);
  97. $lastLikeTs = $likeCountResponse->getLikeTs();
  98.  
  99. sleep(2);
  100. } while ($broadcastProcess->isRunning());
  101.  
  102. // Get the final viewer list of the broadcast.
  103. // NOTE: You should only use this after the broadcast has stopped uploading.
  104. $ig->live->getFinalViewerList($broadcastId);
  105.  
  106. // End the broadcast stream.
  107. // NOTE: Instagram will ALSO end the stream if your broadcasting software
  108. // itself sends a RTMP signal to end the stream. FFmpeg doesn't do that
  109. // (without patching), but OBS sends such a packet. So be aware of that.
  110. $ig->live->end($broadcastId);
  111.  
  112. // Once the broadcast has ended, you can optionally add the finished
  113. // broadcast to your post-live feed (saved replay).
  114. $ig->live->addToPostLive($broadcastId);
  115. } catch (\Exception $e) {
  116. echo 'Something went wrong: '.$e->getMessage()."\n";
  117. }
  118. * Desktop version
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement