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>

1#!/bin/bash
2
3# Personalization
4PLEX_DIR="/Volumes/Media/Videos/YouTube Downloads"
5
6# download the file and move it to the NAS for Plex to pick up
7cd $PLEX_DIR
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>

1#!/bin/bash
2
3# Personalization
4DOWNLOAD_DIR="/Users/dustin/Downloads"
5PLEX_DIR="/Volumes/Media/Audiobooks/YouTube Downloads"
6
7# Not sure how to automatically get Artist name, so I'll just ask for it.
8echo "Metadata Artist: "
9read ARTIST
10
11# Put everything in my downloads directory
12cd $DOWNLOAD_DIR
13
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
19echo $NEW_FILENAME
20echo $TITLE
21THUMB_URL=$(youtube-dl --get-thumbnail $1)
22
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
27
28# Download the video in mp4 format
29youtube-dl -f "mp4" $1
30
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
33
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
36
37# Move the final file to NAS for Plex to pick up
38mv $NEW_FILENAME $PLEX_DIR
39
40# housekeeping
41rm temp.mp3
42rm $FILENAME
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