r/termux • • 11h ago

Showcase YT-DLP for Termux

I made a little script that lets you use YT-DLP to download, MP4 or MP3 videos from Youtube using Termux. It automatically detects a link in your paperclip (you can also write/paste it manually). You need to have installed Termux, Termux:API and of course yt-dlp.

I personally have installed Termux:Widget and use it with a widget on my homescreen, but you can also use it like a normal script from a terminal.

IMPORTANT
You need to change the directory variable to the one that you want. If you don´t want to change the script and use it as is, you can just create a new folder in your root called "yt-dlp-downloads".

Script:

#!/data/data/com.termux/files/usr/bin/bash

# Detect link
DIRECTORY=~/storage/shared/yt-dlp-downloads
MODE=${1:-0}   # 0 = single video, 1 = playlist

# 1) Get link: clipboard first, dialog as fallback
URL=$(termux-clipboard-get)
if [[ "$URL" != http* ]]; then
  URL=$(termux-dialog text -t "yt-dlp" -i "Paste the link" | jq -r '.text')
fi

# Check if link is empty
if [ -z "$URL" ] || [ "$URL" = "null" ]; then
  echo "No link provided."
  read -p "Press Enter to close..."
  exit 1
fi

# Validate link
if [[ "$URL" != http* ]]; then
  echo "Invalid link. Does not contain http."
  read -p "Press Enter to close..."
  exit 1
fi

# Detect playlist
if [[ "$URL" == *youtube.com/playlist* ]]; then
  echo "Detected playlist link, downloading the full playlist."
  MODE=1
else
  echo "Detected single video link, downloading."
fi

# 2) Format
FORMAT=$(termux-dialog radio -t "Format" -v "MP4 (video),MP3 (audio)" | jq -r '.index')

if [ -z "$FORMAT" ] || [ "$FORMAT" = "-1" ] || [ "$FORMAT" = "null" ]; then
  echo "Cancelled."
  read -p "Press Enter to close..."
  exit 1
fi


# Build options
if [ "$MODE" = "0" ]; then
  PLAYLIST_OPT=(--no-playlist)
else
  PLAYLIST_OPT=(--yes-playlist)
fi

if [ "$FORMAT" = "0" ]; then
  FORMAT_OPT=(-f "bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4]/bv*+ba/b" --merge-output-format mp4)
else
  FORMAT_OPT=(-f bestaudio -x --audio-format mp3 --audio-quality 0)
fi

OUTPUT="%(title)s.%(ext)s"
EXTRA_OPT=(-N 8)

mkdir -p "$DIRECTORY" && cd "$DIRECTORY" || {
  echo "Cannot access $DIRECTORY"
  read -p "Press Enter to close..."
  exit 1
}

echo "RESULT: yt-dlp ${PLAYLIST_OPT[*]} ${FORMAT_OPT[*]} ${EXTRA_OPT[*]} -o \"$OUTPUT\" \"$URL\""
yt-dlp "${PLAYLIST_OPT[@]}" "${FORMAT_OPT[@]}" "${EXTRA_OPT[@]}" -o "$OUTPUT" "$URL"
if [ $? -eq 0 ]; then
  termux-notification -t "Download Finished" -c "$URL"
else
  termux-notification -t "Download FAILED" -c "$URL"
fi

read -p "Press Enter to end script..."

# Keep terminal open and usable
exec bash
2 Upvotes

10 comments sorted by

11

u/TomJo2000 Termux Packages Dev 10h ago

We literally have a yt-dlp package. pkg i yt-dlp

10

u/negatrom 11h ago

lmao, no, why bother wasting tokens on crap like this

just do

pkg install pipx; pipx install yt-dlp

and bang yt-dlp installed. no need to kill a million trees for something that already has a solution holy shit

13

u/Monoboy 11h ago

You don't even need to do that. It is offered in the Termux repositories under the name python-yt-dlp.

pkg install python-yt-dlp

And you're done.

2

u/negatrom 10h ago

I find that the pipx version tends to update just a tiny bit faster than the termux repos, it cost me a few minutes of failed downloads more than once before thanks to that, which is why I tend to go the pipx route. plus I don't pollute my base python install with crap, pipx is deliciously encapsulated, so it feeds my "OCD" perfectly.

1

u/YTriom1 10h ago

uv is better than pipx

2

u/negatrom 9h ago

It's, like, an opinion, but I don't like the uv syntax

2

u/YTriom1 9h ago

I'm saying "x is better than y" as a personal opinion, I actually only found out about uv existence through a random reddit comment as well and it was the best replacement for pipx for me, especially uvx and uv run command.

2

u/AlterTableUsernames 7h ago

Agreed and I also like these random tool tips. The entire python ecosystem sucks ass, but uv is the least ass sucking way to install python shit. 

•

u/AdImmediate2565 1h ago

Cool, you like gave a better syntax, no?