Bash 스크립트: 유튜브 비디오 다운로더

 

 요구사항

brew install dialog youtube-dl in macOS

 

 

 Youtube-Download.sh


#!/bin/bash

VIDEO_URL="$1"
VIDEO_FORMAT_LIST="/tmp/video-format.list.$$"
MODE_LIST="/tmp/video-mode.list.$$"
SELECT_FORMAT_CMD="/tmp/select-mode-cmd.$$"
RC="/tmp/rc.$$"
FETCH_AVAILABLE_MODES_CMD="/tmp/fetch-modes.sh.$$"
TARGET="/tmp/selected.list.$$"
HEIGHT=23
WIDTH=60

function clean_up {
	dialog --clear
	rm -f $RC
	rm -f $MODE_LIST
	rm -f $SELECT_FORMAT_CMD
	rm -f $VIDEO_FORMAT_LIST
	rm -f $FETCH_AVAILABLE_MODES_CMD
  rm -f $TARGET
	exit 0
}
trap clean_up SIGHUP SIGINT SIGTERM

dialog --inputbox "Youtube Video URL to download (eg. https://youtu.be/20weXeFOwHs)" 10 70 "" 2> $RC
read VIDEO_URL < $RC
if [[ "$VIDEO_URL" =~ "https://youtu.be/" ]]; then
	echo -n $VIDEO_URL
else
	dialog --clear
	dialog --infobox "URL dosen't seem to be a valid one\n\n $VIDEO_URL" $HEIGHT $WIDTH
	clean_up
fi

echo "#!/bin/bash
youtube-dl -F '$VIDEO_URL' 2> /dev/null | tee $VIDEO_FORMAT_LIST
" >> $FETCH_AVAILABLE_MODES_CMD
chmod +x $FETCH_AVAILABLE_MODES_CMD

dialog --prgbox "Fetching list information of available videos ..." $FETCH_AVAILABLE_MODES_CMD 25 60

perl -pi -e 's/[ ]+/ /g' $VIDEO_FORMAT_LIST
perl -pi -e 's/ , /, /g' $VIDEO_FORMAT_LIST
perl -pi -e 's/^(\d+)\s+/$1:/g' $VIDEO_FORMAT_LIST

touch $MODE_LIST
while read line; do echo "$line" | grep '^[0-9]' | awk -F':' '{print $1,"\""$2"\"", "off"}' >> $MODE_LIST ; done < $VIDEO_FORMAT_LIST
#perl -pi -e 's/\]"$/\]" off/' $MODE_LIST

echo "dialog --checklist \"Youtube downloader by A.J\nWhich Format of ${VIDEO_URL} ?\" 25 100 15 `cat $MODE_LIST | tr '\n' ' '`" >> $SELECT_FORMAT_CMD

. $SELECT_FORMAT_CMD 2> $TARGET

if [[ "`cat $TARGET`" == "" ]]; then
	echo "Canceled."
	clean_up
fi
perl -pi -e 's/\s+/,/g' $TARGET

cat $TARGET

echo "#!/bin/bash" > $FETCH_AVAILABLE_MODES_CMD
echo "youtube-dl --id -f `cat $TARGET` '$VIDEO_URL'" >> $FETCH_AVAILABLE_MODES_CMD
dialog --prgbox "Downloading format of `$TARGET`" $FETCH_AVAILABLE_MODES_CMD 25 60

clean_up