Skip to content

Instantly share code, notes, and snippets.

@MauricioRobayo
Last active March 18, 2020 16:09
Show Gist options
  • Save MauricioRobayo/bbe4f6ce6ad3f7cc40d01fe3622b25e3 to your computer and use it in GitHub Desktop.
Save MauricioRobayo/bbe4f6ce6ad3f7cc40d01fe3622b25e3 to your computer and use it in GitHub Desktop.
YouTube to mp3 command line script
check_commands() {
local ok=0
for command in "$@";do
if ! command -v "${command}" >/dev/null 2>&1;then
echo "'${command}' is not installed!"
ok=1
fi
done
return "${ok}"
}
if ! check_commands sox mid3v2;then
return 1
fi
local mp3_file="$1"
local speed=${2:-1.25}
sox --show-progress "$mp3_file" "${mp3_file%.*}.x125.mp3" speed "$speed" # pitch -700
mid3v2 --song="${mp3_file%.*}_x125" "${mp3_file%.*}.x125.mp3"
check_commands() {
local ok=0
for command in "$@";do
if ! command -v "${command}" >/dev/null 2>&1;then
echo "'${command}' is not installed!"
ok=1
fi
done
return "${ok}"
}
if ! check_commands youtube-dl ffmpeg mid3v2;then
return 1
fi
tmp_file="$(mktemp)"
youtube-dl \
--extract-audio \
--audio-format mp3 \
--ignore-errors \
--output "%(title)s.%(ext)s" "$1" | tee "${tmp_file}"
grep -Po "(?<=\[ffmpeg\] Destination: ).*" "${tmp_file}"|while read -r mp3;do
mid3v2 --verbose --song="${mp3%.*}" "${mp3}"
done
rm "${tmp_file}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment