Skip to content

Instantly share code, notes, and snippets.

@qwerty12
Last active August 24, 2024 23:54
Show Gist options
  • Save qwerty12/e236f9c05bfdccffca35338383bdc02e to your computer and use it in GitHub Desktop.
Save qwerty12/e236f9c05bfdccffca35338383bdc02e to your computer and use it in GitHub Desktop.
Install the followig at least: VLC in Android, `jq`, Termux API, `youtube-dl`, `streamlink`, `gallery-dl` and `aria2c`
#!/data/data/com.termux/files/usr/bin/bash
if [ -z "${1:-}" ]; then
exit 1
fi
url_is_supported() {
REPLY=""
declare -A arr
case "$1" in
*youtube.com/watch*|https://youtu.be/*)
if command -v youtube-dl >/dev/null 2>&1; then
arr[ydl]=0
fi
;;
*)
# threading in Python is slower than this...
if command -v youtube-dl >/dev/null 2>&1; then
printf 'import os\nfrom youtube_dl import extractor\nfor e in extractor.gen_extractors():\n if e.suitable("%s") and e.IE_NAME != "generic":\n os._exit(0)\nos._exit(1)' "$1" | python &
arr[ydl]=$!
fi
if [ -d /data/data/org.videolan.vlc ]; then
if command -v streamlink >/dev/null 2>&1; then
printf 'import os\nfrom streamlink import Streamlink\ntry:\n Streamlink().resolve_url_no_redirect("%s")\nexcept:\n os._exit(1)\nos._exit(0)' "$1" | python &
arr[slk]=$!
fi
fi
if command -v gallery-dl >/dev/null 2>&1; then
printf 'import os\nfrom gallery_dl import extractor\nif extractor.find("%s") is not None:\n os._exit(0)\nos._exit(1)' "$1" | python &
arr[gdl]=$!
fi
for k in "${!arr[@]}"; do
wait "${arr[$k]}"
arr[$k]=$?
done
;;
esac
if [ ${arr[ydl]+bleh} ] && [ "${arr[ydl]}" -eq 0 ]; then
REPLY="video - download,audio - download,vlc - stream (youtube-dl)"
fi
if [ ${arr[slk]+bleh} ] && [ "${arr[slk]}" -eq 0 ]; then
REPLY="${REPLY:+$REPLY,}vlc - stream (streamlink)"
fi
case "$1" in
*soundcloud.com/*)
if command -v scdl >/dev/null 2>&1; then
REPLY="${REPLY:+$REPLY,}scdl - download"
fi
;;
esac
if [ ${arr[gdl]+bleh} ] && [ "${arr[gdl]}" -eq 0 ]; then
REPLY="${REPLY:+$REPLY,}image - download gallery"
fi
if command -v aria2c >/dev/null 2>&1; then
REPLY="${REPLY:+$REPLY,}aria2c - download"
fi
if [ -z "$REPLY" ]; then
exit 1
fi
}
youtube_dl_with_common() { youtube-dl --ignore-config --no-call-home --add-metadata --no-mtime --restrict-filenames --cookies "$cookies" -o "$ytdir"'/%(extractor_key)s/%(title).20s.%(ext)s' "$@"; }
downloads="$HOME/storage/downloads"
ytdir="$downloads/yt-dl"
cookies="$downloads/cookies.txt"
# shellcheck disable=SC2064
trap "rm -f $cookies" INT QUIT TERM EXIT
url_is_supported "$1"
set -euo pipefail
if [ "${#1}" -ge 44 ]; then
len=32
ellipsized_url="${1:0:len-3}...${1: -$len}"
fi
program="youtube-dl"
case "$(termux-dialog spinner -v "$REPLY" -t "${ellipsized_url:-$1}" | jq -re '.text | select (.!=null)')" in
video*)
youtube_dl_with_common -f 'bestvideo[height<=?720][fps<=?30][vcodec!=?vp9][protocol!=http_dash_segments][ext=mp4]+bestaudio[ext=m4a]/best' --write-info-json "$1"
;;
audio*)
youtube_dl_with_common --extract-audio --audio-format mp3 --audio-quality 0 --metadata-from-title '%(artist)s - %(title)s' --embed-thumbnail "$1"
;;
'vlc - stream (youtube-dl)')
decodedurl="$(youtube-dl -f best --ignore-config --no-call-home --cookies "$cookies" -g "$1")"
if ! am start -R 2 -S -n org.videolan.vlc/org.videolan.vlc.gui.video.VideoPlayerActivity -a android.intent.action.VIEW -d "$decodedurl"; then
am start -a android.intent.action.VIEW -t video/mpeg -d "$decodedurl"
fi
exit 0
;;
'vlc - stream (streamlink)')
streamlink --config=/dev/null --player-http --player=bash -a '-c '\''am start -W -S -n org.videolan.vlc/org.videolan.vlc.gui.video.VideoPlayerActivity -a android.intent.action.VIEW -d '\''{filename}'\'' ; read -n 1'\''' "$1" best
exit $?
;;
scdl*)
program="scdl"
"$program" --path "$ytdir" -l "$1"
;;
image*)
program="gallery-dl"
"$program" --ignore-config --no-mtime --write-metadata --cookies "$cookies" -d "$ytdir" "$1"
;;
aria2c*)
program="aria2c"
if command -v youtube-dl >/dev/null 2>&1; then
ua="$(youtube-dl --ignore-config --no-call-home --dump-user-agent)"
else
ua='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3688.1 Safari/537.36'
fi
echo "$1"
"$program" --no-conf --always-resume=false --max-connection-per-server=4 --file-allocation=none --auto-save-interval=0 --enable-http-pipelining=true --user-agent="$ua" --load-cookies="$cookies" --dir="$downloads" "$1"
;;
*)
exit 0
esac
set +e
if [ "$program" != "aria2c" ]; then
termux-media-scan -r "$ytdir"
fi
termux-notification --title "$program" --content "Completed: $1" --id 69 --priority low
@dumblob
Copy link

dumblob commented Nov 2, 2021

This is a really cool idea! I'm doing a similar thing on my desktops/notebooks and now I'm considering using Android more because of this your idea!

@FalsePhilosopher
Copy link

Any chance you want to update to a yt-dlp version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment