Skip to content

Instantly share code, notes, and snippets.

@codeniko
Last active July 20, 2022 19:38
Show Gist options
  • Save codeniko/1815c03fe2055ff30116 to your computer and use it in GitHub Desktop.
Save codeniko/1815c03fe2055ff30116 to your computer and use it in GitHub Desktop.
'Audio'get using youtube-dl. Like wget, except for extracting and saving audio from music/audio videos
#!/bin/bash
# DEPENDENCIES
# apt-get install ffmpeg id3v2
# install youtube-dl from github
# trim a filepath to only get filename, ${1} is filepath
function trimFilename
{
last=0
while :; do
i=$(/usr/bin/expr index "${1:${last}}" '/')
if [ "${i}" -eq 0 ]; then
break
else
last=$(/usr/bin/expr "${i}" + "${last}") # keep the indexes correct
fi
done
filename="${1:${last}}"
}
# ${1} is filepath
function createTags
{
filepath="${1}"
trimFilename "${filepath}"
prevIndex=0 # index of the previously found -
while :; do
# if $i == 0, char not found, else $i is index of char
i=$(/usr/bin/expr index "${filename:${prevIndex}}" '-')
if [ "${i}" -ne 0 ]; then
i=$(/usr/bin/expr "${i}" + "${prevIndex}") # keep the indexes correct
right="${i}" # index after - based on 0
# left=$(/usr/bin/expr "${i}" - 1) # minus one to get index starting from 0 for before -
left="${i}" # minus one to get index starting from 0 for before -
#remove space
if [ "${filename:${left}:1}" == ' ' ]; then
left=$(/usr/bin/expr "${left}" - 2)
else
left=$(/usr/bin/expr "${left}" - 1)
fi
if [ "${filename:${right}:1}" == ' ' ]; then
right=$(/usr/bin/expr "${right}" + 1)
fi
title="${filename:${right}: -4}" # right of - and trim .mp3 ext
artist="${filename:0:${left}}"
prevIndex="${right}"
echo -e "\nFile: \e[4;31;46m${artist}\e[0m\e[4;31m - \e[4;31;42m${title}\e[0m\e[4;31m.mp3\e[0m"
/bin/echo -e "Are these tags correct?"
/bin/echo -e "\e[1;34mArtist:\e[0m ${artist}"
/bin/echo -e "\e[1;32mTitle:\e[0m ${title}"
/bin/echo -n "y or n: "
read ans
if [ "${ans}" == 'y' ]; then
break
fi
else
/bin/echo -n 'Enter Artist: '
read artist
/bin/echo -n 'Enter Title: '
read title
break
fi
done
id3v2 -a "${artist}" -t "${title}" "${filepath}"
}
# dir where files will be stored
storePath="${HOME}/Downloads/Audio Downloads/"
# create directory where files will be stored if doesn't exist
if [ ! -d "${storePath}" ]; then
/bin/mkdir -v -p "${storePath}"
fi
if [ $# -eq 0 ]; then
/bin/echo "ERROR: Need atleast 1 URL."
exit 1
fi
error=0
for url in "$@"
do
bestVidFormat=$(/usr/local/bin/youtube-dl -F "$url" | /usr/bin/tail -n 1)
/bin/echo "Best format: ${bestVidFormat}"
bestVidFormat=$(/bin/echo "${bestVidFormat}" | cut -d ' ' -f 1)
/usr/local/bin/youtube-dl -x --no-playlist --audio-quality 0 --audio-format mp3 -o "%(title)s.%(ext)s" "$url"
#/usr/local/bin/youtube-dl --audio-quality 0 --audio-format best -o "%(title)s.%(ext)s" --max-quality "${bestVidFormat}" "$url"
if [ $? -gt 0 ]; then # error downloading, skip
error=1
continue
fi
# Name of actual file when downloaded, will be mp3
downloadedFileName=`/bin/ls --sort time --time ctime | /usr/bin/head -n 1`
# Path where file is downloaded, current directory when script was executed (assuming had permission to write to this dir)
downloadedPath=`/bin/pwd`
downloadedFullPath="${downloadedPath}/${downloadedFileName}"
# Just the file name, without an extension
/bin/mv "${downloadedFullPath}" "${storePath}"
createTags "${storePath}/$downloadedFileName"
done
if [[ ${error} -eq 1 ]]; then
echo "There has been an error with a download."
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment