Skip to content

Instantly share code, notes, and snippets.

@lemon-sh
Created November 12, 2021 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lemon-sh/6f2d539a8eee6427f2d7c7f483e28000 to your computer and use it in GitHub Desktop.
Save lemon-sh/6f2d539a8eee6427f2d7c7f483e28000 to your computer and use it in GitHub Desktop.
script for downloading yt videos using yt-dlp but with aria2c as the downloader
#!/bin/bash
# by lemon.sh
set -euo pipefail
[[ $# -ne 2 ]] && echo "Usage: $0 <youtube link> <output filename>" && exit 1
type yt-dlp &>/dev/null || { echo "Error: yt-dlp is not available."; exit 10; }
type aria2c &>/dev/null || { echo "Error: aria2c is not available."; exit 11; }
type ffmpeg &>/dev/null || { echo "Error: ffmpeg is not available."; exit 12; }
fileprefix="ytaria_${RANDOM}"
compgen -G "$fileprefix*" && { echo "Error: File(s) with prefix '$fileprefix' exist(s)."; exit 2; }
yt-dlp -F "$1"
echo -e "\nSelect 1st codec:"
read -r prim
echo -e "\nSelect 2nd codec [empty for none]:"
read -r secn
[ -z "$secn" ] || secn=+$secn
echo -e "\nGetting the URL(s)..."
urls=$(yt-dlp -gf "$prim$secn" "$1")
IFS=$'\n'
urlarr=($urls)
unset IFS
[[ -v "urlarr[0]" ]] || { echo "Error: Invalid yt-dlp output"; exit 3; }
aria2c -k 1M -x 16 -s 16 -o "$fileprefix+$prim" "${urlarr[0]}"
if [[ -v "urlarr[1]" ]]; then
aria2c -k 1M -x 16 -s 16 -o "$fileprefix$secn" "${urlarr[1]}"
doas ffmpeg -i "$fileprefix+$prim" -i "$fileprefix$secn" -c copy "$2"
rm "$fileprefix+$prim" "$fileprefix$secn"
else
doas mv "$fileprefix+$prim" "$2"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment