Difference between revisions of "YT-DLP Scripting"

From Da Nerd Mage Wiki
Jump to navigation Jump to search
Line 8: Line 8:
#!/bin/bash
#!/bin/bash


echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo -e "~~~~~~~ " $0 $@ "~~~~~~~ "\\n
echo $0 $@
 
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
if [ $# -eq 0 ] || [ $1 = "-u" ] || [ $1 = "-?" ]; then
echo
  echo -e USAGE: YTchan \[-s\] \[-c\] \[-r RATE\] ChannelName ChannelName ...
echo -e USAGE: YTchan [-s] [-c] ChannelName ChannelName ...
  echo
echo
  echo -e \\t -s = simulate this run
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 -c = use a cookie file exported from your browser to identify yourself to YouTube
  echo -e \\t -r RATE = Limit bandwidth
echo -e \\t ChannelName = The name of the channel \(as defined by YouTube\)
  echo -e \\t\\t Maximum download rate in bytes per second,
echo -e \\t\\t You can specify multiple channels
  echo -e \\t\\t\\t e.g. 50K or 4.2M
echo
  echo -e \\t ChannelName = The name of the channel \(as defined by YouTube\)
echo -e If you see \"HTTP Error 403: Forbidden\" errors in the output, you may need to supply cookies.
  echo -e \\t\\t You can specify multiple channels
echo -e \\t This may be caused by YouTube noticing you\'ve been downloading a bunch...
  echo
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


if [ $# -eq 0 ] || [ $1 = "-u" ]; then
   exit
   exit
fi
fi
Line 31: Line 43:
   SIMULATE=""
   SIMULATE=""
   WITHcookies=""
   WITHcookies=""
  LIMITrate=""


echo ARGS: $@
#echo ARGS: $@
for arg in "$@"
for arg in "$@"
do
do
   if [ ${arg} = "-c" ]; then
   if [ ${arg} = "-c" ]; then
     echo Using Cookies:
     echo Using Cookies: $COOKIEfile
     WITHcookies="--cookies "$COOKIEfile
     WITHcookies="--cookies "$COOKIEfile
    echo $WITHcookies
     shift
     shift
   elif [ ${arg} = "-s" ]; then
   elif [ ${arg} = "-s" ]; then
     echo Simulating
     echo Simulating
     SIMULATE="-s"
     SIMULATE="-s"
    shift
  elif [ ${arg} = "-r" ]; then
    shift
    LIMITrate="-r $1"
    echo Rate Limiting $LIMITrate
     shift
     shift
   else
   else
Line 61: Line 78:


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


Line 79: Line 96:
       cd $DEST
       cd $DEST


echo ">>>>>>>> " $videosURL
       cd Videos
       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)
       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 ..
       cd ..


echo ">>>>>>>> " $shortsURL
       cd Shorts
       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)
       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 ..
       cd ..


echo ">>>>>>>> " $livesURL
       cd Streams
       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)
       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 ..
       cd ..
     else
     else

Revision as of 15:36, 28 September 2025

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