Author: Manuel Lemos
Viewers: 5,484
Last month viewers: 1,593
Categories: PHP Tutorials
Despite it is against YouTube terms of use, many of those users resort to PHP scripts to automate download. However, since YouTube has changed the way their video players work, PHP scripts that worked in the past, do not work anymore.
Read this article to learn about existing PHP scripts that can solve the problem of downloading YouTube or Vimeo videos using PHP in any Website.
As a bonus you may also learn how to search in YouTube for movie trailers of 2018, 2017, 2016, or any other year, as well get more information about movies and TV show episodes from the IMDB site.
Contents
Is It Legal to Download Videos from YouTube?
Why is Your Youtube Downloader Not Working Anymore?
PHP YouTube Downloader Script
PHP Script to Get Links to Download Vimeo and YouTube Videos
Listing Your Channel Videos
YouTube MP4 Downloader
PHP YouTube MP4 Converter Script to use after Download YouTube MP4 Video
MP3 YouTube Downloader
PHP YouTube MP3 Converter Script
Get YouTube Movie Trailers of 2018, 2017, 2016 or Any Other Year
Get More Movie Information Searching IMDB
Download PHP YouTube Download Script Packages
Change Log
What is a PHP Youtube Downloader Script?
A YouTube downloader script is a programming script with code written in PHP or any other language that can download one or more videos to a file in the user machine in MP4 or some other video format supported by YouTube.
Is It Legal to Download Videos from YouTube?
The purpose of this article is not to encourage you to break any laws, video owner copyrights or the YouTube terms of use when you access the videos in a way that may not be authorized by YouTube or the video owners.
The goal here is to guide many users that have accessed and continue to access this and other sites looking for PHP scripts to download videos from YouTube.
When in doubt, you should contact YouTube to determine if the way you are accessing videos made available there, is acceptable for them.
As to whether it is legal to download videos from YouTube, it depends on who owns the video copyrights.
Downloading the Videos of Others
If you want to download videos of other YouTube users, not only you may be violating YouTube terms of use, but you may also be violating the owner copyrights.
YouTube allows videos to have one of two possible licenses: the Standard YouTube License and the Creative Commons license. Downloading a video with the Creative Commons license would not be violating the owner copyrights.
If you are not the owner of the videos, you may still be able to download videos for offline viewing, regardless of the license, using YouTube Red. This is a subscription based service provided by YouTube that allows you to have additional privileges when accessing YouTube videos.
Keep in mind that YouTube is in great part a advertising supported site. Paying a YouTube Red subscription is an alternative way to pay YouTube for the service of providing you the video content, as well reward video creators for their work.
Unfortunately YouTube Red may not yet be available in all countries, so you may not be able to enjoy for its benefits.
Downloading Your Own Videos
If you are the owner of the videos, downloading the videos from YouTube is not illegal. However, you may be breaking the YouTube terms of service and YouTube may cancel your account.
The way to do it with 100% certainty it does not violate the YouTube terms of use is to go on your account video manager page and use the download button for the videos you want to download.
However, if you want to download many of your videos at once, it may become a tedious task. One possible situation on which you may want to download many YouTube videos at once is to keep a copy with yourself or even recover your videos in case you lost the hard drive on which you had your original videos stored.
Why is Your Youtube Downloader Not Working Anymore?
There are many solutions to download YouTube videos out there. Some are sites that provide that service. Others are programming scripts that can be used to download the videos. Those sites also use similar scripts to provide the solution.
The problem is that the scripts rely on YouTube APIs that are not publicly documented. If YouTube discontinues those APIs, the scripts stop working.
The only way to perform the same service is to move on to other APIs that are still supported by YouTube.
PHP YouTube Downloader Script
One way YouTube videos could be download in bulk could have been using the official YouTube API. However, currently the YouTube API does not provide means to download any videos, not even the ones you submitted.
An alternative solution is to use the PHP YouTube Video Downloader package of Nitesh Apte. You can download the package files or install them using the composer program from the PHP Classes composer repository.
This package retrieves the metadata of a video to determine if the videos are available for download from a given URL. The class itself can download the video to a local file in a given directory. The downloaded video could be in MP4 format but nowadays it is very likely to be in the WebM format.
Here is an example script:
$bean = new YouTubeBean(); $bean->setVideoId("xACG4MClvLo"); $bean->setVideoFormat("43"); $bean->setMethod("curl"); $bean->setDestination("./"); $downloader = new YouTubeVideoDownloader(); $downloader->startDownload($bean);
PHP Script to Get Links to Download Vimeo and YouTube Videos
Another alternative to download Vimeo and YouTube videos is the PHP Youtube and Vimeo Video Downloader Script package.
Thius package does not exactly download the videos but returns the URLs to download videos of Vimeo and YouTube, so you can download the videos either clicking on a link or redirecting to the download URLs.
Here is an example script:
$url = "https://www.youtube.com/watch?v=xACG4MClvLo"; // $url = "https://vimeo.com/141975849"; $handler = new LinkHandler(); $downloader = $handler->getDownloader($url); $downloader->setUrl($url); if($downloader->hasVideo()) { echo '<a href="'.$downloader->getVideoDownloadLink().'">'; };
Listing Your Channel Videos
If you want to retrieve the list of all your videos channel videos, you can do that using YouTube API v3. You need to obtain an API key from the Google Developers Console credentials page:
<?php // Your API key here $key = 'Your API Key from the Google Developers Console'; // Your channel ID here $channel = 'UC__StEqeMNvJ-uNhvBwjxuw'; // Get the channel playlists $response = file_get_contents( 'https://www.googleapis.com/' . 'youtube/v3/channels' . '?part=contentDetails' . '&id='.$channel.'&key='.$key); $playlists = json_decode($response); // Get the uploads playlist videos $playlist = $playlists -> items[0] -> contentDetails -> relatedPlaylists->uploads; $response = file_get_contents( 'https://www.googleapis.com/' . 'youtube/v3/playlistItems' . '?part=contentDetails' . '&playlistId='.$playlist.'&key='.$key); $videos = json_decode($response); foreach($videos->items as $video) { // Get the video details $id = $video->contentDetails->videoId; $response = file_get_contents( 'https://www.googleapis.com/' . 'youtube/v3/videos' . '?part=contentDetails' . '&id='.$id.'&key='.$key); $details = json_decode($response); print_r($details); }
YouTube MP4 Downloader
When you download videos from the channel management page, you can download your videos in MP4 format.
However, when you use these scripts, the videos may be in the original format that YouTube stores them, which nowadays is the Webm format. Therefore you may need to convert the videos if you want them in MP4 format.
PHP YouTube MP4 Converter Script to use after Download YouTube MP4 Video
If your video downloaded from YouTube is in Webm format, you may want to convert them to MP4. In this case, the easiest way is to use ffmpeg command.
<?php Exec('ffmpeg -i video.webm video.mp4');
Alternatively you can also use the PHP Video Toolkit package to use a PHP interface to the ffmpeg and other programs to perform also other operations like extracting image frames, extract a segment of a video, convert to animated GIF format, extract audio tracks, etc..
The package files can also be downloaded in zip format or installed using the composer tool from the PHP Classes composer repository.
MP3 YouTube Downloader
As you know, MP3 is a common format for storing audio. Usually MP3 is just the audio part of MPEG stream of a MP4 video, so to extract a MP3 audio from MP4 is a relatively simple task.
However, YouTube does not provide the audio of the videos in MP3 format. Therefore you also need to download the videos first and convert them to MP3 format with another script or tool.
PHP YouTube MP3 Converter Script
If you just want to extract the audio from a video into a MP3 audio file, you can also use ffmpeg command.
<?php Exec('ffmpeg -i video.webm video.mp3');
You can also use the PHP Video Toolkit package for this purpose.
YouTube to MP3 Download Script
An alternative method to download the audio of YouTube videos is to resort to sites that download the videos and convert them into audio files for download in the MP3 format.
The PHP Youtube to MP3 Converter package can display HTML on a Web page of your site to make it serve YouTube videos audio in MP3 format.
Get YouTube Movie Trailers of 2018, 2017, 2016 or Any Other Year
As a bonus for this article, if you want to find or watch movie trailers in YouTube you can use the PHP YouTube Movie Trailers package also from Nitesh Apte.
The package can search for movie trailers of a given year on YouTube and return their video identifiers. The example script shows how to embed the videos in a Web page.
Get More Movie Information Searching IMDB
If you want to retrieve more details about different movies there are several PHP classes to search IMDB, including one more recent that searches IMDB movies scrapping the site pages from Dantigny francois, or an older one that actually search movies using the IMDB API by Tiago André, or one to search for TV shows episodes in IMDB from Arturs Sosins.
Download PHP YouTube Download Script Packages
If you are interested in videos, movies or TV shows and want to do something useful in PHP with them, this article presented several scripts and class packages that you can use.
Here is a summary of the listed PHP script packages that you can download or install using the PHP composer with instructions mentioned on the Download tab of the packages pages:
Keep in mind that some of the packages are based on scraping pages or using undocumented APIs. So, they may stop working in the future.
This article will be updated over time to present solutions that work in the present. If you find out that any of the solutions presented here does not work anymore, post a comment here so the article can be updated.
Meanwhile, if you liked this article, use the share buttons above to let other developers know about it. Also post a comment of you have a question.
Change Log
2017-01-01: These PHP scripts and classes also work in 2017.
You need to be a registered user or login to post a comment
1,616,611 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
Comments:
2. youtube to mp3 converter - Tamia Tube (2019-04-22 14:57)
Tamiatube is one of the best youtube to mp3 converter.... - 0 replies
Read the whole comment and replies
1. comment - arup saha (2019-01-10 19:23)
good... - 3 replies
Read the whole comment and replies