Auto-Downloading Youtube Videos for Plex Media Server with youtube-dl

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.

RELATED >>  Installing Volumio in a Proxmox Virtual Machine

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


11 Comments

  1. Thanks, great article! Don’t have a Plex, but youtube-dl settings will come in handy for my Kodi.

    P.S.: funny how you can’t stand ads but have 3 Amazon ones on your own blog )) I guess it’s ok cause they are topical?

    Reply

    1. Please use an adblocker, I can’t see them and fully support circumventing advertising in whatever way you can :). I realize the irony but the occasional person clicking & buying a Raspberry Pi pays for my server colo.

      Maybe I should do a write up on Pi Hole next!

      PS after using this for a while another thing you might want to do is add the youtube-dl update command to run in crontab every day or so, youtube breaks it a lot but it usually gets updated immediately.

      Reply

  2. Thanks for the guide, I found it really useful. One question is how did you get the thumbnails for the Channel and Playlist as the script only seems to get thumbnails for the individual episodes. Thanks.

    Reply

  3. Are you familiar with any way to download the audio only? I know that youtube-dl can download the video and then extract the audio with ffmpeg, but that wastes bandwidth and I’m on a capped internet connection.

    Reply

  4. Thanks for following up. Here’s what I ended up with.

    youtube-dl –download-archive downloaded.txt –prefer-ffmpeg -i -U -o “/media/music/YouTube/%(uploader)s/%(title)s.%(ext)s” -f bestaudio[ext=m4a] –batch-file=urls_to_download.txt &> /var/log/youtube-dl-cron.log &

    This downloads audio only, no need to extract.

    Reply

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

    Can you provide an example or instructions?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *