Skip to content

Instantly share code, notes, and snippets.

@feuvan
Created November 30, 2011 16:43

Revisions

  1. feuvan revised this gist Nov 30, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions transcode.php
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@
    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 upsteam json";
    print "failed to read upstream json";
    exit;
    }

    @@ -26,7 +26,7 @@

    $info = simplexml_load_string($response);
    if ($info === NULL) {
    print "can't decode upsteam data";
    print "can't decode upstream data";
    exit;
    }

  2. feuvan created this gist Nov 30, 2011.
    52 changes: 52 additions & 0 deletions transcode.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    <?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 upsteam 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 upsteam 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);
    }

    ?>