Skip to content

Instantly share code, notes, and snippets.

@feuvan
Created November 30, 2011 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save feuvan/1409747 to your computer and use it in GitHub Desktop.
Save feuvan/1409747 to your computer and use it in GitHub Desktop.
Grab (implicitly assume recent as x264/aac) iask share video and convert it to mp4.
<?php
define('FFMPEG', '/usr/bin/ffmpeg');
define('WGET', '/usr/bin/wget');
set_time_limit(3600); /* we are working with small chunks of files */
ignore_user_abort(true); /* do not terminate script execution if disconnect */
@ini_set("output_buffering", 0);
@ini_set('implicit_flush', 1);
header("Connection: close");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
$vid = (int)$_GET['vid'];
$num = (int)$_GET['num'];
if ($num == 0) $num =1;
$response = file_get_contents("http://v.iask.com/v_play.php?vid=". $vid);
if ($response === FALSE) {
print "failed to read upstream json";
exit;
}
/* http://code.google.com/p/cnvideoapi/source/browse/trunk/VideoApi/Adapter/Sina.php?r=trunk */
/* to make simplexml happy */
$response = preg_replace("/\<\!\[CDATA\[(.*?)\]\]\>/ies", "base64_encode('$1')", $response);
$info = simplexml_load_string($response);
if ($info === NULL) {
print "can't decode upstream data";
exit;
}
$count = count($info->durl);
if ($num > $count) {
print "incorrect num ($num), exceeds video segment count ($count)";
exit;
}
$url = urldecode(urlencode(base64_decode($info->durl[$num-1]->url)));
$dest = "vid".$vid."_".$num.".mp4";
if (file_exists($dest)) {
print $dest;
} else {
// for sina iask, we just copy streams
//
$cmd = WGET ." \"". $url ."\" -O - |".FFMPEG ." -i - -vcodec copy -acodec copy ". $dest . " 2>&1";
print ("going to execute ".$cmd);
passthru($cmd);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment