Often I'd be sent a song via YouTube that I liked and I wanted to add it to my iTunes. That process was always rather tedious, so I decided to create a little script that would download the YouTube video in the background, strip the audio as an mp3, and add it to iTunes. It worked really well and was incredibly useful, and I set it up to be triggered when I did a certain gesture on my trackpad (using BetterTouchTool). I only stopped using it when I switched to Spotify. I also made one that would download the YouTube video and then open it in VLC so that you could watch it fully buffered rather than waiting for it to buffer (relevant for my house, which only got 3mbps down, 1.5mbps up).

read more

mp3convert.sh

#!/bin/bash

#original idea: http://swarminglogic.com/article/2014_08_whylinux
#Tara suggested converting to mp3
#I wanted to add it to iTunes too

#requirements: must have ffmpeg, and must have ffmpeg and youtube-dl directories hardcoded below. 

#to install youtube-dl, do sudo pip install youtube-dl (or brew)
#to install ffmpeg, do brew install ffmpeg
#also seems like you need the up to date xcode

cd ~/Downloads/

say "One moment, please..."& #this just makes sure the user knows that they trigger the script. 

url="$(pbpaste)" #take in the clipboard

fileDownloadOutput="$(/usr/local/bin/youtube-dl --add-metadata --ffmpeg-location /usr/local/bin/ffmpeg -f 141/140 $url -o '%(title)s.%(ext)s')" #download the audio of the video (141 specifes what should be the high quality audio stream) and give it the file the video's title
fileName=`echo $fileDownloadOutput | awk -F\" '{print $(NF-1)}'` #parse the output for the filename, this is hacky because using the --get-filename param for youtube-dl messed up other things

open "$fileName" #opens the file/adds the file to iTunes
	

vlcopen.sh

#!/bin/bash

#original idea: http://swarminglogic.com/article/2014_08_whylinux
cd /tmp/
mkdir youtubeDL/
cd youtubeDL
rm myVideo.mp4

url="$(pbpaste)"
(/usr/local/bin/youtube-dl --restrict-filenames --no-part $url -o myVideo.mp4)& #for debugging: 2>~/Desktop/openVLCErrors.txt
processId=$!
echo "ProcessID: $processId"
echo "Waiting for file creation"

say "Let me open that in VLC for you" #this acts as a delay to let the download start

echo "Opening file..."
/Applications/Extra\ Applications/VLC.app/Contents/MacOS/VLC myVideo.mp4