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 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.

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.

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.

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.

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.

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.

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.

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.

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..

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.