Posted on

I have two scripts which help me import YouTube videos into my Plex server. This script allows me to download entire YouTube channels to my local directory. It keeps track of what has already been downloaded, so that the script doesn’t download the whole channel each time the script is run. My recommendation is that you set up a cron job to automatically run the script on a schedule you find appropriate.

If you like this script, be sure to check out my other script, Youtube-to-Plex, which downloads individual YouTube videos.


Plex is a versatile home media server software which allows me to have greater control over the content which I watch. On occasion, I may store a video from YouTube onto my Plex server. Why keep a copy of a video which I can already pull up on YouTube whenever I want?

  1. The video may not stay up forever (e.g. a friend temporarily uploads a video and you download it)
  2. You want to keep an archive of an important video
  3. Ease of keeping track of watch history (Plex is easier to navigate vs. YouTube on videos watched)

This software is free for you to use and licensed with the MIT License. If you do use it, please drop by my GitHub, and drop the Plex-Scripts Repository a star. Any suggestions or bugs? Create a new Issue on GitHub.

#!/bin/bash

# This is a script to save entire Youtube channels to your server, in a fashion that Plex would easily understand
# Created by Pranav Mishra

# --------------------------------------------------------------------------------------------------
# MIT License

# Copyright (c) 2020 Pranav Mishra
# Online repository at https://github.com/pranavmishra90

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# --------------------------------------------------------------------------------------------------




#---------------------------------------------------
#Listing Channels that have been downloaded already
#---------------------------------------------------


cd /volume1/Plex-Media/Youtube/Education
echo "Youtube Education Channels already downloaded"
echo "------------------------------------"
ls -1
echo "------------------------------------"
echo ""
echo ""

cd /volume1/Plex-Media/Youtube/TV-on-Youtube 
echo "TV Shows on Youtube already downloaded"
echo "------------------------------------"
ls -1
echo "------------------------------------"
echo ""
echo ""

#-------------------------------------
#Download entire educational channel
#-------------------------------------

echo "Downloading complete education channels"
cd /volume1/Plex-Media/Youtube/Education
echo "------------------------------------"
ls -1
echo "------------------------------------"
echo ""
echo ""

/usr/local/bin/youtube-dl --playlist-reverse --batch-file=education_allvideos.txt \
-i -o "%%(uploader)s/%%(uploader)s - S01E%%(playlist_index)s - %%(title)s [%%(upload_date)s] [%%(id)s].%%(ext)s" \
-f 'bestvideo[height<=720][ext=mp4]+bestaudio[ext=m4a]/best[height<=720][ext=mp4]' --merge-output-format mp4 \
--download-archive education_downloaded.txt \
--ignore-config \
--write-thumbnail --embed-subs --write-sub --sub-lang en \
--ignore-errors --no-continue --no-overwrites --no-post-overwrites \
--embed-thumbnail \
--add-metadata

#Print the current date to the downloaded archive

date >> education_downloaded.txt

#---------------------------------------
#Download entire TV-on-Youtube channel
#---------------------------------------

echo "Downloading complete TV-on-Youtube channels"

#Switch to the Youtube Channels (Non-educational) folder in the Plex Media directory
cd /volume1/Plex-Media/Youtube/TV-on-Youtube 
echo "------------------------------------"
ls -1
echo "------------------------------------"
echo ""
echo ""

/usr/local/bin/youtube-dl --playlist-reverse --batch-file=tv-on-youtube_allvideos.txt \
-i -o "%%(uploader)s/%%(uploader)s - S01E%%(playlist_index)s - %%(title)s [%%(upload_date)s] [%%(id)s].%%(ext)s" \
-f 'bestvideo[height<=720][ext=mp4]+bestaudio[ext=m4a]/best[height<=720][ext=mp4]' --merge-output-format mp4 \
--download-archive tv-on-youtube_downloaded.txt \
--ignore-config \
--write-thumbnail --embed-subs --write-sub --sub-lang en \
--ignore-errors --no-continue --no-overwrites --no-post-overwrites \
--embed-thumbnail \
--add-metadata

#-----------------------------------------------
#Download channel starting with 10 newest videos
#-----------------------------------------------

echo "Downloading channel with 10 newest videos"

/usr/local/bin/youtube-dl --batch-file=10_newest_list.txt \
-i -o "%%(uploader)s/%%(uploader)s - S01E%%(playlist_index)s - %%(title)s [%%(upload_date)s] [%%(id)s].%%(ext)s" \
-f 'bestvideo[height<=720][ext=mp4]+bestaudio[ext=m4a]/best[height<=720][ext=mp4]' --merge-output-format mp4 \
--download-archive tv-on-youtube_downloaded.txt \
--ignore-config \
--write-thumbnail --embed-subs --write-sub --sub-lang en \
--ignore-errors --no-continue --no-overwrites --no-post-overwrites \
--embed-thumbnail \
--add-metadata \
--max-downloads 10


#----------------------------------
#Download videos after Jan 1, 2019
#----------------------------------

echo "downloading 2019 channel videos"

/usr/local/bin/youtube-dl --playlist-reverse --batch-file=2019onwards_list.txt \
-i -o "%%(uploader)s/%%(uploader)s - S01E%%(playlist_index)s - %%(title)s [%%(upload_date)s] [%%(id)s].%%(ext)s" \
-f 'bestvideo[height<=720][ext=mp4]+bestaudio[ext=m4a]/best[height<=720][ext=mp4]' --merge-output-format mp4 \
--dateafter 20190101 \
--download-archive tv-on-youtube_downloaded.txt \
--ignore-config \
--write-thumbnail --embed-subs --write-sub --sub-lang en \
--ignore-errors --no-continue --no-overwrites --no-post-overwrites \
--embed-thumbnail \
--add-metadata

#Print the current date to the downloaded archive
date >> tv-on-youtube_downloaded.txt

echo "---------------------"
echo "Youtube-For-Plex Complete"

Please remember to only archive videos that you have a legal right to download.