PHP Classes
PHP Classes

YouTube Downloader PHP Script to MP4 that Works in 2016 and 2017

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog YouTube Downloader PH...   Post a comment Post a comment   See comments See comments (3)   Trackbacks (0)  


Updated on: 2017-01-02

Posted on: 1 month ago

Categories: PHP Tutorials

Many users want to download YouTube videos, either to watch offline or to take backups of their own videos.

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 videos using PHP.

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.


Click on the links to go to directly to the respective article section.

Is It Legal to Download Videos from YouTube?

PHP Script to Download YouTube Videos

Listing Your Channel Videos

PHP YouTube MP4 Converter Script

PHP YouTube MP3 Converter Script

Get YouTube Movie Trailers of 2018, 2017, 2016 or Any Other Year

Get More Movie Information Searching IMDB


Change Log

What is a 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.

PHP Script to Download YouTube Videos

One YouTube videos could be download in bulk could have been using the 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();

$downloader = new YouTubeVideoDownloader();

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:

 // 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( '' . '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( '' . '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( '' . 'youtube/v3/videos' . '?part=contentDetails' . '&id='.$id.'&key='.$key);
  $details = json_decode($response);

PHP YouTube MP4 Converter Script

Since nowadays the videos are downloaded in WebM format, you may want to convert them to MP4. In this case, the easiest way is to use ffmpeg command.

 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.

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.

 Exec('ffmpeg -i video.webm video.mp3');

You can also use the PHP Video Toolkit package for this purpose.

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.


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.

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

Login Immediately with your account on:



1. comment - arup saha (2016-12-05 18:09)
good... - 2 replies
Read the whole comment and replies

  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog YouTube Downloader PH...   Post a comment Post a comment   See comments See comments (3)   Trackbacks (0)