2017-04-08 1 views
1

나는 유튜브 동영상을 호스팅 공간으로 직접 다운로드하려고합니다. 내 컴퓨터에서 xampp과 같은 로컬 서버에서 스크립트를 실행하면 제대로 작동합니다. 하지만 000webhosting에서 스크립트를 실행하면 작동하지 않습니다. 이는 오류 에러는 다음과 같다이유튜브 동영상을 서버에 다운로드하여 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 코드입니다.

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 

모두 잘 작동하지만 동영상이 다운로드되지 않습니다

<?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을 사용하여 파일을 실행하다 온라인 서버, 호스팅 공간 ..

도움 .. 미리 감사 ..

+0

'@'를 모두 제거하고 오류가 있는지 확인하십시오. – apokryfos

+0

처음에는 [Youtube Terms of Use] (https://www.youtube.com/static?template=terms)를 읽어야합니다. – wormi4ok

+0

okk .. –

답변

0

아래 사용할 수 있습니다에 오픈 소스 유튜브 다운 로더 PHP 스크립트의 링크

Youtube Downloader PHP Script

이 스크립트는 또한 적절한 보안 기능을 디바이스의 해상도에 따라 비디오 및 더 많은 기능을 할 수 있습니다.

+0

위의 스크립트를 해결하고 싶습니다. –

+0

이 스크립트를 다운로드하고 스크립트 링크를 대답에서 가져올 수 있습니다. – ImBS

관련 문제