feat(user/bkytpldl-generic):Limit channel name to 32 bytes
[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.1.2
5
6declare -a args; # array for yt-dlp arguments
7declare -a urls urls_rand; # array for YouTube playlist URLs
8
9# Settings
10dir_out="~/Videos/";
11urls+=("https://www.youtube.com/playlist?list=PLxxx"); # Adjust me. YouTube playlist URL goes here
12urls+=("https://www.youtube.com/playlist?list=PLxxx"); # Adjust me. YouTube playlist URL goes here
13urls+=("https://www.youtube.com/playlist?list=PLxxx"); # Adjust me. YouTube playlist URL goes here
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# == Assemble options ==
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).32B.%(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
92### Shuffle playlist download order
93mapfile -t urls_rand < <(printf "%s\n" "${urls[@]}" | shuf);
94for url in "${urls_rand[@]}"; do
95 args+=("$url");
96done;
97
98# Change working directory to output dir
99pushd "$dir_out" || die "FATAL:Failed to change pwd to:dir_out:$dir_out";
100
101# == Download videos ==
102
103#yell "DEBUG:args:$(declare -p args)"; # debug command
104must yt-dlp "${args[@]}"; # execute command
105popd || die "FATAL:Failed to return from dir_out:$dir_out";
106
107# Author: Steven Baltakatei Sandoval
108# License; GPLv3+