Bash Script: Automating yt-dlp

bash-scripts

yt-dlp is a powerful tool for downloading videos and audio from various websites. While the name implies YouTube, the tool can download videos from many more sites. When scripted in bash, yt-dlp can supplement streaming service content.

However, it is important to note that yt-dlp should not be used to download illegal or pirated content. Downloading copyrighted material without the copyright holder’s permission is illegal and can result in serious legal consequences.

If you are unsure whether or not a video or audio file is copyrighted, it is best to err on the side of caution and not download it. There are many legitimate sources of free and copyrighted content available online.

Here are some tips for avoiding copyright infringement when using yt-dlp:

  • Only download videos and audio files that you are authorized to download.
  • Be aware of the copyright laws in your country.
  • If you are unsure whether or not a video or audio file is copyrighted, do not download it.
  • There are many legitimate sources of free and copyrighted content available online.

The Scripts

These are two aliases I use to get videos and mp3 files.

alias ytmp3="yt-dlp -f140 -x --audio-format mp3 -o '%(title)s.%(ext)s'"
alias ytvid="yt-dlp -f mp4 -o '%(title)s.%(ext)s'"

Create Your Own Subscriptions

This script is used with a cronjob to download videos daily to my media server. The script downloads any video on the channel that has a timestamp between the date of execution and one week prior. When run a few times a day, this script works really well to keep the latest videos coming in. In addition, the second to last line will remove videos older than 14 days when the script runs. This is in an effort to not waste space.

#!/bin/bash
# Youtube Subscription Script
# This script will download videos uploaded to a specificed channel for the past # week from the time of execution.

# List of channels, a.k.a. subscriptions
channel1=https://URL-to-video-channel
channel2=https://URL-to-video-channel
channel3=https://URL-to-video-channel
channel4=https://URL-to-video-channel
channel5=https://URL-to-video-channel
channel6=https://URL-to-video-channel
channel7=https://URL-to-video-channel
channel8=https://URL-to-video-channel
channel9=https://URL-to-video-channel
channel10=https://URL-to-video-channel


# Commands to download the channel's videos

cd /path/to/youtube/downloads

yt-dlp --dateafter today-1week -f mp4 -o '%(title)s.%(ext)s' $channel1

yt-dlp --dateafter today-1week -f mp4 -o '%(title)s.%(ext)s' $channel2

yt-dlp --dateafter today-1week -f mp4 -o '%(title)s.%(ext)s' $channel3

yt-dlp --dateafter today-1week -f mp4 -o '%(title)s.%(ext)s' $channel4

yt-dlp --dateafter today-1week -f mp4 -o '%(title)s.%(ext)s' $channel5

yt-dlp --dateafter today-1week -f mp4 -o '%(title)s.%(ext)s' $channel6

yt-dlp --dateafter today-1week -f mp4 -o '%(title)s.%(ext)s' $channel7

yt-dlp --dateafter today-1week -f mp4 -o '%(title)s.%(ext)s' $channel8

yt-dlp --dateafter today-1week -f mp4 -o '%(title)s.%(ext)s' $channel9

yt-dlp --dateafter today-1week -f mp4 -o '%(title)s.%(ext)s' $channel10

cd

# This line will delete any file older than 14 days. This is to maintain
# disk space.

find /path/to/youtube/downloads -mtime +14 -exec rm {} \;

exit 0