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/bash23# Personalization4PLEX_DIR="/Volumes/Media/Videos/YouTube Downloads"56# download the file and move it to the NAS for Plex to pick up7cd $PLEX_DIR8youtube-dl $19cd -
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/bash23# Personalization4DOWNLOAD_DIR="/Users/dustin/Downloads"5PLEX_DIR="/Volumes/Media/Audiobooks/YouTube Downloads"67# Not sure how to automatically get Artist name, so I'll just ask for it.8echo "Metadata Artist: "9read ARTIST1011# Put everything in my downloads directory12cd $DOWNLOAD_DIR1314# Get variables to use later15FILENAME=$(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 $FILENAME19echo $NEW_FILENAME20echo $TITLE21THUMB_URL=$(youtube-dl --get-thumbnail $1)2223# Download thumbnail and crop it to a square24wget $THUMB_URL -O cover.webp25dwebp cover.webp -o cover.png26convert -define jpeg:size=256x256 cover.png -thumbnail 256x256^ -gravity center -extent 256x256 cover.jpg2728# Download the video in mp4 format29youtube-dl -f "mp4" $13031# 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.mp33334# add cover art35ffmpeg -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_FILENAME3637# Move the final file to NAS for Plex to pick up38mv $NEW_FILENAME $PLEX_DIR3940# housekeeping41rm temp.mp342rm $FILENAME43rm cover.*44cd -