Skip to content

Instantly share code, notes, and snippets.

@M41KL-N41TT
Created November 18, 2023 20:59
Show Gist options
  • Save M41KL-N41TT/97ef60ed2b46fdfe4ec933d23c08c631 to your computer and use it in GitHub Desktop.
Save M41KL-N41TT/97ef60ed2b46fdfe4ec933d23c08c631 to your computer and use it in GitHub Desktop.
youtube-dlp/yt-dlp search YouTube for string & then download COUNT number of showed results *concurrently* without *headless*
#!/bin/bash
# Define a variable for the search term, ensuring it's properly quoted
YTDLSEARCH="<search_term>"
COUNT=20
# Check if yt-dlp is installed
if ! command -v yt-dlp &> /dev/null; then
echo "yt-dlp could not be found, please install it."
exit 1
fi
# Fetch the video ids with the ytsearch term and construct the full URLs
# Save the URLs to urls.txt ensuring that the file is created fresh each time we run this
yt-dlp "ytsearch$COUNT:$YTDLSEARCH" --get-id -s | sed 's_^_https://www.youtube.com/watch?v=_' > urls.txt
# Check if urls.txt has content before attempting to download
if [ ! -s urls.txt ]; then
echo "No URLs found. Exiting."
exit 1
fi
# Download the best audio for each URL in urls.txt concurrently, using a maximum of 10 processes
xargs -P 4 -a urls.txt -I {} yt-dlp {}
# Note: If downloads fail, they will not stop the script; failed URLs can be retried later.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment