Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- youtube-dl wrapper script
- =================================
- #!/bin/bash
- #requires .env: YTU_DEST
- # YTU_DEST=~/Videos or similar
- # YTU_DEST=/home/uc/Videos/ytuW
- # YTU_LOG=ytu.log
- # YTU_LIST=ytulist.txt
- # ie:
- # YTU_DEST=/home/uc/Downloads/ytu
- #
- # https://askubuntu.com/questions/438376/how-to-download-all-videos-on-a-youtube-channel
- # https://github.com/ytdl-org/youtube-dl/blob/master/README.md#options
- set_media_type () {
- echo "---------------------------------";
- echo "Current media type: $OPT_MEDIATYPE";
- echo " ----- Set media type ------ ";
- echo "---------------------------------";
- echo "V - VIDEO";
- echo "A - AUDIO";
- read -p "Select Option...: " OPT_AV
- echo "---------------------------------";
- echo "Selected $OPT_AV"
- # get video unless audio selected
- if [ "$OPT_AV" = "A" -o "$OPT_AV" = "a" ] ; then
- OPT_MEDIATYPE="-f best -x --audio-format "mp3" --audio-quality 0 --embed-thumbnail";
- #OPT_MEDIATYPE="-f 'bestaudio[ext=mp3]/bestaudio[ext=m4a]/bestaudio[ext=webm]/bestaudio'";
- OPT_ARCHIVE_FILE=$OPT_ARCHIVE_FILE_AUDIO;
- else
- OPT_MEDIATYPE="-f best";
- fi
- echo "Assigned Media Type: $OPT_MEDIATYPE";
- # output status before proceding
- #echo "---------------------------------";
- #echo "Current media type: ${OPT_MEDIATYPE}";
- #echo "Current URL: $URL -- $1"
- #echo "Current DATEAFTER: $OPTS_DATEAFTER -- $2"
- #echo "---------------------------------";
- }
- set_job_type () {
- # echo "---------------------------------------------------";
- # echo "URL is : $URL";
- # echo "DATEAFTER is : $OPTS_DATEAFTER";
- # echo " (OPTIONAL YYYYMMDD - example: 20191231 ) ";
- echo "----------------------------------------------------";
- echo "----------- SELECT JOB TYPE ------------------------";
- echo "----------------------------------------------------";
- echo "1 - ONE URL NOT ALL PLAYLIST"; # https://www.youtube.com/watch?v=hECs8372M8A
- echo "2 - PLAYLIST ALL BY SINGLE URL - ( get entire playlist )";
- echo "----------------------------------------------------";
- echo "3 - PLAYLIST PAGE ALL";
- echo "4 - VIDEOS PAGE ALL";
- echo "5 - CHANNEL ALL";
- echo "---------------------------------------------------";
- echo "6 - Read LIST - of SINGLE URLS from list $YTU_LIST";
- echo "7 - View LOG: - $YTU_LOG";
- echo "8 - READ URL and DATEAFTER";
- echo "----------------------------------------------------";
- echo " 0 - EXIT";
- read -p "Select option and press ENTER or CTRL-C to ABORT: " OPTS
- OPT_JOB_TYPE="$OPTS"
- echo "----------------------------------------------------"
- echo "----------------------------------------------------"
- echo " ENTERED : $OPT_JOB_TYPE";
- #echo " TARGET URL: $URL";
- #echo " DATEAFTER SET TO: _ $OPTS_DATEAFTER _";
- #echo "----------------------------------------------------";
- }
- set_list_option () {
- echo "URL: $URL";
- if [ "$URL" != "" ] ; then
- OPT_USE_LIST="N";
- else
- read_list_option
- fi
- }
- read_list_option () {
- echo "---------------------------------";
- echo "Current List Setting : $OPT_USE_LIST";
- echo "Current List Location: $YTU_LIST";
- echo "----- READ from LIST? Y/N ------ ";
- echo "---------------------------------";
- read -p "Select Option...: " RESP
- echo "Selected: $RESP"
- echo "---------------------------------";
- # get video unless audio selected
- if [ "$RESP" = "Y" -o "$RESP" = "y" ] ; then
- OPT_USE_LIST="Y";
- else
- OPT_USE_LIST="N";
- set_url_date
- fi
- }
- set_url_date () {
- echo "Read URL and DATEAFTER"
- read -p "Type...>" URL RESP
- if [ "$RESP" = "" ] ; then
- OPTS_DATEAFTER=
- else
- OPTS_DATEAFTER="--dateafter ${RESP}"
- fi
- #if [ "$URL" = "" ] ; then
- # echo "URL is empty, retry.."
- # set_url_date
- #fi
- # give a change to enter URL if empty
- #if [ "$URL" = "" ]; then
- # set_url_date
- #fi
- }
- ytuExecute() {
- case "$OPT_JOB_TYPE" in
- "1") echo "Geting single URL ( dont get entire playlist )"; # https://www.youtube.com/watch?v=hECs8372M8A
- OPT_PLAYLIST_YESNO="$OPT_PLAYLIST_NO";
- if [ "$OPT_AV" = "A" -o "$OPT_AV" = "a" ] ; then
- OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_SINGLE_AUDIO"
- else
- OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_SINGLE_VIDEO"
- fi
- OPT_JOB_SPECIFIC=
- run_command #"$URL" "$OPTS_DATEAFTER"
- ;;
- "2") echo "Geting single URL ( get entire playlist )"; # https://www.youtube.com/watch?v=QYrjvLFT_B4&list=PLUjQ3tZwzZPG3tA6BjnjhAJVnZ8n4smUQ&index=4
- OPT_PLAYLIST_YESNO="$OPT_PLAYLIST_YES";
- if [ "$OPT_AV" = "A" -o "$OPT_AV" = "a" ] ; then
- OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_PLAYLIST_AUDIO"
- else
- OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_PLAYLIST_VIDEO"
- fi
- OPT_JOB_SPECIFIC=
- run_command
- ;;
- "3") echo "Geting playlist by URL"; # https://www.youtube.com/playlist?list=PLUjQ3tZwzZPG3tA6BjnjhAJVnZ8n4smUQ
- OPT_PLAYLIST_YESNO="$OPT_PLAYLIST_YES";
- if [ "$OPT_AV" = "A" -o "$OPT_AV" = "a" ] ; then
- OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_PLAYLIST_AUDIO"
- else
- OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_PLAYLIST_VIDEO"
- fi
- OPT_JOB_SPECIFIC=
- run_command "$URL"
- ;;
- "4") echo "Geting all videos by URL"; # https://www.youtube.com/channel/UC8butISFwT-Wl7EV0hUK0BQ/videos
- OPT_PLAYLIST_YESNO="$OPT_PLAYLIST_NO";
- if [ "$OPT_AV" = "A" -o "$OPT_AV" = "a" ] ; then
- OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_SINGLE_AUDIO"
- else
- OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_SINGLE_VIDEO"
- fi
- OPT_JOB_SPECIFIC=
- run_command "$URL"
- ;;
- "5") echo "Geting entire channel by URL"; # https://www.youtube.com/channel/UCIJGI_3XgnfUaSNQD8D2IMQ
- OPT_PLAYLIST_YESNO="$OPT_PLAYLIST_NO";
- if [ "$OPT_AV" = "A" -o "$OPT_AV" = "a" ] ; then
- OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_SINGLE_AUDIO"
- else
- OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_SINGLE_VIDEO"
- fi
- OPT_JOB_SPECIFIC=
- run_command "$URL"
- ;;
- "6") echo "Getting download urls from list: $YTU_LIST";
- OPT_PLAYLIST_YESNO="$OPT_PLAYLIST_NO";
- if [ "$OPT_AV" = "A" -o "$OPT_AV" = "a" ] ; then
- OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_SINGLE_AUDIO"
- else
- OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_SINGLE_VIDEO"
- fi
- OPT_JOB_SPECIFIC="$OPT_BATCH_FILE"
- run_command "$URL"
- ;;
- "7")
- echo "-------------------------------------------------"
- echo "REVIEW LOGFILE: ${YTU_LOG}"
- echo "Press ENTER or CTRL-C to ABORT"
- echo "-------------------------------------------------"
- read -p ">" pausing
- OPT_JOB_SPECIFIC=
- viewlog
- ;;
- "8") echo "Read URL and DATEAFTER"
- read -p "Type...>" URL RESP
- if [ "$RESP" = "" ] ; then
- OPTS_DATEAFTER=
- else
- OPTS_DATEAFTER="--dateafter ${RESP}"
- fi
- if [ "$URL" = "" ] ; then
- echo "URL is empty, exiting.."
- exit 1
- fi
- OPT_JOB_SPECIFIC=
- run_command "${URL}"
- ;;
- "0") exit 0;
- ;;
- *)
- ;;
- esac;
- # write DONE msg to log
- annotate_file_dldone
- set_list_option
- }
- function exiting () {
- echo "DONE, EXITING NOW.....";
- exiting
- }
- run_command () {
- read -p "READY TO START???? Press ENTER or CTRL-C to ABORT" pausing
- OPTS_STATIC="$OPT_CONSOLE_TITLE $OPT_VERBOSE $OPT_MEDIATYPE $OPT_WRITETHUMBNAIL $OPT_WRITE_DESCRIPTION $OPT_WRITE_INFO_JSON $OPT_IGNORE_ERRORS $OPT_NO_OVERWRITES $OPT_RESTRICT_FILENAMES $OPT_ARCHIVE_FILE";
- OPTS_DYNAMIC="$OPT_JOB_SPECIFIC $OPT_OUTPUT_FILE $OPTS_DATEAFTER";
- if [ "$OPT_USE_LIST" = "Y" -o "$OPT_USE_LIST" = "y" ] ; then
- echo "updating option string: BEFORE: $OPTS_DYNAMIC"
- OPTS_DYNAMIC="$OPTS_DYNAMIC $OPT_BATCH_FILE"
- echo "updating option string: AFTER : $OPTS_DYNAMIC"
- else
- echo "NOT using list"
- fi
- # echo "RUNNING: youtube-dl $OPTS_ALL $OPTS_JOB $URL";
- CMD="youtube-dl $OPTS_STATIC $OPTS_DYNAMIC $URL"
- echo "COMMAND STRING: $CMD"
- $CMD >> ytu.log 2>&1 & tail -f ytu.log
- }
- #run_command () { # $URL, $OPTS_ALL, $OPTS_JOB
- # OPTS_ALL="--console-title --verbose -f $OPT_MEDIATYPE --write-thumbnail --write-description --write-info-json --ignore-errors --no-overwrites --restrict-filenames $OPTS_DATEAFTER";
- #
- # # echo "RUNNING: youtube-dl $OPTS_ALL $OPTS_JOB $URL";
- # CMD="youtube-dl $OPTS_ALL $OPTS_JOB $OPTS_DATEAFTER $URL"
- # echo "COMMAND STRING: $CMD"
- # $CMD
- #}
- exiting () {
- echo "EXITING.....";
- exit 0;
- }
- annotate_file_dldone () {
- local MESSAGE="
- ############################################################################
- #### DOWNLOADS COMPLETE
- #### ${JOB} ON : ${TIMESTAMP}
- ############################################################################
- "
- echo "$MESSAGE" >> ytu.log
- }
- viewlog () {
- less ${YTU_LOG}
- echo "EXITING.....";
- }
- move_mp3 () {
- # NOT NEEDED, UNLESS THE FILENAME CONTAINS SPACES & SPECIALS CHARS THAT INDUCE THE '<FILENAME>' QUOTE WRAPPING BY LINUX OS
- if [ "$OPT_AV" = "A" -o "$OPT_AV" = "a" ] ; then
- mv *.mp3 "$YTU_DEST_MP3"
- fi
- }
- # https://www.youtube.com/watch?v=nO9N0TgAblE
- echo "######################### INIT #########################################";
- # export .env vars & clear screen
- export $(grep -v '^#' /home/uc/s/.env | xargs)
- #export $(grep -v '^#' /home/silosix/s/.env | xargs)
- clear
- URL="$1";
- echo "URL PASSED: _ $URL _";
- # set date if entered & valid ( add len check )
- if [ "$2" != "" ]; then
- OPTS_DATEAFTER="--dateafter $2"; #YYYYMMDD OR now/#days/#months/#years
- else
- OPTS_DATEAFTER= # ASSIGN TO EMPTY SO IT WON'T BE USED
- fi
- echo "------------------------"
- echo "PASSED PARAMS: "
- echo "------------------------"
- echo "1-URL : $URL"
- echo "2-OPTS_DATEAFTER: $OPTS_DATEAFTER"
- echo "------------------------"
- echo "######################### CHECK FOR VALID DIRS #########################################";
- # init YTU_DEST, YTU_LOG, YTU_LIST
- if [ -z "$YTU_DEST" ] ; then
- echo "Current Destination Directory is empty: $YTU_DEST"
- YTU_DEST="~/Videos"
- echo "using the default dir: $YTU_DEST"
- fi
- if [ ! -d "$YTU_DEST" ] ; then
- echo "Current Destination Directory is invalid: $YTU_DEST"
- YTU_DEST="~/Videos"
- echo "using the default dir: $YTU_DEST"
- fi
- if [ ! -d "$YTU_DEST" ] ; then
- echo "Default Destination dir is invalid also exiting: $YTU_DEST"
- exit 1;
- fi
- if [ ! -z "$YTU_LOG" ] ; then
- YTU_LOG="$YTU_DEST/ytu.log"
- touch "$YTU_LOG"
- fi
- # not needed if filename is 'cleaned' with correct youtube-dl option
- YTU_DEST_MP3="$YTU_DEST/mp3"
- # check /media/media/ytu/ytu-list.txt.. THEN use local as backup
- if [ -z "$YTU_DEST/ytu-list.txt" ] ; then
- YTU_LIST="$YTU_DEST/ytu-list.txt"
- else
- YTU_LIST="$dir_git/ytu/ytu-list.txt"
- fi
- echo "------------------------"
- echo ".CONFIG PARAMS in .env";
- echo "------------------------"
- echo "SCRIPT_DIR: $SCRIPT_DIR"
- echo "YTU_DEST : $YTU_DEST"
- echo "YTU_LOG : $YTU_LOG"
- echo "YTU_LIST : $YTU_LIST"
- echo "------------------------"
- echo "######################### STATIC VARS #########################################";
- # STATIC VARS - ALWAYS USED
- OPT_CONSOLE_TITLE="--console-title"
- OPT_VERBOSE="--verbose"
- OPT_WRITETHUMBNAIL="--write-thumbnail"
- OPT_WRITE_DESCRIPTION="--write-description"
- OPT_WRITE_INFO_JSON="--write-info-json"
- OPT_IGNORE_ERRORS="--ignore-errors"
- OPT_NO_OVERWRITES="--no-overwrites"
- OPT_RESTRICT_FILENAMES="--restrict-filenames"
- OPT_ARCHIVE_FILE_VIDEO="--download-archive archive-video.txt"
- OPT_ARCHIVE_FILE_AUDIO="--download-archive archive-audio.txt"
- OPT_ARCHIVE_FILE="$OPT_ARCHIVE_FILE_VIDEO"
- OPT_BATCH_FILE="--batch-file $YTU_LIST"
- echo "######################### DYNAMIC VARS ########################################";
- # DYNAMIC VARS
- OPT_JOB_TYPE=
- OPT_PLAYLIST_YESNO=
- OPT_AV="V"
- OPT_MEDIATYPE="-f best"
- OPT_OUTPUT_FILE=
- OPT_JOB_SPECIFIC=
- OPT_USE_LIST="N"
- # set in function()
- OPT_PLAYLIST_NO="--no-playlist";
- OPT_OUTPUT_FILE_SINGLE_VIDEO="--output $YTU_DEST/%(uploader)s/%(title)s.%(ext)s";
- OPT_OUTPUT_FILE_SINGLE_AUDIO="--output $YTU_DEST/%(uploader)s/audio/%(title)s-audio.%(ext)s";
- OPT_PLAYLIST_YES="--yes-playlist"
- OPT_OUTPUT_FILE_PLAYLIST_VIDEO="--output $YTU_DEST/%(uploader)s/%(playlist)s/%(playlist_index)s-%(title)s.%(ext)s";
- OPT_OUTPUT_FILE_PLAYLIST_AUDIO="--output $YTU_DEST/audio/%(uploader)s/%(playlist)s/%(playlist_index)s-%(title)s.%(ext)s";
- # AGGREGATE ALL VARS TO THESE THEN CALL YTDL
- OPTS_DYNAMIC=
- OPTS_ALL=
- # YOUTUBE-DL OPTION INIT PER JOB
- JOB_SINGLEPLAYLIST_NO=
- JOB_SINGLEPLAYLIST_YES=
- JOB_PLAYLIST_ALL=
- JOB_VIDEOS_ALL=
- JOB_CHANNEL_ALL=
- echo "######################### CALL #####################################################";
- # 1ST PARAM = URL
- # 2ND PARAM = DATEAFTER ( 20191231 )
- set_list_option
- set_media_type "$URL" $2
- set_job_type
- ytuExecute
- echo "######################### END #####################################################";
- exit 0;
- # SPARE OPTIONS
- # --config-location PATH
- # --playlist-reverse
- # --download-archive FILE
- # --batch-file FILE
- # --console-title
- #"$OPT_CONSOLE_TITLE"
- #"$OPT_VERBOSE"
- #"$OPT_MEDIATYPE"
- #"$OPT_WRITETHUMBNAIL"
- #"$OPT_WRITE_DESCRIPTION"
- #"$OPT_WRITE_INFO_JSON"
- #"$OPT_IGNORE_ERRORS"
- #"$OPT_NO_OVERWRITES"
- #"$OPT_RESTRICT_FILENAMES"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement