Skip to content

Instantly share code, notes, and snippets.

@connerturmon
Created April 3, 2018 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save connerturmon/a255a3f5d3a0c46d49ea26f94c156d1b to your computer and use it in GitHub Desktop.
Save connerturmon/a255a3f5d3a0c46d49ea26f94c156d1b to your computer and use it in GitHub Desktop.
Script to rip audio from YouTube URLs via youtube-dl
#!/bin/bash
### ARGUMENTS:
# $1 : URL to YouTube Video
# $2 : (Optional) Folder in $HOME to place .mp3 file
## ytaud - This script downloads high quality .mp3 audio from a YouTube URL ($1) and places it in the user's ~/Music/ folder or $HOME/$2 folder.
## The quality by default is maximum (320kbps?) and I will likely add support for changing this later.
# Color for output
RED='\033[0;32m'
NC='\033[0m'
# Show script startup (for better visualization in terminal history)
echo -e "\n${RED}Beginning YouTube .mp3 rip${NC}"
# Check to see if the user wants to place the .mp3 in a specific directory
if [[ -z "$2" ]]; then
# If not, download the .mp3 to the user's Music folder (if it exists; if not, create it)
if [[ -d $HOME/Music/ ]]; then
youtube-dl -x --audio-format mp3 --audio-quality 0 --output "$HOME/Music/%(title)s.%(ext)s" $1
echo -e "${RED}Successfully downloaded .mp3 file to $HOME/Music/${NC}\n"
else
# Create user's Music directory if it does not exist
echo -e "${RED}No Music directory in HOME folder, creating one now...${NC}\n"
mkdir $HOME/Music
youtube-dl -x --audio-format mp3 --audio-quality 0 --output "$HOME/Music/%(title)s.%(ext)s" $1
echo -e "${RED}Successfully created and downloaded .mp3 to $HOME/Music/${NC}\n"
fi
else
# If user-requested directory exists, download to there
if [[ -d $2 ]]; then
youtube-dl -x --audio-format mp3 --audio-quality 0 --output "$HOME/$2/%(title)s.%(ext)s" $1
echo -e "${RED}Successfully downloaded .mp3 file to $HOME/$2/${NC}\n"
else
# Create the user-requested directory if it does not exist, and then download to there
echo -e "${RED}Directory $HOME/$2/ does not exist... Creating now...${NC}"
mkdir $HOME/$2
youtube-dl -x --audio-format mp3 --audio-quality 0 --output "$HOME/$2/%(title)s.%(ext)s" $1
echo -e "${RED}Successfully created and downloaded .mp3 to $HOME/$2/${NC}\n"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment