diff options
author | EuAndreh <eu@euandre.org> | 2022-08-14 20:59:57 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2022-08-14 20:59:57 -0300 |
commit | fd117df8ee936d6ccedc9350e0d5304e2bf8b0d2 (patch) | |
tree | 8c32f82b4f04327cf4290c40b0b3a1332d2bfb49 /bin/yt | |
parent | bin/gc: Enable "set -x" option (diff) | |
download | dotfiles-fd117df8ee936d6ccedc9350e0d5304e2bf8b0d2.tar.gz dotfiles-fd117df8ee936d6ccedc9350e0d5304e2bf8b0d2.tar.xz |
bin/yt: Add working utility
Diffstat (limited to 'bin/yt')
-rwxr-xr-x | bin/yt | 110 |
1 files changed, 110 insertions, 0 deletions
@@ -0,0 +1,110 @@ +#!/bin/sh +set -eu + +usage() { + cat <<-'EOF' + Usage: + yt [-f] [-n PLAYLIST_COUNT] [URL...|FILE...|-] + yt -h + EOF +} + +help() { + cat <<-'EOF' + + Options: + -n PLAYLIST_COUNT the number of videos to grab from a + playlist (default: 15) + -f force to download a video already in the + archive + -h, --help show this message + + URL an 'https://...' address + FILE a file with 'https://...' addresses, one + per line + + Download videos and store them locally. + + + Examples: + + Download the video from the given URL: + + $ yt https://www.youtube.com/watch?v=EihZv2XgJdU + + + Force re-download the latest 5 videos already downloaded: + + $ yt -nf5 https://www.youtube.com/watch?list=TLPQMTIwODIwMjLnL2XjyRRgSw + EOF +} + + +for flag in "$@"; do + case "$flag" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +PLAYLIST_COUNT=15 +while getopts 'fn:h' flag; do + case "$flag" in + f) + FORCE=1 + ;; + n) + PLAYLIST_COUNT="$OPTARG" + ;; + h) + usage + help + exit + ;; + *) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) +set -x + +if [ -z "${1:-}" ]; then + echo 'Missing URL|FILE argument' >&2 + usage >&2 + exit 2 +fi + + +if [ ! -e "$1" ]; then + F="$(mkstemp)" + printf '%s\n' "$1" > "$F" +else + F="$1" +fi + + +YT_TEMPLATE="$HOME/Downloads/yt-dl/%(uploader)s/%(upload_date)s %(title)s.%(ext)s" + +EXTRA_OPTIONS='' +if [ -z "${FORCE:-}" ]; then + EXTRA_OPTIONS="--download-archive $HOME/Downloads/yt-dl/seen.txt" +fi + +youtube-dl \ + --batch-file "$F" \ + --format best \ + --prefer-free-formats \ + --playlist-end "$PLAYLIST_COUNT" \ + --write-description \ + --output "$YT_TEMPLATE" \ + $EXTRA_OPTIONS |