Snippets

Peter Finlayson youtube-dl wrapper

Created by Peter Finlayson last modified
#!/bin/bash

VERSION="1.3.2"
YOUTUBE_DL="youtube-dl"
YOUTUBE_DL="/home/pfinlayson/.local/bin/youtube-dl"
YOUTUBE_DL="/home/pfinlayson/.local/bin/yt-dlp"
MP4_FORMAT="137+140"

declare -a args

quiet_mode=""
output_template_default="%(title)s-%(id)s.%(ext)s"
output_template_twitch="%(id)s-%(title)s.%(ext)s"
output_template="$output_template_default"

TWITCH_RE='^(http[s]?://)?([A-Za-z0-9]*[.])?twitch.tv/'

function usage {
    echo "$(basename $0) $VERSION - Wrapper script for youtube-dl"
    echo "  Usage: $(basename $0) <url>"
    echo "    -F         List of available audio and video formats"
    echo "    -f FORMAT  Download the video using FORMAT"
    echo "    --mp4      Download a 1080p MP4 with AAC audio (if available)"
    echo "For more information about the optiona available in the base tool, run:"
    echo "  '$YOUTUBE_DL' --help"
}

# parse the command line
function parse_args {
    local arg
    
	for arg in "$@"; do
		case "$arg" in
		-h|--help)
			usage
			exit 0
			;;
		-v|--version)
			usage
			exit 0
			;;
		--mp4)
			args+=( "-f" "$MP4_FORMAT" )
			;;
		-q|--quiet)
			args+=( "$arg" )
			quiet_mode="true"
			;;
		*)	
			# switch to twitch names maybe
			[[ "$arg" =~ $TWITCH_RE ]] && output_template="$output_template_twitch"
			# pass the arg to youtube-dl
			args+=("$arg")
			;;
		esac
	done
}

parse_args "$@"

[[ -z "$quiet_mode" ]] && echo youtube-dl -o "$output_template" --restrict-filenames --no-mtime "${args[@]}"
exec "$YOUTUBE_DL" -o "$output_template" --restrict-filenames --no-mtime "${args[@]}"

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.