Post

Downloading YouTube videos directly using youtube-dl

A tool for downloading YouTube videos directly through your terminal, and how to integrate it into Plex.


Introduction and Purpose

I was looking for a way to directly download YouTube videos without using sketchy converter websites, and I came across a tool on GitHub that would allow me to do this directly through the Windows command line, youtube-dl.

This blog post will explain how to download this tool, go through its usage and documentation, and explore how we can set up a batch script to streamline this process to download these files directly to a Plex library and have it updated to be watched immediately.


How to Download

yt-downloader works on Linux, Mac, and Windows, with solid installation instructions and usage documentation on GitHub.

To download the utility onto Windows, you can either download the .exe file to any location on your PATH or use the pip command.

I ran into issues with the latest version on the youtube-dl repository but was able to get it working using the nightly version.

I used the pip installation method since I had it already installed, but I will provide explanations for both methods on a fresh Windows 11 machine.

pip install --upgrade --force-reinstall "git+https://github.com/ytdl-org/ytdl-nightly.git"

Method 1: Downloading the .exe and adding it to PATH

Download the latest version of youtube-dl.exe from the ytdl-nightly repository.

I suggest creating a folder in your C:\Program Files\Youtube-DL and moving the executable file there.

Add the folder’s directory to your PATH environmental variable:

  • Copy the directory path for youtube-dl.exe (C:\Program Files\Youtube-DL)
  • Open Settings and navigate to System > About > Advanced system settings > Environment Variables
  • Select PATH > Edit > add youtube-dl’s directory to the list.

Open a new command prompt, and you should be able to use the youtube-dl command.

If you get a message saying the operation “cannot proceed because msvcr100.dll was not found”, this can be fixed by installing “Microsoft Visual C++ 2010 Redistributable”. This can be fixed by installing both the x86 and x64 versions from the Microsoft Docs.


Method 2: Downloading with Python, pip, and git

pip is the standard package manager used by Python to easily import modules to use when writing Python scripts.

This also means that pip comes installed with Python, and we can get it by downloading the latest version of Python (3.12.2 as of writing) from the official website.

  • When running the Python installer, check both:
    • “Use admin privileges when installing py.exe”
    • “Add python.exe to PATH”

After that, you should be able to use the py command in the command prompt.
It should open and have the following output:

C:\Users\Austin> py
Python 3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

If the Windows Store opens instead when using the command, you can tell Windows it is installed by doing the following:

  • Open Settings: ‘System > Settings > Apps > Apps & features’
  • Select ‘App execution aliases’
  • Uncheck the entries for: ‘App Installer - python.exe’ and ‘App Installer - python3.exe’

The pip command to download Python modules should now work using the command py -m pip install <module>

Download Git for Windows from its official website, and add Git to your path:

  • Find the download directory for Git (C:\Program Files\Git\bin)
  • Open Settings and navigate to System > About > Advanced system settings > Environment Variables
  • Select PATH > Edit > add Git’s directory to the list.

We can now download the nightly version of youtube-dl using the following:

py -m pip install --upgrade --force-reinstall "git+https://github.com/ytdl-org/ytdl-nightly.git"

Usage

After installation, you should now be able to download videos from YouTube using the youtube-dl <YT-URL> command.

You can view the documentation on Github, or using the help command: youtube-dl -h


Creating a Batch Script to utilize with Plex

If you are hosting a Plex Media Server, you can access media files anywhere you can download Plex (which includes mobile and most Smart TVs).

This is handy if you have a backlog of YouTube videos that you’d like to watch on TV without advertisements.

For this setup, I have created a Plex account, installed Plex Media Server, and organized my Media folders as shown below.

📂 Plex Media Server Folder Structure
/Media /Movies movie content /TV Shows television content /YouTube youtube content

I was able to come up with the following batch script:

@echo off
:: This batch file downloads a YouTube video from a URL and saves to a chosen Plex library.
:: Then forces that Plex library to scan for updates.
set PLEX_TOKEN=(see: Finding an authentication token)
set /p URL=Enter YouTube URL:

set ID=0
echo Enter the library that you would like this media to be saved to:

:loop

"C:\Program Files\Plex\Plex Media Server\Plex Media Scanner.exe" --list

set /p ID="Library ID#: "

if %ID%==1 (set DIR=Movies&&goto endloop)
if %ID%==2 (set DIR=TV Shows&&goto endloop)
if %ID%==3 (set DIR=YouTube&&goto endloop)

echo. && echo Invalid Choice
goto loop


:endloop

set DL_DIR=C:/Users/silkt/Videos/Plex/Media/%DIR%/
echo. && echo Media will be saved to %DL_DIR%

youtube-dl %URL% --output "%DL_DIR%%%(title)s - %%(uploader)s.%%(ext)s"

echo Updating Plex Media Server (%DL_DIR%) folder
curl "http://localhost:32400/library/sections/%ID%/refresh?X-Plex-Token=%PLEX_TOKEN%"

This post is licensed under CC BY 4.0 by the author.