如何运行youtube视频



大家好,我是php的初学者。我需要帮助。我分析和研究,youtube有两种类型的视频id

1) HPEeeQwbTLT8
2)ijLZ9 rlEhg

没有。1在我的代理脚本中运行良好。

http://experiment.letwatchus.com/videos/getvideo.php?videoid=HPEeQwbTLT8&type=下载

没有。2有破折号(-)我的php脚本无法下载这些类型的id视频

http://experiment.letwatchus.com/videos/getvideo.php?videoid=ijLZ9-rlEhg&type=下载

如果你检查上面的链接,第二个链接不会下载视频,但第一个链接会运行良好。

和另一个代理服务器运行这两个id都很好。此代理服务器运行良好,具有以上两个id。

needlunlock.com

我不知道他们如何处理视频的2号破折号id。请帮我,告诉我发生了什么。

getvideo.php文件代码如下

<?php
include_once('config.php');
ob_start();// if not, some servers will show this php warning: header is already set in line 46...
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
return preg_replace('/[^A-Za-z0-9-]/', '', $string); // Removes special chars.
}
function formatBytes($bytes, $precision = 2) { 
$units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); 
$bytes = max($bytes, 0); 
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); 
$pow = min($pow, count($units) - 1); 
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . '' . $units[$pow]; 
} 
function is_chrome(){
$agent=$_SERVER['HTTP_USER_AGENT'];
if( preg_match("/likesGecko)sChrome//", $agent) ){  // if user agent is google chrome
if(!strstr($agent, 'Iron')) // but not Iron
return true;
}
return false;   // if isn't chrome return false
}
if(isset($_REQUEST['videoid'])) {
$my_id = $_REQUEST['videoid'];
if(strlen($my_id)>11){
$url   = parse_url($my_id);
$my_id = NULL;
if( is_array($url) && count($url)>0 && isset($url['query']) && !empty($url['query']) ){
$parts = explode('&',$url['query']);
if( is_array($parts) && count($parts) > 0 ){
foreach( $parts as $p ){
$pattern = '/^v=/';
if( preg_match($pattern, $p) ){
$my_id = preg_replace($pattern,'',$p);
break;
}
}
}
if( !$my_id ){
echo '<p>No video id passed in</p>';
exit;
}
}else{
echo '<p>Invalid url</p>';
exit;
}
}
} else {
echo '<p>No video id passed in</p>';
exit;
}
if(isset($_REQUEST['type'])) {
$my_type =  $_REQUEST['type'];
} else {
$my_type = 'redirect';
}
if ($my_type == 'Download') {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<style type="text/css">
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #f5f5f5;
}
.download {
max-width: 300px;
padding: 19px 29px 29px;
margin: 0 auto 20px;
background-color: #fff;
border: 1px solid #e5e5e5;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
-moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
box-shadow: 0 1px 2px rgba(0,0,0,.05);
}
.download .download-heading {
text-align:center;
margin-bottom: 10px;
}
.mime, .itag {
width: 75px;
display: inline-block;
}
.itag {
width: 15px;
}
.size {
width: 20px;
}
.userscript {
float: right;
margin-top: 5px
}
#info {
padding: 0 0 0 130px;
position: relative;
height:100px;
}
#info img{
left: 0;
position: absolute;
top: 0;
width:120px;
height:90px
}
</style>
</head>
<body>
<div class="download">
<?php
} // end of if for type=Download
/* First get the video info page for this video id */
//$my_video_info = 'http://www.youtube.com/get_video_info?&video_id='. $my_id;
$my_video_info = 'http://www.youtube.com/get_video_info?&video_id='. $my_id.'&asv=3&el=detailpage&hl=en_US'; //video details fix *1
$my_video_info = curlGet($my_video_info);
/* TODO: Check return from curl for status code */
$thumbnail_url = $title = $url_encoded_fmt_stream_map = $type = $url = '';
parse_str($my_video_info);
echo '<div id="info">';
switch($config['ThumbnailImageMode'])
{
case 2: echo '<a href="getimage.php?videoid='. $my_id .'&sz=hd" target="_blank"><img src="getimage.php?videoid='. $my_id .'" border="0" hspace="2" vspace="2"></a>'; break;
case 1: echo '<a href="getimage.php?videoid='. $my_id .'&sz=hd" target="_blank"><img src="'. $thumbnail_url .'" border="0" hspace="2" vspace="2"></a>'; break;
case 0:  default:  // nothing
}
echo '<p>'.$title.'</p>';
echo '</div>';
$my_title = $title;
$cleanedtitle = clean($title);
if(isset($url_encoded_fmt_stream_map)) {
/* Now get the url_encoded_fmt_stream_map, and explode on comma */
$my_formats_array = explode(',',$url_encoded_fmt_stream_map);
if($debug) {
echo '<pre>';
print_r($my_formats_array);
echo '</pre>';
}
} else {
echo '<p>No encoded format stream found.</p>';
echo '<p>Here is what we got from YouTube:</p>';
echo $my_video_info;
}
if (count($my_formats_array) == 0) {
echo '<p>No format stream map found - was the video id correct?</p>';
exit;
}
/* create an array of available download formats */
$avail_formats[] = '';
$i = 0;
$ipbits = $ip = $itag = $sig = $quality = '';
$expire = time(); 
foreach($my_formats_array as $format) {
parse_str($format);
$avail_formats[$i]['itag'] = $itag;
$avail_formats[$i]['quality'] = $quality;
$type = explode(';',$type);
$avail_formats[$i]['type'] = $type[0];
$avail_formats[$i]['url'] = urldecode($url) . '&signature=' . $sig;
parse_str(urldecode($url));
$avail_formats[$i]['expires'] = date("G:i:s T", $expire);
$avail_formats[$i]['ipbits'] = $ipbits;
$avail_formats[$i]['ip'] = $ip;
$i++;
}
if ($debug) {
echo '<p>These links will expire at '. $avail_formats[0]['expires'] .'</p>';
echo '<p>The server was at IP address '. $avail_formats[0]['ip'] .' which is an '. $avail_formats[0]['ipbits'] .' bit IP address. ';
echo 'Note that when 8 bit IP addresses are used, the download links may fail.</p>';
}
if ($my_type == 'Download') {
echo '<p align="center">List of available formats for download:</p>
<ul>';
/* now that we have the array, print the options */
for ($i = 0; $i < count($avail_formats); $i++) {
echo '<li>';
echo '<span class="itag">' . $avail_formats[$i]['itag'] . '</span> ';
if($config['VideoLinkMode']=='direct'||$config['VideoLinkMode']=='both')
echo '<a href="' . $avail_formats[$i]['url'] . '&title='.$cleanedtitle.'" class="mime">' . $avail_formats[$i]['type'] . '</a> ';
else
echo '<span class="mime">' . $avail_formats[$i]['type'] . '</span> ';
echo '<small>(' .  $avail_formats[$i]['quality'];
if($config['VideoLinkMode']=='proxy'||$config['VideoLinkMode']=='both')
echo ' / ' . '<a href="download.php?mime=' . $avail_formats[$i]['type'] .'&title='. urlencode($my_title) .'&token='.base64_encode($avail_formats[$i]['url']) . '" class="dl">download</a>';
echo ')</small> '.
'<small><span class="size">' . formatBytes(get_size($avail_formats[$i]['url'])) . '</span></small>'.
'</li>';
}
//echo '</ul><small>Note that you initiate download either by clicking video format link or click "download" to use this server as proxy.</small>';
//  if(($config['feature']['browserExtensions']==true)&&(is_chrome()))
//  echo '<a href="ytdl.user.js" class="userscript btn btn-mini" title="Install chrome extension to view a 'Download' link to this application on Youtube video pages."> Install Chrome Extension </a>';
?>
</body>
</html>
<?php
} else {
/* In this else, the request didn't come from a form but from something else
* like an RSS feed.
* As a result, we just want to return the best format, which depends on what
* the user provided in the url.
* If they provided "format=best" we just use the largest.
* If they provided "format=free" we provide the best non-flash version
* If they provided "format=ipad" we pull the best MP4 version
*
* Thanks to the python based youtube-dl for info on the formats
*                              http://rg3.github.com/youtube-dl/
*/
$format =  $_REQUEST['format'];
$target_formats = '';
switch ($format) {
case "best":
/* largest formats first */
$target_formats = array('38', '37', '46', '22', '45', '35', '44', '34', '18', '43', '6', '5', '17', '13');
break;
case "free":
/* Here we include WebM but prefer it over FLV */
$target_formats = array('38', '46', '37', '45', '22', '44', '35', '43', '34', '18', '6', '5', '17', '13');
break;
case "ipad":
/* here we leave out WebM video and FLV - looking for MP4 */
$target_formats = array('37','22','18','17');
break;
default:
/* If they passed in a number use it */
if (is_numeric($format)) {
$target_formats[] = $format;
} else {
$target_formats = array('38', '37', '46', '22', '45', '35', '44', '34', '18', '43', '6', '5', '17', '13');
}
break;
}
/* Now we need to find our best format in the list of available formats */
$best_format = '';
for ($i=0; $i < count($target_formats); $i++) {
for ($j=0; $j < count ($avail_formats); $j++) {
if($target_formats[$i] == $avail_formats[$j]['itag']) {
//echo '<p>Target format found, it is '. $avail_formats[$j]['itag'] .'</p>';
$best_format = $j;
break 2;
}
}
}
//echo '<p>Out of loop, best_format is '. $best_format .'</p>';
if( (isset($best_format)) && 
(isset($avail_formats[$best_format]['url'])) && 
(isset($avail_formats[$best_format]['type'])) 
) {
$redirect_url = $avail_formats[$best_format]['url'].'&title='.$cleanedtitle;
$content_type = $avail_formats[$best_format]['type'];
}
if(isset($redirect_url)) {
header("Location: $redirect_url"); 
}
} 
?>

下载.php代码:

**

<?php
include_once('config.php');
// Check download token
if (empty($_GET['mime']) OR empty($_GET['token']))
{
exit('Invalid download token 8{');
}
// Set operation params
$mime = filter_var($_GET['mime']);
$ext  = str_replace(array('/', 'x-'), '', strstr($mime, '/'));
$url  = base64_decode(filter_var($_GET['token']));
$name = urldecode($_GET['title']). '.' .$ext; 
// Fetch and serve
if ($url)
{
$size=get_size($url);
// Generate the server headers
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== TRUE)
{
header('Content-Type: "' . $mime . '"');
header('Content-Disposition: attachment; filename="' . $name . '"');
header('Expires: 0');
header('Content-Length: '.$size);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
}
else
{
header('Content-Type: "' . $mime . '"');
header('Content-Disposition: attachment; filename="' . $name . '"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Content-Length: '.$size);
header('Pragma: no-cache');
}
readfile($url);
exit;
}
// Not found
exit('File not found 8{');

**

朋友们,我正在调试我的代码,当我点击1时,会给出id大小,但在2时,我没有得到任何大小,我想这就是为什么第二个链接类型没有下载的原因。这是我获取尺寸的函数。

function get_size($url) {
$my_ch = curl_init();
curl_setopt($my_ch, CURLOPT_URL,$url);
curl_setopt($my_ch, CURLOPT_HEADER,         true);
curl_setopt($my_ch, CURLOPT_NOBODY,         true);
curl_setopt($my_ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($my_ch, CURLOPT_TIMEOUT,        10);
$r = curl_exec($my_ch);
foreach(explode("n", $r) as $header) {
if(strpos($header, 'Content-Length:') === 0) {
return trim(substr($header,16)); 
}
}
return '';
}

提取视频id的PHP示例如下:

<?php
preg_match(
'/[\?\&]v=([^\?\&]+)/',
'http://www.youtube.com/watch?v=OzHvVoUGTOM&feature=channel',
$matches
);
// $matches[1] should contain the youtube id
?>
<embed width="420" height="315"
src="http://www.youtube.com/v/wHdSkyhuocM">

最新更新