Reputation: 61
I am trying to download Youtube videos directly to hosting space. When i try the script on local server like xampp on my PC then it works well. but when i try the script on 000webhosting it do not works. It shows error like this
See the problem i am having in this screen shot
the error is like below.
Uh oh, it looks like an error occurred: No MP4 video source was able to be located.
Below is PHP code for downloading Youtube video.
<?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 ( !@file_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( );
}
?>
All you need to do is copy this script to your webserver, and run the file with a URL like one of the following:
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
All is working well but the videos are not downloading to online server, hosting space..
help .. thanks in advance..
Upvotes: 6
Views: 22965
Reputation: 33
You can use this library
Install
composer require vqhteam/ytdownload
Use
<?php
use Vqhteam\Ytdownload\YTDownload;
require_once "vendor/autoload.php";
$get = YTDownload::getLink("Youtube Video Id");
var_dump($get);
?>
https://packagist.org/packages/vqhteam/ytdownload
Upvotes: 0
Reputation: 11
Install this package https://github.com/socialAPIS/YoutubeDownloader
See this example https://github.com/socialAPIS/YoutubeDownloader/blob/master/Examples/FetchPlayer.php
$video = file_get_contents($video_url);
file_put_contents($somedir . './videoplayback.mp4', $video);
Upvotes: 1
Reputation: 345
You can use this package https://packagist.org/packages/athlon1600/youtube-downloader .This only depends on cURL and also gets updated regularly as of May 2020.You can install it via composer require athlon1600/youtube-downloader
.
Upvotes: 0
Reputation: 662
YouTube Video Downloader Library:
URL will look like this: http://localhost/index.php?vid=VIDEOID
$_GET['vid'] = 'VIDEOID';
$vid = $_GET['vid']; //the youtube video ID
parse_str(file_get_contents("http://youtube.com/get_video_info?video_id=" . $vid), $info); //decode the data
$videoData = json_decode($info['player_response'], true);
$videoDetails = $videoData['videoDetails'];
$streamingData = $videoData['streamingData'];
$streamingDataFormats = $streamingData['formats'];
//set video title
$video_title = $videoDetails["title"];
print_r($streamingDataFormats[1]['url']);
$video = $streamingDataFormats[1]['url'];
file_put_contents("video-title-name.mp4", fopen($video, 'r'));
echo 'Youtube Video Download finished! Now check the file.';
Upvotes: 4
Reputation: 138
Try to use urlencode()
for your youtube url
http://php.net/manual/en/function.urlencode.php
Upvotes: 0