Wednesday, March 23, 2016

Download YouTube Playlist

    It's been ages since I wrote my last post, I almost forgot about it completely. Anyways, now I'm back at home for the Holi holidays with a lot of time to kill, I can write one or two posts :P :P

    So, last weekend, I was about to leave for my home the next day and wanted to download Episodes of Last Week Tonight (its a news Satire & its hilarious; must watch!) to take back home and kill time. This was supposed to pretty easy, all I had to do was paste the playlist link in some app and viola!!

But as it always turns out, it wasn't.

    I tried various programs in Windows, but all of them had some problems. Some wouldn't allow me to download more than a certain number of videos in a playlist, some would only download only a small part of each video without registering and few who actually worked throttled the speed to an absolute moribund state. It was pretty frustrating, and I decided to switch to Linux, because hey, it's *almost* always better!

    So, booted into Linux and searched the awesome open-source community for the solution to my problems and it was only a matter of time till I found the perfect module: youtube-dl. It had everything, and more functionality and options than any downloader you could find on windows. My work was done. Copied the link of the playlist and tried to run the script but urrrghhh!! this showed up:

    Didn't matter which playlist I chose, it threw the same error. But it was downloading single videos seamlessly. Bored out of my mind, desperate for the videos and for the sense of *achieving something*, I decided to use python to do this. It was finally the time I actually implemented anything I learned in the past 2 years to an actual use.

    So, how to do this. I knew I could string multiple commands in linux together by using && operator. So I tried downloading two single videos together by joining one of them via && operator and it worked. So, by mathematical induction, I could do this for n videos (it's a pretty good analogy..... and it makes you sound smart :P). So, the main problem was solved. Next, I needed the links for all the videos in the playlist. I'd started web-scraping with python a few days ago and it seemed like a good practice. The YouTube page was pretty complicated(for a beginner), but after a while I figured it out.

#Scapes for links from soup object, adds hostname infront and appends to final list
for x in soup.find_all("a",{"class":"pl-video-title-link yt-uix-tile-link yt-uix-sessionlink  spf-link "}):
    links.append("" + x["href"])

Tried the script on a few playlists and it worked!!! Part 1 was done.

Next up was the main downloader.
This was fairly easy to do, all I needed to do was concatenate the links with the youtube-dl commands and join all of these via the && command.

#concatenate all the links into a single command
for i in range(len(names)):
    command +="youtube-dl -ciwf best "+names[i]+" && " 

command = command[:-4]

    I first tried to run the download method on a different thread, or by forking out a new process, but I realized youtube-dl already did that and it would always go out of my control, so I dropped the whole idea. Made a few specifications, like storing the downloaded videos in a different folder etc. and I was done.

Took me around 2-3 hours to complete, but finally did it!!

Started the downloads and went to sleep(it was 5am already). The satisfaction from completing this was immense and I had the best sleep in weeks!!

Download the code: YouTube Playlist downloader