Community Platform
Interests
  • Application development
This Year
No Points
Total
1000 Points
MIS Badge

Click here
to validate the recipient

Download Youtube Playlists [PHP Web App Tutorial]

I saw a friend downloading music from Youtube, pasting in each URL and downloading songs individually. I was like, “That’s ridiculous! Its taking too much time! Why don’t you just torrent if you’re going to do that?”. She was unwilling to venture to the Pirate’s bay so I decided to make a more convenient way to download mp3’s from YouTube.

This is pretty much just a web wrapper for dl-youtube and ffmpeg but interesting nonetheless. The real lesson here is interacting with the host system via PHP scripts.

It’s important to keep in mind that this was designed for personal (single user) use, it should be glaringly obvious that there would be bugs/incorrect behavior with concurrent usage and that this is totally unfit/unsafe for production.

This is what our final product will look like:

screen-shot-2016-10-11-at-9-31-51-am

And while loading:

screen-shot-2016-10-11-at-9-34-31-am

Here’s what the app does:

  1. Accepts a YouTube playlist URL
  2. Fetches the audio files in .m4a format
  3. Uses ffmpeg to convert them to .mp3
  4. Produces a .zip of the mp3’s
  5. Sends the zip to the user
  6. Deletes the files.

In order to make it work you’ll need:

  • A web server
  • PHP
  • Python
  • dl-youtube
  • ffmpeg

You can get the code here.

First things first, install dl-youtube (On Linux, OS X):

$ sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl && chmod a+rx /usr/local/bin/youtube-dl

Next, install ffmpeg. Since I’m on OS X I used homebrew:

$ brew install ffmpeg

We’ll start off by making a directory within the web root and creating index.php


# If you're in /var/www/* or a directory owned by root or apache you may as well type
# $ sudo su
# and save yourself some trouble
$ mkdir downloadYoutube && cd downloadYoutube
$ touch index.php style.css

Now open the directory in your favorite text editor and let’s get coding.

I included Bootstrap and jQuery just to make things quicker but both are optional. Bootstrap is for styling and jQuery is a “write less do more” javaScript library. Neither of these libraries are necessary.

The homepage is really basic, just a form. Because of an inefficient parser there’s no quick way to post HTML so I’ll just post pictures. I typically hate this practice but, like I said, it’s unavoidable here.

screen-shot-2016-10-11-at-9-50-52-am

So as you can see, we require main.php at the top, and if the form is submitted, the script creates a new download object and calls the downloadPlaylist method which is passed the URL.

Once the download button is clicked/form is submitted  jQuery unhides the loading message and the download begins.

 

The bread and butter here is in a couple of different files,  dl.php and main.php.

 

At the top we define a global variable so we can insure that dl.php isn’t accessed directly.

main.php interacts with ffmpeg and youtube-dl to get mp3’s, then passes them to dl.php to prep for download. Because brew only installs for the local user, PHP doesn’t have youtube-dl or ffmpeg in it’s path so I include the full path to the binaries. We only want mp3 files from the youtube videos, so we set those options.


#The command

$ 2>&1 

#pipes output to stderr to stdout.

# 0 = stdin, 1 = stdout, 2= stderr
screen-shot-2016-10-11-at-10-15-49-am

 

dl.php contains a script (or, a single function) for making a zip. It utilizes PHP’s ZipArchive class. The process is fairly straight forward, the function checks to make sure that the files passed exist, and that a zip can be created and saved. If the zip can be created, it is, and the function returns true. Otherwise, it returns false.

screen-shot-2016-10-11-at-10-00-04-am

 

After everything’s all said and done, you should be able to download youtube playlists as a .zip with a simple web interface. No more ads, no more waiting.

Skip to toolbar