This site runs best with JavaScript enabled.

YouTube to Plex

Command-line scripts to get YouTube content in to Plex videos and audiobooks formats.

I’ve recently created a couple of scripts that will download and convert videos on YouTube to Plex.

What You Need My script makes use of other great tools. Follow the instructions on the links below to install them on your system.

  • youtube-dl
  • ffmpeg
  • wget
  • imagemagick

I’m using a mac. I just used brew to install these packages:

1brew install youtube-dl ffmpeg wget imagemagick

Downloading Videos

The following script will change directories to my preferred plex video location and then download the video to that location. I run this script like so:

youtube2plex <youtube_url>

3# Personalization
4PLEX_DIR="/Volumes/Media/Videos/YouTube Downloads"
6# download the file and move it to the NAS for Plex to pick up
8youtube-dl $1
9cd -

Downloading Audios

In another post I explained how I use Plex for audiobooks. I love the Prologue app. I will often use this script to convert YouTube video (such as BYU Speeches) to audio so I can listen to them in my prologue app. Yes, I could go to BYU speeches and download the audio, but this does more. It puts in the metadata as well as downloading a thumbnail of the video and cropping it to a square image so it looks good in my app.

With this script I can simply run:

youtube2prologue <youtube_url>

3# Personalization
5PLEX_DIR="/Volumes/Media/Audiobooks/YouTube Downloads"
7# Not sure how to automatically get Artist name, so I'll just ask for it.
8echo "Metadata Artist: "
9read ARTIST
11# Put everything in my downloads directory
14# Get variables to use later
15FILENAME=$(youtube-dl -f "mp4" --get-filename $1)
16NEW_FILENAME=$(youtube-dl --get-filename -x -o '%(title)s.mp3' $1)
17TITLE=$(youtube-dl --get-title $1)
18echo $FILENAME
20echo $TITLE
21THUMB_URL=$(youtube-dl --get-thumbnail $1)
23# Download thumbnail and crop it to a square
24wget $THUMB_URL -O cover.webp
25dwebp cover.webp -o cover.png
26convert -define jpeg:size=256x256 cover.png -thumbnail 256x256^ -gravity center -extent 256x256 cover.jpg
28# Download the video in mp4 format
29youtube-dl -f "mp4" $1
31# Convert to MP3 (Doing this manually because filenames were not consistent with youtube-dl)
32ffmpeg -i $FILENAME -vn -ar 44100 -ac 2 -b:a 192k -metadata title=$TITLE -metadata album=$TITLE -metadata artist=$ARTIST -metadata album_artist=$ARTIST temp.mp3
34# add cover art
35ffmpeg -i temp.mp3 -i cover.jpg -map 0:0 -map 1:0 -c copy -id3v2_version 3 -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (front)" $NEW_FILENAME
37# Move the final file to NAS for Plex to pick up
40# housekeeping
41rm temp.mp3
43rm cover.*
44cd -

Discuss on TwitterEdit post on GitHub

Share article
Dustin Davis

Dustin Davis is a software engineer, people manager, hacker, and entreprenuer. He loves to develop systems and automation. He lives with his wife and five kids in Utah.

Join the Newsletter

Dustin Davis