powered by nequal
Home » Wozozo_WWW_YouTube » Timeline » 2281

Changeset 2281 -- 2010-12-19 22:59:08

Author
させざき
Comment
support currently format and using format on ZF command

Diffs

Wozozo_WWW_YouTube/trunk/src/Wozozo/WWW/YouTube.php

@@ -14,7 +14,7 @@
/**
* @var array
*/
-    protected $_config = array('fmt' => 18,
+    protected $_config = array('prefer_fmt' => null,
'save' => 'GETCWD', //'GETCWD' will use getcwd();
'request_video_stream' => true, //output stream
'response_video_cleanup' => true
@@ -103,7 +103,7 @@
public function requestVideo(Wozozo_WWW_YouTube_VideoInfo $videoInfo)
{
// retrive url & save-file-path
-        $url = $videoInfo->makeDownloadUrl($this->_config['fmt']);
+        $url = $videoInfo->makeDownloadUrl($this->_config['prefer_fmt']);
$client = $this->getHttpClient();
$client->setUri($url);
@@ -158,7 +158,7 @@
public function suggestSavePath($videoInfo)
{
$dir = $this->_config['save'];
-        $fmt = $this->_config['fmt'];
+        $fmt = $this->_config['prefer_fmt'];
if ('GETCWD' ===  $dir) {
$dir = getcwd();
} else {
@@ -166,11 +166,11 @@
throw new InvalidArgumentException('Invalid dir'.$dir);
}
}
-        $path = $dir . DIRECTORY_SEPARATOR . $videoInfo['video_id'] . self::detectSuffix($fmt);
+        $path = $dir . DIRECTORY_SEPARATOR . $videoInfo['video_id'] . self::detectSuffix($videoInfo->detectFmt($fmt));
return $path;
}
-
+
/**
* borrowed from WWW::YouTube::Download
*
@@ -186,10 +186,13 @@
case '18' :
case '22' :
case '37' :
+            case '38' :
return '.mp4';
-            case '13' :
case '17' :
return '.3gp';
+            case '43':
+            case '45':
+                return '.vp8';
default :
return '.flv';
}
@@ -222,16 +225,5 @@
return false;
}
-
-    /*
-    public function __destruct()
-    {
-        if (is_string($s = $this->_config['output_stream']) && $this->_config['download_response_cleanup']) {
-            if (file_exists($s)) {
-                unlink($s);
-            }
-        }
-    }
-    */
}

Wozozo_WWW_YouTube/trunk/src/Wozozo/WWW/YouTube/VideoInfo.php

@@ -3,8 +3,10 @@
class Wozozo_WWW_YouTube_VideoInfo implements ArrayAccess
{
+    const FORMAT_FLV = 'flv';
const FORMAT_MP4 = 'mp4';
-    const FORMAT_FLV = 'flv';
+    const FORMAT_3GP = '3gp';
+    const FORMAT_VP8 = 'vp8';
private $_parsedVideoInfo;
@@ -29,37 +31,57 @@
}
// get detected high quality fmt
-        //$fmt = (string) $this->_highFmt($fmt);
-
-        //$fmt = $this->_highFmt();
+        $fmt = $this->detectFmt($preferFmt);
-        //$videoId = $this->_parsedVideoInfo['video_id'];
-        //$token = $this->_parsedVideoInfo['token'];
-        //return sprintf(Wozozo_WWW_YouTube::PATH_DOWNLOAD, $videoId, $token, $fmt);
$fmtUrlMap = $this->parseFmtUrlMap();
-        return current($fmtUrlMap);
+        return $fmtUrlMap[$fmt];
}
-    protected function _highFmt($preferFmt = null)
+    /**
+     * detect format int (High)
+     *
+     * @see http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs
+     *
+     * @param int|string|null
+     * @return int
+     */
+    public function detectFmt($preferFmt = null)
{
-        if (is_integer($fmt)) {
-            return $fmt;
+        if (is_integer($preferFmt)) {
+            return $preferFmt;
}
-        //$fmts  = $this->getFmts();
$fmts = $this->parseFmtUrlMap();
-        return current($fmts);
+        if (null === $preferFmt) {
+            return key($fmts); //@todo calcurate high fmt
+        }
-        $mp4s = array(37, 22, 18, 17, 13);
-        if (self::FORMAT_MP4 === $fmt) {
-            $intersect = array_intersect($fmts, $mp4s);
-            return current($intersect);
-        } else {
-            $diff = array_diff($fmts, $mp4s);
-            return current($diff);
+        $keys = array_keys($fmts);
+
+        switch (strtolower($preferFmt)) {
+            case self::FORMAT_MP4:
+                $fmt = current(array_intersect($keys, array(38, 37, 22, 18)));
+                break;
+            case self::FORMAT_VP8:
+                $fmt = current(array_intersect($keys, array(45, 43)));
+                break;
+            case self::FORMAT_FLV:
+                $fmt = current(array_intersect($keys, array(35, 34, 5)));
+                break;
+            case self::FORMAT_VP8:
+                $fmt = current(array_intersect($keys, array(17)));
+                break;
+            default:
+                throw new Exception('unknown format');
}
+
+        if (false === $fmt) {
+            throw new Exception("couldn't find format");
+        }
+
+        return $fmt;
}
/**
@@ -69,9 +91,6 @@
*  fmt_list or fmt_map
*  eg. 22/2000000/9/0/115,35/640000/9/0/115,34/0/9/0/115,5/0/7/0/0"
*
-     * @see http://kenz0.s201.xrea.com/weblog/2008/11/youtube_9.html
-     * @see http://creazy.net/2008/12/youtube_video_format_list.html
-     *
* @return array
*/
public function getFmts()

Wozozo_WWW_YouTube/trunk/src/Wozozo/WWW/YouTube/Tool/YoutubeProvider.php

@@ -20,17 +20,18 @@
$this->_out($url);
}
-    public function download($id, $path = 'GETCWD')
+    public function download($id, $path = 'GETCWD', $format = 'DETECT')
{
-        $this->_download($id, $path, false);
+        $format = ('DETECT' === $format) ? null : $format;
+        $this->_download($id, $format, $path, false);
}
public function downloadCouchdb($id)
{
-        $this->_download($id, null, true);
+        $this->_download($id, $format, null, true);
}
-    private function _setupSave($videoInfo, $path, $couch = false)
+    private function _setupSave($videoInfo, $path = null, $couch = false)
{
if ($couch) {
$config = $this->_loadConfig('couchdb');
@@ -55,12 +56,14 @@
}
}
-    protected function _download($id, $save, $couch = false)
+    //@todo format
+    protected function _download($id, $format, $save, $couch = false)
{
$videoId = Wozozo_WWW_YouTube::detectVideoId($id);
$this->_out("Video ID :$videoId");
$youtube = $this->_loadYoutube();
+        $youtube->setConfig(array('prefer_fmt' => $format));
$videoInfo = $youtube->getVideoInfo($videoId);
$this->_out("Status :". $videoInfo['status']);