Recently it appears Youtube has decided they want to become cable TV, complete with the unending commercials for American pharmaceuticals. Since I cannot stand watching any advertising, am cloud averse, and also don’t like Youtube’s terribly busy interface, I have automated downloading of channels and playlists I follow with the wonderful youtube-dl tool into my Odroid XU4 based Plex Media Server.

Downloading is pretty straight forward as long as we take care to follow Plex’s file naming guidelines. Getting all the metadata in without doing any work requires a little bit of configuration, and to automate it all takes a few simple shell scripts.







PLEX SERVER SETUP

I like everything to be tidy in my Plex Server, so the immediate problem I noticed with downloading Youtube videos is that Plex doesn’t really have a good way to scrape the the metadata. Some popular channels have TVDB entries, but most don’t. If you select “TV Shows” as the type of library it won’t show any videos at all, but if you select “Other Videos” it will refuse to even show manually added metadata.

Fortunately there is a custom scanner and media agent that makes organizing personal video archives much easier. We need to install both:

Installing is fairly straightforward, copy the files to the appropriate directories for your operating system.

Restart Plex Media Server, and go on over to the Server settings page, enable the new media agents here:

Now we can create a Youtube library of type “TV Shows” if we enable our media agent and scanner here:

That’s all we need to do on the Plex side.





DOWNLOADING YOUTUBE

Youtube-dl is quite powerful. You will need to install it according to the manual instructions because inevitably the package for your OS will be out of date. In Linux this is accomplished at a shell:

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

Looking over the youtube-dl docs you will see there are quite a few options for what to download and how to add those files. For our purposes here we’re going to archive an entire channel/playlist, and then set some scripts up to check for new future videos on those channels, with all the metadata and thumbnails.

So here’s one long command to download an entire channel (or playlist), starting at the beginning, getting the best audio and video and combining them, adding metadata, writing thumbnail, outputting a Plex preferred filename and batching this while also avoiding re-downloading things we’ve already downloaded. Whew!

youtube-dl --playlist-reverse --download-archive /mnt/Media/Youtube/downloaded.txt -i -o "%(uploader)s/%(playlist)s/%(playlist)s - S01E%(playlist_index)s - %(title)s [%(id)s].%(ext)s" -f bestvideo[ext=mp4]+bestaudio[ext=m4a] --merge-output-format mp4 --add-metadata --write-thumbnail --batch-file=/mnt/Media/Youtube/channel_list.txt

You can see there are two TXT files here that I have placed in my Youtube media folder. One is a list of URLs of things I want to download, the other is a list of already downloaded media so that future runs of youtube-dl will know what to grab.

Let’s simply put that command in a shell script and save it as youtube_archive.sh

#!/bin/bash youtube-dl --playlist-reverse --download-archive /mnt/Media/Youtube/downloaded.txt -i -o "%(uploader)s/%(playlist)s/%(playlist)s - S01E%(playlist_index)s - %(title)s [%(id)s].%(ext)s" -f bestvideo[ext=mp4]+bestaudio[ext=m4a] --merge-output-format mp4 --add-metadata --write-thumbnail --batch-file=/mnt/Media/Youtube/channel_list.txt

Then add to cron with the crontab -e command, this will run every 12 hours:

0 */12 * * * /path/to/script/youtube_archive.sh

For Youtube channels or playlists where you want to use different settings for downloading or naming (ie. if a channel publishes separate web series to different playlists, if you only want to download the latest videos instead of everything, etc.). Then add your command to the youtube_archive.sh script but use a different channel_list.txt.

MORE LINKS



