Youtube from the Command Line
This project is a script that allows you to search Youtube from the command line with certain parameters. It is possible to view Youtube as simply a library of videos that you can access however you want. I did this on Linux Mint DE, but it will work on any distribution.
The script is as follows:
#!/bin/bash
# Default values
QUERY=""
LIMIT=10
EXCLUDES=()
MIN_DURATION=""
PRINT_FMT="%(title)s | %(webpage_url)s"
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--query|-q)
QUERY="$2"
shift 2
;;
--limit|-n)
LIMIT="$2"
shift 2
;;
--exclude|-x)
EXCLUDES+=("$2")
shift 2
;;
--min-duration|-d)
MIN_DURATION="$2"
shift 2
;;
*)
QUERY="$QUERY $1"
shift
;;
esac
done
QUERY=$(echo "$QUERY" | sed 's/^ *//;s/ *$//')
if [[ -z "$QUERY" ]]; then
echo "Usage: ytsearch --query \"search terms\" [--exclude channel] [--min-duration seconds] [--limit N]"
exit 1
fi
# Build match-filter expression
FILTER=""
for CH in "${EXCLUDES[@]}"; do
FILTER="$FILTER & channel != '$CH'"
done
if [[ -n "$MIN_DURATION" ]]; then
FILTER="$FILTER & duration > $MIN_DURATION"
fi
FILTER="${FILTER# & }"
# Build base command
CMD=(yt-dlp "ytsearch${LIMIT}:${QUERY}" --quiet --no-warnings --print "$PRINT_FMT")
# Only add match-filter if it's non-empty
if [[ -n "$FILTER" ]]; then
CMD+=(--match-filter "$FILTER")
fi
# Run it
"${CMD[@]}"
Paste it into an appropriately named file — I called it ytsearch — and save it in the bin folder/directory; this will mean you can run it from anywhere. Don't forget to make it executable by running the following command: chmod +x ytsearch. With this done, you can run it from the command line as follows:
- ytsearch --query "something you want to find" — this is the simplest search.
- ytsearch --query "something you want to find" --exclude ChannelName — this allows you to exclude certain channels from the results (sometimes certain channels dominate and you might want to avoid them).
- ytsearch --query "something you want to find" --min-duration 300 — videos must be at least 5 minutes long.
- ytsearch --query "something you want to find" --limit 40 — allows you to change the default limit set by the script (10 in my case).
- ytsearch something you want to find — actually this is the simplest search. For example: ytsearch cry boy cry will return titles and URLs for Blue Zoo's 80s hit.
What inspired me to do this was a comment I saw on a Youtube video: "Whole video was spoilt by the amount of adverts", and I was thinking "What adverts?", but that's because I wasn't trapped in their app world. I mean, simply using the Brave browser would have spared them the adverts. I'm amazed at the way people just accept things as they are without investigating alternatives. The shift to smartphones has stopped people thinking about how they engage with the digital world.
Anyway, I digress. You're probaby wondering what you should do with the URLs you get back from your search. Well, the simple answer is you can paste them into any media player. That way you'll have managed the whole transaction without ever visiting the Youtube website. I simply type mpv followed by the URL at the command line and watch the video.