Guest User

sbs-v1.52.php

a guest
Mar 23rd, 2018
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.31 KB | None | 0 0
  1. <?php
  2.  
  3. #usage: sbs_cmd.php search_text=<id> stream=<n>
  4.  
  5. # id is the number from the SBS website video URL, eg:
  6. #
  7. #  https://www.sbs.com.au/ondemand/video/1160879171989/show
  8. #
  9. # n is the stream index from the list printed, default is highest resolution.
  10. #
  11. # Run the ffmpeg command line shown.
  12.  
  13. # Version 1.00 - inital working release as a command line variant
  14. # Version 1.01 - modified to work with more SBS shows by better matching the "html:link" URL
  15. # Version 1.03 - strip out any offending characters from title & write to tmp files
  16. # Version 1.04 - change output filename string (written to file) to ".mp4" instead of ".ts"
  17. # Version 1.05 - add ouput of URL to download subtitles
  18. # Version 1.06 - fix for downloads that didn't include any video stream
  19. # Version 1.07 - remove offending newline from URL download string
  20. # Version 1.08 - change HTML defined characters '&apos;' and '&amp;' to an apostrophe and an ampersand
  21. # Version 1.09 - modify $init_url to reflect SBS server change
  22.  
  23. # Version 1.50 - update for SBS server changes.
  24. # Version 1.51 - fix initial match to be non-greedy.
  25. # Version 1.52 - option for selecting stream, output subtitle command.
  26.  
  27. function curl_get($url, array $get = NULL, array $options = array())
  28. /**
  29. * Send a GET requst using cURL
  30. * @param string $url to request
  31. * @param array $get values to send
  32. * @param array $options for cURL
  33. * @return string
  34. */
  35. {
  36.     $defaults = array(
  37.         CURLOPT_URL => $url. (strpos($url, '?') === FALSE ? '?' : ''),
  38.         CURLOPT_HEADER => 0,
  39.         CURLOPT_RETURNTRANSFER => TRUE,
  40. # Might need to uncomment below two lines on some Windows PHP installs
  41. # Not good for security, but if it works, "Meh" it's only SBS downloads
  42. #CURLOPT_SSL_VERIFYHOST => 0,
  43. #CURLOPT_SSL_VERIFYPEER => 0,
  44.        CURLOPT_TIMEOUT => 4
  45.     );
  46.  
  47.     $ch = curl_init();
  48.     curl_setopt_array($ch, ($options + $defaults));
  49.     if( ! $result = curl_exec($ch))
  50.       { trigger_error(curl_error($ch)); }
  51.     curl_close($ch);
  52.     return $result;
  53. }
  54.  
  55. function parse_m3u($m3u)
  56. {
  57.   $streams = array();
  58.   $next_is_url = false;
  59.  
  60.   $line = strtok($m3u, "\r\n");
  61.   while( $line !== FALSE )
  62.   {
  63.       if( $next_is_url )
  64.       {
  65.           if( preg_match("/http.*?index_[0-9]+_av.*/", $line, $stream_url) )
  66.           {
  67.             $key = "st_" . str_pad($resolution_array[1], 4, "0", STR_PAD_LEFT) . str_pad($bandwidth_array[1], 10, "0", STR_PAD_LEFT);
  68.             $streams[$key] = array($bandwidth_array[1], $resolution_array[1], $resolution_array[2], rtrim($stream_url[0]));
  69.           }
  70.  
  71.           $next_is_url = false;
  72.       }
  73.       else if( preg_match("/#EXT-X-STREAM-INF/", $line) )
  74.       {
  75.           if( preg_match("/BANDWIDTH=([0-9]+)/", $line, $bandwidth_array)
  76.             and preg_match("/RESOLUTION=([0-9]+)x([0-9]+)/", $line, $resolution_array) )
  77.           {
  78.             $next_is_url = true;
  79.           }
  80.       }
  81.  
  82.       $line = strtok("\r\n");
  83.   }
  84.  
  85.   strtok("", "");
  86.  
  87.   rsort($streams);
  88.  
  89.   return $streams;
  90. }
  91.  
  92. parse_str(implode('&', array_slice($argv, 1)), $_POST);
  93. $st = isset($_POST['search_text']) ? $_POST['search_text'] : false;
  94. $stream = isset($_POST['stream']) ? $_POST['stream'] : 0;
  95.  
  96. $init_url = "www.sbs.com.au/api/video_pdkvars/id/" . $st . "?form=json HTTP/1.1";
  97. $json_data = curl_get($init_url);
  98. preg_match("/\"html\":\"http.{5}link\.theplatform\.com.*?=m3u/", $json_data, $matches);
  99.  
  100. $smil_url = str_replace("\/", "/", $matches[0]);
  101. $smil_url = str_replace('"html":"', "", $smil_url);
  102. $smil_data = curl_get($smil_url);
  103.  
  104. preg_match("/http:\/\/videocdn\.sbs\.com\.au\/u\/video\/SBS\/managed\/closedcaptions.*?.SRT/", $smil_data, $subs_matches);
  105. preg_match("/https?:\/\/(sbsvodns|sbsvodco-vh).*?=off/", $smil_data, $m3u_url);
  106. preg_match('/title="([^"]+)"/', $smil_data, $title);
  107.  
  108. $m3u_data = curl_get($m3u_url[0]);
  109.  
  110. $av_urls = parse_m3u($m3u_data);
  111.  
  112. if( $stream < 0 or $stream >= count($av_urls) )
  113. {
  114.     $stream = 0;
  115. }
  116.  
  117. $ffmpeg_url="";
  118. $url_idx=0;
  119. foreach($av_urls as $url)
  120. {
  121.     echo $url_idx; $url[1]; echo ": "; echo $url[1]; echo "x"; echo $url[2]; echo " bandwidth "; echo $url[0];
  122.     if( $stream == $url_idx )
  123.     {
  124.         $ffmpeg_url = rtrim($url[3]);
  125.         echo " < selected";
  126.     }
  127.     echo "\n";
  128.  
  129.     $url_idx++;
  130. }
  131.  
  132. if( count($subs_matches) == 0 )
  133. {
  134.     $ffsubs_url="";
  135. }
  136. else
  137. {
  138.     $ffsubs_url=$subs_matches[0];
  139. }
  140.  
  141. $fftitle = str_replace("title=","", $title[0]);
  142. $bad_chars = array("\\", "/", ":", "*", "?", "\"", "<", ">", "|", "'");
  143. $bad_amp = array("&amp;");
  144. $bad_apos = array("&apos;");
  145. $fftitle = str_replace($bad_chars, "", $fftitle);
  146. $fftitle = str_replace($bad_amp, "&", $fftitle);
  147. $fftitle = str_replace($bad_apos, "'", $fftitle);
  148.  
  149. echo "The command string to download via ffmpeg is: \n\n";
  150. echo 'ffmpeg -i "'; echo $ffmpeg_url; echo '" -c copy "'; echo $fftitle; echo '.ts"';
  151. echo "\n\n";
  152. if( !empty($ffsubs_url) )
  153. {
  154.     echo "Subtitles: \n\n";
  155.     echo 'ffmpeg -i "' ; echo $ffsubs_url; echo '" "'; echo $fftitle; echo '.srt"';
  156.     echo "\n\n";
  157. }
  158.  
  159. $fd_u = fopen("sbsurl.tmp","w"); fwrite($fd_u,"\"$ffmpeg_url\""); fclose($fd_u);
  160. $fd_v = fopen("sbsvideo.tmp","w"); fwrite($fd_v,"\"$fftitle.mp4\""); fclose($fd_v);
  161. $fd_su = fopen("sbsurlsubs.tmp","w"); fwrite($fd_su,"\"$ffsubs_url\""); fclose($fd_su);
  162. $fd_sv = fopen("sbsvideosubs.tmp","w"); fwrite($fd_sv,"\"$fftitle.srt\""); fclose($fd_sv);
  163. ?>
Advertisement
Add Comment
Please, Sign In to add comment