Skip to content

Instantly share code, notes, and snippets.

@ergosteur
Created July 31, 2024 12:54
Show Gist options
  • Save ergosteur/43356fec1eec86169c62984541b148f8 to your computer and use it in GitHub Desktop.
Save ergosteur/43356fec1eec86169c62984541b148f8 to your computer and use it in GitHub Desktop.
Script to archive youtube videos including metadata, safe filenames, support for subs and multiple preset formats. Easily customizable
#!/bin/bash
# Unified script for downloading videos in various formats, supporting individual and batch downloads
# Configuration Variables
archivepath="/volume1/atlantic/users/matt/youtube-dl-archive"
output_template='%(uploader)s/%(upload_date)s %(title).50s [%(id)s][%(resolution)s][%(vcodec).4s+%(acodec).4s][%(dynamic_range)s][%(format_id)s][%(availability)s].%(ext)s'
ffmpeg_location="/var/packages/ffmpeg6/target/bin/"
ytdlp_binary="/var/services/homes/matt/.local/bin/yt-dlp"
options="--windows-filenames --write-thumbnail --embed-chapters --add-metadata --ffmpeg-location=$ffmpeg_location"
alias ffmpeg=ffmpeg6
# Function to display usage
function usage {
echo "Usage: $0 --format [format] [--batch /path/to/batch_file.txt] [URL]"
echo "Formats available: av01, 4k, 4ksubs, hdr, basic, basicsubs, subsonly, vp9"
echo "Examples:"
echo " $0 --format 4k --batch /path/to/batch_list.txt"
echo " $0 --format av01 https://www.youtube.com/watch?v=example"
exit 1
}
# Check for no arguments, help request or invalid flags
if [ $# -eq 0 ] || [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
usage
fi
# Default values
format="basic" # Default format if not specified
batch_mode=0
batch_file=""
# Parse command line arguments
while (( "$#" )); do
case "$1" in
--format)
format=$2
shift 2
;;
--batch)
batch_mode=1
batch_file=$2
shift 2
;;
--) # end argument parsing
shift
break
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
usage
;;
*) # Handle non-option arguments here
break
;;
esac
done
# Check if there are remaining arguments which should be the URL
if [ "$batch_mode" -eq 0 ] && [ "$#" -eq 0 ]; then
echo "Error: No URL provided for download."
usage
fi
video_url=$1 # This should now correctly assign the URL
# Set format and corresponding download archive file based on the specified option
case $format in
av01)
format_option='bestvideo[vcodec^=av01][height>=?900]+bestaudio[ext=m4a]'
archive_file="downloaded-av01.txt"
;;
4k)
format_option='bestvideo[height>=?1300]+bestaudio'
archive_file="downloaded4k.txt"
;;
hdr)
format_option='bestvideo[height>=?900][dynamic_range*=HDR]+bestaudio[ext=webm]'
archive_file="downloaded-hdr.txt"
;;
basic)
format_option='bestvideo[vcodec^=avc1][height>=?900]+bestaudio[ext=m4a]/bestvideo[ext=mp4]+bestaudio[ext=m4a]'
archive_file="downloaded.txt"
;;
subsonly)
format_option='bestvideo[vcodec^=avc1][height>=?900]+bestaudio[ext=m4a]/bestvideo[ext=mp4]+bestaudio[ext=m4a]'
archive_file="downloaded-subsonly.txt"
options+=" --write-sub --sub-lang en --skip-download"
;;
basicsubs)
format_option='bestvideo[vcodec^=avc1][height>=?900]+bestaudio[ext=m4a]/bestvideo[ext=mp4]+bestaudio[ext=m4a]'
archive_file="downloaded-basicsubs.txt"
options+=" --write-sub --sub-lang en"
;;
4ksubs)
format_option='bestvideo[height>=?1300]+bestaudio'
archive_file="downloaded-4ksubs.txt"
options+=" --write-sub --sub-lang en"
;;
vp9)
format_option='bestvideo[vcodec^=vp9][height>=?900]+bestaudio[acodec=opus]'
archive_file="downloaded-vp9.txt"
;;
*)
echo "Invalid format: $format"
echo "Valid formats are: av01, 4k, basic, subs, vp9"
exit 1
;;
esac
# Execute based on batch mode or single video
if [ "$batch_mode" -eq 1 ]; then
echo "Processing batch file: $batch_file with format: $format_option"
$ytdlp_binary --cookies $archivepath/cookies.txt --download-archive $archivepath/$archive_file -i -o "$archivepath/$output_template" -f "$format_option" $options --batch-file="$archivepath/$batch_file"
else
echo "Downloading video: $video_url with format: $format_option"
$ytdlp_binary --cookies $archivepath/cookies.txt --download-archive $archivepath/$archive_file -i -o "$archivepath/$output_template" -f "$format_option" $options "$video_url"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment