Created
May 7, 2016 17:36
-
-
Save bzz0217/9373c426f6ec7095f9841cc4ed182c86 to your computer and use it in GitHub Desktop.
動画ダウンローダ - youtube-dlで動画情報を取得
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* youtube-dlコマンド 動画情報取得 | |
*/ | |
class youtube_dl_get_info { | |
private $info; | |
/* | |
* プロパティの動画情報取得 | |
*/ | |
public function get_info(){ | |
return $this->info; | |
} | |
/* | |
* 動画情報取得 | |
* @param string url 動画URL | |
*/ | |
public function command($url){ | |
$args = escapeshellarg($url); | |
$cmd = '/usr/bin/youtube-dl -j --cache-dir /tmp ' . $args . ' 2>&1'; | |
exec($cmd, $msg, $res); | |
if ($res==0){ | |
//--------------------- | |
// 取得成功 | |
//--------------------- | |
$tmp = json_decode($msg[0], true); | |
$this->info = array( | |
'thumbnail' => $tmp['thumbnail'], | |
'title' => $tmp['fulltitle'], | |
'error_code' => 0, | |
'error_info' => '', | |
'error_msg' => '' | |
); | |
}else{ | |
//--------------------- | |
//取得失敗 | |
//--------------------- | |
$ERROR_PATTERN = "{^ERROR: Unable to download webpage: HTTP Error (\d+):(.*?);}"; | |
$this->info = array( | |
'thumbnail' => '', | |
'title' => '', | |
); | |
if (preg_match($ERROR_PATTERN, $msg[0], $code)){ | |
$this->info['error_code'] = $code[1]; | |
$this->info['error_info'] = $code[2]; | |
} | |
$this->info['error_msg'] = $msg[0]; | |
} | |
return $this->info; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment