YT-DLP Scripting

From Da Nerd Mage Wiki
Revision as of 15:37, 28 September 2025 by Tinker (talk | contribs) (→‎YTchan)
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 -e "~~~~~~~ " $0 $@ "~~~~~~~ "\\n

if [ $# -eq 0 ] || [ $1 = "-u" ] || [ $1 = "-?" ]; then
  echo -e USAGE: YTchan \[-s\] \[-c\] \[-r RATE\] 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.
  echo -e \\t -r RATE = Limit bandwidth
  echo -e \\t\\t Maximum download rate in bytes per second,
  echo -e \\t\\t\\t e.g. 50K or 4.2M
  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 The following \"ERROR\"s will also appear in 00000000-ERRORS
  echo -e \(Unfortunately, without indications of which files caused them...\)
  echo
  echo -e  \"ERROR: \[youtube:tab\] \@ChannelName: This channel does not have a videos tab\"
  echo -e  \"ERROR: \[youtube:tab\] \@ChannelName: This channel does not have a shorts tab\"
  echo -e  \"ERROR: \[youtube:tab\] \@ChannelName: This channel does not have a streams tab\"
  echo -e \\t Is simply indicating that the channel doesn\'t have that type of content.
  echo -e \\t \(All 3 means the channel doesn\'t actually have any content... :P \)
  echo
  echo -e \"ERROR: unable to download video data: HTTP Error 403: Forbidden\"
  echo -e \\t Means you may need to supply cookies.
  echo -e \\t Often caused by things like age restrictions.
  echo -e \\t May also be caused by YouTube noticing you\'ve been downloading a bunch...
  echo

  exit
fi

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

  SIMULATE=""
  WITHcookies=""
  LIMITrate=""

#echo ARGS: $@
for arg in "$@"
do
  if [ ${arg} = "-c" ]; then
    echo Using Cookies: $COOKIEfile
    WITHcookies="--cookies "$COOKIEfile
    shift
  elif [ ${arg} = "-s" ]; then
    echo Simulating
    SIMULATE="-s"
    shift
  elif [ ${arg} = "-r" ]; then
    shift
    LIMITrate="-r $1"
    echo Rate Limiting $LIMITrate
    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

echo ">>>>>>>> " $videosURL
      cd Videos
      yt-dlp $SIMULATE $LIMITrate $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 ..

echo ">>>>>>>> " $shortsURL
      cd Shorts
      yt-dlp $SIMULATE $LIMITrate $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 ..

echo ">>>>>>>> " $livesURL
      cd Streams
      yt-dlp $SIMULATE $LIMITrate $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