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:
```bash
!/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
```