youtube-dl: download shell script ( DRAF )

YouTube-DL Wrapper Shell Script

 

# ALL # SINGLE
youtube-dl -no-playlist --verbose -f bestvideo \
--write-thumbnail --write-description --write-info-json --no-overwrites --restrict-filenames \
--output '/home/uc/Videos/ytu/%(uploader)s/%(title)s.%(ext)s' \
https://www.youtube.com/watch?v=jfM6cOtqBHE

 

# Playlist
youtube-dl --yes-playlist --verbose -f bestvideo \
--write-thumbnail --write-description --write-info-json --no-overwrites --restrict-filenames \
--output '/home/uc/Videos/ytu/%(uploader)s/%(playlist)s/%(playlist_index)s-%(title)s.%(ext)s' \
https://www.youtube.com/watch?v=jfM6cOtqBHE

 

# SINGLE
youtube-dl --no-playlist --verbose -f bestvideo \
--write-thumbnail --write-description --write-info-json --no-overwrites --restrict-filenames \
--output '/home/uc/Videos/ytu/%(uploader)s/%(title)s.%(ext)s' \
https://www.youtube.com/watch?v=jfM6cOtqBHE

 

# After
--dateafter now-6months
# Download only the videos uploaded in the 200x decade
youtube-dl --dateafter 20000101 --datebefore 20091231

 

youtube-dl --verbose -f bestvideo --dump-json --write-thumbnail --write-description --write-info-json --no-overwrites --restrict-filenames --output '/home/uc/Videos/ytu/%(uploader)s/%(title)s.%(ext)s' https://www.youtube.com/watch?v=jfM6cOtqBHE

 

 

 

Videos can be filtered by their upload date using the options --date, --datebefore or --dateafter. They accept dates in two formats:

  • Absolute dates: Dates in the format YYYYMMDD.
  • Relative dates: Dates in the format (now|today)[+-][0-9](day|week|month|year)(s)?
--dateafter now-6months

$ # Download only the videos uploaded in the 200x decade
$ youtube-dl --dateafter 20000101 --datebefore 20091231

 


Full 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

ytuExecute() {
    
    echo "URL is       : $1";
    echo "DATEBEFORE is: $2";

    opts_all="--console-title --verbose -f bestvideo --write-thumbnail --write-description --write-info-json --ignore-errors --no-overwrites --restrict-filenames $OPTS_DATEAFTER";

    job_singleplaylist_no="--no-playlist --output $YTU_DEST/%(uploader)s/%(title)s.%(ext)s";
    job_singleplaylist_yes="--yes-playlist --output $YTU_DEST/%(uploader)s/%(playlist)s/%(playlist_index)s-%(title)s.%(ext)s";

    job_playlistall="--yes-playlist --output $YTU_DEST/%(uploader)s/%(playlist)s/%(playlist_index)s-%(title)s.%(ext)s";
    job_videosall="--no-playlist --output $YTU_DEST/%(uploader)s/%(title)s.%(ext)s";
    job_channel_all="--no-playlist --output $YTU_DEST/%(uploader)s/%(title)s.%(ext)s";    

    # SPARE OPTIONS    
    # --config-location PATH
    # --playlist-reverse
    # --download-archive FILE
    # --batch-file FILE
    # --console-title
    
    echo "Testing destination dir -" $YTU_DEST
    cd $YTU_DEST
    
    echo "1 - Geting single URL ( don't get entire playlist )";  # https://www.youtube.com/watch?v=hECs8372M8A
    echo "2 - Geting single URL ( get entire playlist )";
    echo "----------------------------------------------------";
    echo "3 - Geting playlist by URL";
    echo "4 - Geting all videos by URL";
    echo "5 - Geting all channel by URL";
    echo "6 - Geting single urls from list $YTU_LIST";
    echo "7 - View Log: $YTU_LOG";  
    echo "----------------------------------------------------";
    echo " 0 - EXIT";
    read -p "Select option and press ENTER or CTRL-C to ABORT:  " OPTS
    echo "----------------------------------------------------"
    echo "----------------------------------------------------"
    echo " ENTERED: $OPTS";    
    echo " TARGET URL: $URL";    
    echo "----------------------------------------------------";    
    read -p "Ready? Press ENTER or CTRL-C to ABORT" pausing
    
    opts_job=""; # init
    
    case "$OPTS" in
    "1") echo "Geting single URL ( dont get entire playlist )"; # https://www.youtube.com/watch?v=hECs8372M8A
        opts_job="$job_singleplaylist_no";
        ;;
    "2") echo "Geting single URL ( get entire playlist )";      # https://www.youtube.com/watch?v=QYrjvLFT_B4&list=PLUjQ3tZwzZPG3tA6BjnjhAJVnZ8n4smUQ&index=4
        opts_job="$job_singleplaylist_yes";
        ;;        
    "3") echo "Geting playlist by URL";     # https://www.youtube.com/playlist?list=PLUjQ3tZwzZPG3tA6BjnjhAJVnZ8n4smUQ
        opts_job="$job_playlistall";
        ;;
    "4") echo "Geting all videos by URL";   # https://www.youtube.com/channel/UC8butISFwT-Wl7EV0hUK0BQ/videos
        opts_job="$job_videosall";
        ;;
    "5") echo "Geting entire channel by URL";  # https://www.youtube.com/channel/UCIJGI_3XgnfUaSNQD8D2IMQ
        opts_job="$job_channel_all";
        ;;
    "6") opts_job="$job_singleplaylist_no --batch-file ${YTU_LIST}";
        ;;
    "7")
        echo "-------------------------------------------------"
        echo "REVIEW LOGFILE: ${YTU_LOG}"
        echo "Press ENTER or CTRL-C to ABORT"
        echo "-------------------------------------------------"
        read -p ">" pausing
        viewlog
        ;;

    "0")
        ;;
    esac;
    
    echo "RUNNING: youtube-dl $opts_all $opts_job $URL";
    cmd="youtube-dl $opts_all $opts_job --dateafter 20150824 $1"
    $cmd
    
    echo "DONE, EXITING NOW.....";
    exiting
}

exiting () {
    echo "EXITING.....";
    exit 0;
}

viewlog () {
    less ${YTU_LOG}
    echo "EXITING.....";
}

echo "######################### INIT #####################################################";
# export .env vars & clear screen
export $(grep -v '^#' /home/uc/s/.env | xargs)
clear

echo "PASSED PARAMS: "
echo "1-URL           : "${1}
echo "2-OPTS_DATEAFTER: "${2}

URL="$1";

if [ "$2" != "" ]; then
    OPTS_DATEAFTER="--datebefore $2";
else
    OPTS_DATEAFTER="";
fi
    
# init YTU_DEST, YTU_LOG, YTU_LIST
if [ "$YTU_DEST" == "" ] ; then
    YTU_DEST="~/Videos"
fi

if [ "$YTU_LOG" == "" ] ; then
    YTU_LOG="$YTU_DEST/ytu.log"
fi

if [ "$YTU_LIST" == "" ] ; then
    YTU_LIST="$YTU_LIST/ytulist.txt"
fi

# for cd to script dir for access to php classes
echo "DEST: $YTU_DEST"
SCRIPT_DIR=~/s/ytu
cd "$SCRIPT_DIR"

echo "######################### CALL #####################################################";
#menu "$1" # OLD LOGIC

# 1ST PARAM = URL
# 2ND PARAM = DATEAFTER ( 20191231 )
ytuExecute $1 $2

echo "######################### END #####################################################";
exit 0;