Skip to content

Instantly share code, notes, and snippets.

@rsa16
Created October 29, 2022 00:41
Show Gist options
  • Save rsa16/dc2eb2d4e2143afec61f6a276e51417c to your computer and use it in GitHub Desktop.
Save rsa16/dc2eb2d4e2143afec61f6a276e51417c to your computer and use it in GitHub Desktop.
search youtube and download
#!/bin/bash
tempfile=$(mktemp)
youtube_dl_log=$(mktemp)
youtube-dl -j "ytsearch5:$*" > $tempfile
# workaround for lack of mapfile in bash < 4
# https://stackoverflow.com/a/41475317/6598435
while IFS= read -r line
do
youtube_urls+=("$line")
done < <(cat $tempfile | jq '.webpage_url' | tr -d '"' )
# # for bash >= 4
# mapfile -t youtube_urls < <(cat $tempfile | jq '.webpage_url' | tr -d '"' )
cat $tempfile | jq '.fulltitle, .webpage_url'
while :
do
echo "Enter video number to download."
read i
# don't download anything if you just press enter
if [ ! x"$i" == x"" ]
then
# to make numbering of videos more intuitive (start from 1 not 0)
youtube-dl -x --audio-format mp3 --no-progress ${youtube_urls[$i - 1]} &
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment