Skip to content

Instantly share code, notes, and snippets.

@cwchiu
Created March 5, 2015 09:27
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 cwchiu/f25491193524f7a40b19 to your computer and use it in GitHub Desktop.
Save cwchiu/f25491193524f7a40b19 to your computer and use it in GitHub Desktop.
抓取 YouTube 影片下載網址
<?php
/***************************************************************************************************
* 功用:抓取 YouTube 影片下載網址
*
* 用法:在 console 模式下執行
* php-cgi -q getvideo.php "http://www.youtube.com/watch?v=ReUXNUy6w98&mode=related&search="
*
* 開發環境:
* [1] PHP5 for Windows
* [2] Windows XP Professional
* [3] Editplus 2.12
*
* 設計者:Chui-Wen Chiu(Arick)
*
* 開發日誌:
* 2006/06/24 建立
*
* TODO:
* 1. 支援批次
* 2. 呼叫外部程式(ex:flashget)進行下載
*
* 參考資料:
* [1] http://vbb3.twftp.org/showthread.php?t=7625
*
***************************************************************************************************/
set_time_limit(0);
//define('DEBUG', 1);
function debug($data, $isArray = false){
if(defined('DEBUG')){
$Crlf = defined('HTML')?'<br/>':"n";
if ($isArray){
print_r($data);
echo $Crlf;
}else{
echo $data, $Crlf;
}
}
}
class YouTube{
var $content;
var $lineArray;
var $urlSet;
var $videoUrls;
// public:
/**
* 萃取 YouTube 指定 URL 的 FLV
*
* @param string $url 連接的 YouTube Video URL
* @return string flv 的 URL
**/
function CatchURL($url){
$this->videoUrls = array();
$this->_catchURL($url);
$this->_combinData();
$this->_filterData();
$this->_extractData();
return $this->videoUrls;
}
// private
/**
* 抓取指定 URL 的網頁資料
*
* @param string $url 連接的 YouTube Video URL
**/
function _catchURL($url){
debug("0) 連線…{$url}");
$fp = fopen($url, 'r');
if(!$fp){
die('連不上指定網址');
}
debug("1) 資料接收中…");
$buffser_size = 8192;
$this->content = “;
while (!feof($fp)) {
$this->content .= fread($fp, $buffser_size);
}
fclose($fp);
}
/**
* 資料合併
**/
function _combinData(){
debug("2) 資料合併中…(".strlen($this->content).')');
$this->lineArray = explode("n", $this->content);
}
/**
* 移除不必要的資料
**/
function _filterData(){
debug("3) 資料過濾中…(".count($this->lineArray). ")");
$this->urlSet = array_filter($this->lineArray, array($this, "_filter") );
}
/**
* 資料萃取
**/
function _extractData(){
debug("4) 資料抽取中(".count($this->urlSet) . ")");
array_walk($this->urlSet, array($this, '_extract') );
}
/**
* 過濾條件
*
**/
function _filter($v) {
return (!(strpos($v, 'var fo = new SWFObject("/player2.swf?video_id=') === false));
}
/**
* 萃取條件
**/
function _extract($item2, $key) {
static $template = 'http://www.youtube.com/get_video.php?';
debug($item2);
$pattern = '(.*)player2.swf?(.*)", "movie_player"';
eregi($pattern, $item2, $result);
$data = $result[2];
if ($data != “) {
$this->videoUrls[] = $template . $data;
}
}
}
$url = $argv[1];
$aYT = new YouTube();
$VideoUrls = $aYT->CatchURL($url);
echo $VideoUrls[0];
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment