Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- #usage: sbs_cmd.php search_text=<id> stream=<n>
- # id is the number from the SBS website video URL, eg:
- #
- # https://www.sbs.com.au/ondemand/video/1160879171989/show
- #
- # n is the stream index from the list printed, default is highest resolution.
- #
- # Run the ffmpeg command line shown.
- # Version 1.00 - inital working release as a command line variant
- # Version 1.01 - modified to work with more SBS shows by better matching the "html:link" URL
- # Version 1.03 - strip out any offending characters from title & write to tmp files
- # Version 1.04 - change output filename string (written to file) to ".mp4" instead of ".ts"
- # Version 1.05 - add ouput of URL to download subtitles
- # Version 1.06 - fix for downloads that didn't include any video stream
- # Version 1.07 - remove offending newline from URL download string
- # Version 1.08 - change HTML defined characters ''' and '&' to an apostrophe and an ampersand
- # Version 1.09 - modify $init_url to reflect SBS server change
- # Version 1.50 - update for SBS server changes.
- # Version 1.51 - fix initial match to be non-greedy.
- # Version 1.52 - option for selecting stream, output subtitle command.
- function curl_get($url, array $get = NULL, array $options = array())
- /**
- * Send a GET requst using cURL
- * @param string $url to request
- * @param array $get values to send
- * @param array $options for cURL
- * @return string
- */
- {
- $defaults = array(
- CURLOPT_URL => $url. (strpos($url, '?') === FALSE ? '?' : ''),
- CURLOPT_HEADER => 0,
- CURLOPT_RETURNTRANSFER => TRUE,
- # Might need to uncomment below two lines on some Windows PHP installs
- # Not good for security, but if it works, "Meh" it's only SBS downloads
- #CURLOPT_SSL_VERIFYHOST => 0,
- #CURLOPT_SSL_VERIFYPEER => 0,
- CURLOPT_TIMEOUT => 4
- );
- $ch = curl_init();
- curl_setopt_array($ch, ($options + $defaults));
- if( ! $result = curl_exec($ch))
- { trigger_error(curl_error($ch)); }
- curl_close($ch);
- return $result;
- }
- function parse_m3u($m3u)
- {
- $streams = array();
- $next_is_url = false;
- $line = strtok($m3u, "\r\n");
- while( $line !== FALSE )
- {
- if( $next_is_url )
- {
- if( preg_match("/http.*?index_[0-9]+_av.*/", $line, $stream_url) )
- {
- $key = "st_" . str_pad($resolution_array[1], 4, "0", STR_PAD_LEFT) . str_pad($bandwidth_array[1], 10, "0", STR_PAD_LEFT);
- $streams[$key] = array($bandwidth_array[1], $resolution_array[1], $resolution_array[2], rtrim($stream_url[0]));
- }
- $next_is_url = false;
- }
- else if( preg_match("/#EXT-X-STREAM-INF/", $line) )
- {
- if( preg_match("/BANDWIDTH=([0-9]+)/", $line, $bandwidth_array)
- and preg_match("/RESOLUTION=([0-9]+)x([0-9]+)/", $line, $resolution_array) )
- {
- $next_is_url = true;
- }
- }
- $line = strtok("\r\n");
- }
- strtok("", "");
- rsort($streams);
- return $streams;
- }
- parse_str(implode('&', array_slice($argv, 1)), $_POST);
- $st = isset($_POST['search_text']) ? $_POST['search_text'] : false;
- $stream = isset($_POST['stream']) ? $_POST['stream'] : 0;
- $init_url = "www.sbs.com.au/api/video_pdkvars/id/" . $st . "?form=json HTTP/1.1";
- $json_data = curl_get($init_url);
- preg_match("/\"html\":\"http.{5}link\.theplatform\.com.*?=m3u/", $json_data, $matches);
- $smil_url = str_replace("\/", "/", $matches[0]);
- $smil_url = str_replace('"html":"', "", $smil_url);
- $smil_data = curl_get($smil_url);
- preg_match("/http:\/\/videocdn\.sbs\.com\.au\/u\/video\/SBS\/managed\/closedcaptions.*?.SRT/", $smil_data, $subs_matches);
- preg_match("/https?:\/\/(sbsvodns|sbsvodco-vh).*?=off/", $smil_data, $m3u_url);
- preg_match('/title="([^"]+)"/', $smil_data, $title);
- $m3u_data = curl_get($m3u_url[0]);
- $av_urls = parse_m3u($m3u_data);
- if( $stream < 0 or $stream >= count($av_urls) )
- {
- $stream = 0;
- }
- $ffmpeg_url="";
- $url_idx=0;
- foreach($av_urls as $url)
- {
- echo $url_idx; $url[1]; echo ": "; echo $url[1]; echo "x"; echo $url[2]; echo " bandwidth "; echo $url[0];
- if( $stream == $url_idx )
- {
- $ffmpeg_url = rtrim($url[3]);
- echo " < selected";
- }
- echo "\n";
- $url_idx++;
- }
- if( count($subs_matches) == 0 )
- {
- $ffsubs_url="";
- }
- else
- {
- $ffsubs_url=$subs_matches[0];
- }
- $fftitle = str_replace("title=","", $title[0]);
- $bad_chars = array("\\", "/", ":", "*", "?", "\"", "<", ">", "|", "'");
- $bad_amp = array("&");
- $bad_apos = array("'");
- $fftitle = str_replace($bad_chars, "", $fftitle);
- $fftitle = str_replace($bad_amp, "&", $fftitle);
- $fftitle = str_replace($bad_apos, "'", $fftitle);
- echo "The command string to download via ffmpeg is: \n\n";
- echo 'ffmpeg -i "'; echo $ffmpeg_url; echo '" -c copy "'; echo $fftitle; echo '.ts"';
- echo "\n\n";
- if( !empty($ffsubs_url) )
- {
- echo "Subtitles: \n\n";
- echo 'ffmpeg -i "' ; echo $ffsubs_url; echo '" "'; echo $fftitle; echo '.srt"';
- echo "\n\n";
- }
- $fd_u = fopen("sbsurl.tmp","w"); fwrite($fd_u,"\"$ffmpeg_url\""); fclose($fd_u);
- $fd_v = fopen("sbsvideo.tmp","w"); fwrite($fd_v,"\"$fftitle.mp4\""); fclose($fd_v);
- $fd_su = fopen("sbsurlsubs.tmp","w"); fwrite($fd_su,"\"$ffsubs_url\""); fclose($fd_su);
- $fd_sv = fopen("sbsvideosubs.tmp","w"); fwrite($fd_sv,"\"$fftitle.srt\""); fclose($fd_sv);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment