2017-04-08 101 views
1

我想直接下载YouTube视频到主机空间。当我在我的电脑上尝试本地服务器上的脚本,例如xampp时,它的效果很好。但是当我尝试000webhosting脚本时,它不起作用。它显示错误这样YouTube视频有下载使用PHP服务器

See the problem i am having in this screen shot

错误是像下面。

Uh oh, it looks like an error occurred: No MP4 video source was able to be located.

下面是下载Youtube视频的PHP代码。

<?php 

// What YouTube URL was provided to us? 
$url = (string) @$_GET['url']; 

/** 
* Define a function to extract a YouTube Video ID from a URL. 
* 
* @param string $url A YouTube Video URL 
* @return mixed   If successful, will return a string. If failed, will return NULL 
*/ 

function getYouTubeVideoIdFromUrl($url) 
{ 
preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $matches); 

// If this match exists. 
if (sizeof($matches) >= 2 && strlen($matches[1])) 
{ 
    return $matches[1]; 
} 

return NULL; 
} 

/** 
* Define a function to extract a YouTube encoded stream URL. 
* 
* @param array $streams An array of streams provided to us from YouTube's "get_video_info" page. 
* @return mixed    If successful, will return the MP4 stream URL. If failed, will return NULL 
*/ 

function getMP4FromEncodedStream($streams) 
{ 
foreach($streams as $stream) 
{ 
    // Decode this stream's data. 
    parse_str($stream, $data); 

    // If we found our MP4 stream source. 
    if (stripos($data['type'], 'video/mp4') === 0) 
    { 
     return $data['url']; 
    } 
} 

// We didn't find any, whoops.. 
return NULL; 
} 

// Try to validate their request. 
try 
{ 
// If an invalid YouTube URL was provided. 
if (($videoId = getYouTubeVideoIdFromUrl($url)) === NULL) 
{ 
    throw new Exception('An invalid YouTube Video URL was provided.'); 
} 

// Retrieve all information pertaining to this Video; more specifically, we're looking for the encoded video streams. 
parse_str(file_get_contents('http://youtube.com/get_video_info?video_id=' . $videoId), $videoData); 

// If there's a problem extracting information. 
if (@$videoData['status'] == 'fail') 
{ 
    throw new Exception($videoData['reason']); 
} 

// Were we able to locate an encoded stream in MP4 format? 
if (($streamUrl = getMP4FromEncodedStream(explode(',', $videoData['url_encoded_fmt_stream_map']))) === NULL) 
{ 
    throw new Exception('No MP4 video source was able to be located.'); 
} 

// Where will we be saving this Video? 
$saveAs = dirname(__FILE__) . '/' . $videoId . '.mp4'; 

// Try to open the encoded video stream URL. 
if ($read = @fopen($streamUrl, 'r')) 
{ 
    // Open the file we want to save to. 
    $write = @fopen($saveAs, 'w'); 

    // Write the stream to our file. 
    $streamReturn = stream_copy_to_stream($read, $write); 

    // Close our files. 
    @fclose($read); 
    @fclose($write); 

    // If we were unable to copy from stream. 
    if ($streamReturn === false) 
    { 
     throw new Exception('We were unable to copy this from the stream.'); 
    } 
} 

// If our new file doesn't exist, we have a problem. 
if ([email protected]_exists($saveAs)) 
{ 
    throw new Exception('We encountered an issue saving this Video.'); 
} 

// Everything saved properly, let them know there they can see their file. 
print 'Your Video <strong>' . $videoId . '</strong> has been saved to <strong>' . $saveAs . '</strong>'; 
} 
// If something went wrong. 
catch(Exception $e) 
{ 
print '<strong>Uh oh, it looks like an error occurred:</strong> ' . $e->getMessage(); 
} 

?> 

所有你需要做的就是这个脚本复制到你的网络服务器,并运行一个URL文件类似下列之一:

http://www.mywebserver.com/youtube-downloader.php?url=https://www.youtube.com/watch?v=dQw4w9WgXcQ 
http://www.mywebserver.com/youtube-downloader.php?url=https://youtu.be/dQw4w9WgXcQ 

一切工作正常,但影片没有下载网上的服务器,主机空间..

帮助.. 在此先感谢..

+0

删除所有'@'并检查是否有任何错误。 – apokryfos

+0

你应该先阅读[YouTube使用条款(https://www.youtube.com/static?template=terms)。 – wormi4ok

+0

OKK ..我会尝试删除 –

回答

0

您可以使用以下开源影片下载PHP脚本的链接

Youtube Downloader PHP Script

该脚本还可以使每台设备的高分辨率视频和更多的功能与适当的安全功能。

+0

我想上面的脚本加以解决 –

+0

您可以下载这个脚本,并得到回答的脚本链接无法使用 – ImBS