YT-DLP Scripting

From Da Nerd Mage Wiki
Revision as of 22:14, 26 September 2025 by Tinker (talk | contribs)
Jump to navigation Jump to search

YTchan

A script for downloading entire channels...

(assumes your base for downloading YouTube videos is at: /mnt/Download_Space/YTDL/)

#!/bin/bash

echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo $0 $@
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo
echo -e USAGE: YTchan [-s] [-c] ChannelName ChannelName ...
echo
echo -e \\t -s = simulate this run
echo -e \\t -c = use a cookie file exported from your browser to identify yourself to YouTube
echo -e \\t ChannelName = The name of the channel \(as defined by YouTube\)
echo -e \\t\\t You can specify multiple channels
echo
echo -e If you see \"HTTP Error 403: Forbidden\" errors in the output, you may need to supply cookies.
echo -e \\t This may be caused by YouTube noticing you\'ve been downloading a bunch...
echo

if [ $# -eq 0 ] {{!}}{{!}} [ $1 = "-u" ]; then
  exit
fi

COOKIEfile=/mnt/Download_Space/YTDL/www.youtube.com_cookies.txt

  SIMULATE=""
  WITHcookies=""

echo ARGS: $@
for arg in "$@"
do
  if [ ${arg} = "-c" ]; then
    echo Using Cookies:
    WITHcookies="--cookies "$COOKIEfile
    echo $WITHcookies
    shift
  elif [ ${arg} = "-s" ]; then
    echo Simulating
    SIMULATE="-s"
    shift
  else
    CHANNEL=$1
    shift
    echo
    echo Working on channel: $CHANNEL
    echo ========================================
    if wget --spider --quiet https://www.youtube.com/\@$CHANNEL > /dev/null 2>&1; then
      videosURL=https://www.youtube.com/\@$CHANNEL/videos
      shortsURL=https://www.youtube.com/\@$CHANNEL/shorts
      livesURL=https://www.youtube.com/\@$CHANNEL/streams

      DEST=/mnt/Download_Space/YTDL/$CHANNEL

      ERRfile=$DEST/00000000-ERRORS
      ARCHfile=$DEST/11111111-ARCHIVE

      echo
      echo Channel: $CHANNEL
      echo $videosURL
      echo $shortsURL
      echo $livesURL
      echo $DEST
      echo

      if [ -d $DEST ]; then
        echo $DEST exists
      else
        echo no $DEST... Building it...
        mkdir $DEST $DEST/Videos $DEST/Shorts $DEST/Streams
      fi

      echo

      cd $DEST

      cd Videos
      yt-dlp $SIMULATE $WITHcookies -w --download-archive $ARCHfile --write-description -t mp4 -o "%(upload_date)s - %(title)s.%(ext)s" $videosURL 2> >(/usr/bin/tee -a $ERRfile)
      cd ..

      cd Shorts
      yt-dlp $SIMULATE $WITHcookies -w --download-archive $ARCHfile --write-description -t mp4 -o "%(upload_date)s - %(title)s.%(ext)s" $shortsURL 2> >(/usr/bin/tee -a $ERRfile)
      cd ..

      cd Streams
      yt-dlp $SIMULATE $WITHcookies -w --download-archive $ARCHfile --write-description -t mp4 -o "%(upload_date)s - %(title)s.%(ext)s" $livesURL 2> >(/usr/bin/tee -a $ERRfile)
      cd ..
    else
      echo YouTube says this channel does not exist.
    fi   
  fi
done