dongshan3806 2015-04-27 09:49 采纳率: 0%
浏览 449

如何运行YouTube视频

Hello everyone I am a beginner in php. I need help. I analyze and research that youtube have two types of video id

1) HPEeQwbTLT8
2) ijLZ9-rlEhg

No. 1 is running good in my proxy script.

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

No. 2 which have dash (-) my php script is unable to download these type of id videos

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

if you check above link 2nd link will not download the video but first link will running well.

and another proxy server run these both id well. This proxy server is running good with above both id.

needunblock.com

I don't know how they can process No. 2 dash type id of video please help me and give me an idea what is happening.

getvideo.php file code are as follow.

<?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("/like\sGecko\)\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"); 
}

} 
?>

download.php code :

**

<?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{');

**

Friends I have debugging my code and when I hit no. 1 id size is given but in no.2 I did'nt get any size I think thats why 2nd link type not downloaded. here is my function of getting size .

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("
", $r) as $header) {
        if(strpos($header, 'Content-Length:') === 0) {
            return trim(substr($header,16)); 
        }
     }
    return '';
}
  • 写回答

2条回答 默认 最新

  • dongzai3917 2015-04-27 09:56
    关注
    <embed width="420" height="315"
    src="http://www.youtube.com/v/wHdSkyhuocM">
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看