feat(user/bkytpldl-generic):Add generic yt-dlp wrapper
[BK-2020-03.git] / user / bkytpldl-generic
... / ...
CommitLineData
1#!/usr/bin/env bash
2# Desc: Download YouTube videos
3# Usage: $ ./bkytpldl-generic
4# Version: 4.0.0
5
6# Adjust me
7dir_out="~/Videos/";
8url_playlist1="https://www.youtube.com/playlist?list=PLxxx";
9url_playlist2="https://www.youtube.com/playlist?list=PLxxx";
10url_playlist3="https://www.youtube.com/playlist?list=PLxxx";
11
12declare -a args; # array for yt-dlp arguments
13declare -a urls urls_rand; # array for YouTube playlist URLs
14
15yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
16die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
17must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
18
19# check dependencies
20if ! command -v yt-dlp 1>/dev/random 2>&1; then die "FATAL:yt-dlp not found."; fi;
21
22# Donʼt run multiple yt-dlp instances
23if pgrep "^yt-dlp$" 1>/dev/random 2>&1; then die "FATAL:yt-dlp already running."; fi;
24
25# Check directories
26if [[ ! -d $dir_out ]]; then mkdir -p "$dir_out"; fi;
27
28# == Download Video ==
29
30# yt-dlp output options
31## Restrict file name character set
32#args+=("--restrict-filenames"); # Remove non-ASCII characters
33args+=("--trim-filenames=120"); # Use in tandem with `%(title).120B`
34
35## Request to write accompanying files
36args+=("--write-subs"); # Write subtitles file
37args+=("--write-auto-subs"); # Write subtitles file
38#args+=("--all-subs"); # Download all available subtitles (causes many requests)
39#subLangs="en.*,ja.*,id.*,es.*,zh-Hans.*,zh-Hant.*,sv.*,el.*,hi.*,ru.*,bn.*,fr.*,ko.*,ar.*,nv.*"; # custom language list
40subLangs="en,en-orig,en.*"; # custom language list
41args+=("--sub-langs" "$subLangs");
42args+=("--write-info-json"); # Write accompanying json file
43args+=("--no-overwrites"); # Don't overwrite files
44args+=("--write-thumbnail"); # Write thumbnail
45
46## Only download metadata
47#args+=("--no-download"); # Don't download video file.
48
49## Save meta-data
50args+=("--write-comments"); # Get comments
51### Limit comments
52### comment_sort values:
53### top : use YouTube top comment algorithm
54### new : get newest comments (default)
55### max_comments values:
56### max-comments : max number of parent comments or replies
57### max-parents : max number of comment threads
58### max-replies : max number of replies across all threads
59### max-replies-per-thread : max number of replies per thread
60args+=("--extractor-args" "youtube:comment_sort=top;max_comments=10000,100,10000,100");
61
62## Randomize order in which playlist items are downloaded
63args+=("--playlist-random");
64
65## Delay between downloads
66minSleep="30";
67maxSleep="$(( minSleep + (RANDOM + RANDOM + RANDOM) / ( 3 * 400) ))"; # roughly 60 seconds
68args+=("--min-sleep-interval" "$minSleep");
69args+=("--max-sleep-interval" "$maxSleep");
70args+=("--sleep-requests" "2"); # delay on metadata requests
71args+=("--sleep-subtitles" "10"); # delay for subtitles
72
73## Remember downloaded videos to avoid redownload attempts
74pathDA="$dir_out"/.bkytpldl_history.txt;
75args+=("--download-archive" "$pathDA");
76
77## Use firefox 'default-release' profile cookies
78## Example: Linux: from ~/.mozilla/firefox/deadbeef.default-release/
79#args+=("--cookies-from-browser");
80#args+=("firefox:deadbeef.default-release"); Default Firefox profile name
81
82## Specify output filename format
83## Note: `$(title).120B` shortens title to 120 bytes (useful for
84## titles with UTF-8 characters.
85args+=("-o");
86args+=("%(playlist)s/%(upload_date)s.%(channel)s.%(channel_id)s.%(title).120B.%(id)s.%(ext)s");
87
88## Limit download resolution to 1080p
89args+=("-S" "res:1080");
90
91## Specify playlist URLs to download
92urls+=("$url_playlist1");
93urls+=("$url_playlist2");
94urls+=("$url_playlist3");
95### Shuffle playlist download order
96mapfile -t urls_rand < <(printf "%s\n" "${urls[@]}" | shuf);
97for url in "${urls_rand[@]}"; do
98 args+=("$url");
99done;
100
101# Change working directory to output dir
102pushd "$dir_out" || die "FATAL:Failed to change pwd to:dir_out:$dir_out";
103
104# Download videos
105#yell "DEBUG:args:$(declare -p args)"; # debug command
106must yt-dlp "${args[@]}"; # execute command
107popd || die "FATAL:Failed to return from dir_out:$dir_out";
108
109# Author: Steven Baltakatei Sandoval
110# License; GPLv3+