Skip to content

Commit dfc8f56

Browse files
committed
Limit parallelism, fix filename too long, add -r option
1 parent eee5edb commit dfc8f56

File tree

2 files changed

+43
-31
lines changed

2 files changed

+43
-31
lines changed

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ Easiest would be using the [Windows Subsystem for Linux (WSL)](https://docs.micr
6666
- Put [`yt`](yt/yt) in your path.
6767
6868
## Usage
69-
- Type `yt -h` to print the following text and exit.
69+
Some examples:
70+
- Type `yt -h` to print the following text and exit.
7071
- Type `yt` to download audio files for the (space separated) URL(s) fetched from clipboard.
72+
- Type `yt -v` for (max) 1080p mkv download.
73+
7174
```
7275
NAME
7376
yt - fine-tuning the use of youtube-dl. Download music or video from e.g. YouTube,
@@ -104,12 +107,12 @@ OPTIONS
104107
sufficient permissions (run with sudo if needed).
105108
106109
-s
107-
Enable silent mode.
110+
Enable silent mode (send stdout to /dev/null).
108111
109112
-S
110-
Enable sequential mode. Default behaviour: parallel mode (send to background).
111-
Playlists are downloaded sequentially, as youtube-dl does not support parallel
112-
downloading of playlists (see #3746).
113+
Enable sequential mode. Default behaviour: parallel mode. Each playlist will
114+
be downloaded sequentially as youtube-dl does not support parallel downloading
115+
of playlists (see #3746). The MAXPROCS (env) var sets parallelism (default 4).
113116
114117
-v
115118
Enable video mode. Defaults to audio mode. Only mono and stereo are supported.
@@ -135,6 +138,9 @@ OPTIONS
135138
-a KBITS_PER_SECOND
136139
Set the output audio bitrate. Defaults to 256kbit/s.
137140
141+
-r HERTZ
142+
Set the output audio sampling rate. Defaults to 44100Hz.
143+
138144
-P PIXELS
139145
Set the maximum height in pixels of the video output. Ignored when -v is not
140146
specified. Defaults to 1080px.

yt/yt

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ OPTIONS
3636
sufficient permissions (run with sudo if needed).
3737
3838
-s
39-
Enable silent mode.
39+
Enable silent mode (send stdout to /dev/null).
4040
4141
-S
42-
Enable sequential mode. Default behaviour: parallel mode (send to background).
43-
Playlists are downloaded sequentially, as youtube-dl does not support parallel
44-
downloading of playlists (see #3746).
42+
Enable sequential mode. Default behaviour: parallel mode. Each playlist will
43+
be downloaded sequentially as youtube-dl does not support parallel downloading
44+
of playlists (see #3746). The MAXPROCS (env) var sets parallelism (default 4).
4545
4646
-v
4747
Enable video mode. Defaults to audio mode. Only mono and stereo are supported.
@@ -67,6 +67,9 @@ OPTIONS
6767
-a KBITS_PER_SECOND
6868
Set the output audio bitrate. Defaults to 256kbit/s.
6969
70+
-r HERTZ
71+
Set the output audio sampling rate. Defaults to 44100Hz.
72+
7073
-P PIXELS
7174
Set the maximum height in pixels of the video output. Ignored when -v is not
7275
specified. Defaults to 1080px.
@@ -95,13 +98,15 @@ All rights reserved."
9598
local OPTIND # https://stackoverflow.com/a/16655341/5511061
9699
local SILENT=false
97100
local PARALLEL=true
101+
local PARALLELISM=${MAXPROCS:=4}
98102
local VIDEO_MODE=false
99103
local CLIPBOARD=false
100104
local CUSTOM_DESTINATION=false
101105
local PLAYLIST=false
102106
local KEEP_AUDIO=false
103107
local CUSTOM_AUDIO_BITRATE=false
104108
local AUDIO_BITRATE=257
109+
local AUDIO_SAMPLING_RATE=44100
105110
local MAX_PIXELS=1080
106111
local MP4=false
107112
local AVC=false
@@ -125,7 +130,7 @@ All rights reserved."
125130
if [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
126131
set -- "-h"
127132
fi
128-
while getopts ":hUsSvcDpka:P:mMH" opt; do
133+
while getopts ":hUsSvcDpka:r:P:mMH" opt; do
129134
case $opt in
130135
U)
131136
sudo youtube-dl -U
@@ -170,6 +175,21 @@ All rights reserved."
170175
;;
171176
esac
172177
;;
178+
r)
179+
case $OPTARG in
180+
''|*[!0-9]*)
181+
echo "-r: specify sampling rate as integer. Type \"yt -h\" for more info."
182+
return
183+
;;
184+
*)
185+
if [ $OPTARG -gt 48000 ]; then
186+
echo "-r: sampling rate should not be greater than 48000Hz."
187+
return
188+
fi
189+
AUDIO_SAMPLING_RATE=$OPTARG
190+
;;
191+
esac
192+
;;
173193
P)
174194
case $OPTARG in
175195
''|*[!0-9]*)
@@ -210,7 +230,7 @@ All rights reserved."
210230
done
211231

212232
if $CLIPBOARD || [ ${#URLS[@]} -eq 0 ]; then
213-
URLS=($(pbpaste))
233+
URLS+=($(pbpaste))
214234
fi
215235
if ! $SILENT; then
216236
echo "${URLS[@]}"
@@ -233,16 +253,14 @@ All rights reserved."
233253

234254
# BASE_OPTIONS
235255
local BASE_OPTIONS=(--geo-bypass --ignore-config -i)
236-
local output_filename="${destination}%(title)s %(id)s.%(ext)s"
256+
local output_filename="${destination}%(title).200s %(id)s.%(ext)s"
237257
local download_archive="${destination}downloaded.txt"
238258
BASE_OPTIONS+=(-o "$output_filename")
239259
BASE_OPTIONS+=(--download-archive "$download_archive")
260+
BASE_OPTIONS+=(--add-metadata --metadata-from-title "%(artist)s - %(title)s")
240261
if ! $PLAYLIST; then
241262
BASE_OPTIONS+=(--no-playlist)
242263
fi
243-
if ! $VIDEO_MODE; then
244-
BASE_OPTIONS+=(--add-metadata --metadata-from-title "%(artist)s - %(title)s")
245-
fi
246264

247265

248266
# DOWNLOAD_OPTIONS
@@ -254,7 +272,7 @@ All rights reserved."
254272
if [ $MAX_PIXELS -gt 1080 ] && ! $SILENT; then
255273
echo "Maximum resolution is set to ${MAX_PIXELS} and -m is present. Downloads will be limited to max 1080p on sites that don't provide higher resolution video streams in MP4 container (e.g. YouTube)."
256274
fi
257-
local DOWNLOAD_OPTIONS=(--merge-output-format mp4 --postprocessor-args "-threads 0 -vcodec copy -acodec aac -b:a ${AUDIO_BITRATE}k -ar 44100")
275+
local DOWNLOAD_OPTIONS=(--merge-output-format mp4 --postprocessor-args "-threads 0 -vcodec copy -acodec aac -b:a ${AUDIO_BITRATE}k -ar ${AUDIO_SAMPLING_RATE}")
258276
if $AVC; then
259277
DOWNLOAD_OPTIONS+=(-f "(bestvideo[vcodec^=avc][height<=${MAX_PIXELS}]/bestvideo"'[vcodec!^=vp9]'"[height<=${MAX_PIXELS}])+(${audio_selector})/best[height<=${MAX_PIXELS}]")
260278
else
@@ -264,7 +282,7 @@ All rights reserved."
264282
local DOWNLOAD_OPTIONS=(--merge-output-format mkv -f "(bestvideo[vcodec=vp9][height<=${MAX_PIXELS}]/bestvideo[vcodec!=vp9.2][height<=${MAX_PIXELS}])+(${audio_selector})/best[height<=${MAX_PIXELS}]")
265283
fi
266284
else
267-
local DOWNLOAD_OPTIONS=(--embed-thumbnail --audio-format m4a --audio-quality ${AUDIO_BITRATE}k --postprocessor-args "-ar 44100" -x -f "${audio_selector}/best")
285+
local DOWNLOAD_OPTIONS=(--embed-thumbnail --audio-format m4a --audio-quality ${AUDIO_BITRATE}k --postprocessor-args "-ar ${AUDIO_SAMPLING_RATE}" -x -f "${audio_selector}/best")
268286
if $KEEP_AUDIO; then
269287
DOWNLOAD_OPTIONS+=(-k)
270288
fi
@@ -281,21 +299,9 @@ All rights reserved."
281299

282300
if $PARALLEL; then
283301
if $SILENT; then
284-
len=$((${#URLS[@]}-2))
285-
if [ $len -gt -1 ]; then
286-
for i in $(seq 0 $len); do
287-
(youtube-dl "${BASE_OPTIONS[@]}" "${DOWNLOAD_OPTIONS[@]}" "${URLS[i]}" > /dev/null &)
288-
done
289-
fi
290-
youtube-dl "${BASE_OPTIONS[@]}" "${DOWNLOAD_OPTIONS[@]}" "${URLS[$(($len+1))]}" > /dev/null
302+
printf "\"%s\"\n" "${URLS[@]}" | xargs -n 1 -P ${PARALLELISM} -I{} youtube-dl "${BASE_OPTIONS[@]}" "${DOWNLOAD_OPTIONS[@]}" "{}" > /dev/null
291303
else
292-
len=$((${#URLS[@]}-2))
293-
if [ $len -gt -1 ]; then
294-
for i in $(seq 0 $len); do
295-
(youtube-dl "${BASE_OPTIONS[@]}" "${DOWNLOAD_OPTIONS[@]}" "${URLS[i]}" &)
296-
done
297-
fi
298-
youtube-dl "${BASE_OPTIONS[@]}" "${DOWNLOAD_OPTIONS[@]}" "${URLS[$(($len+1))]}"
304+
printf "\"%s\"\n" "${URLS[@]}" | xargs -n 1 -P ${PARALLELISM} -I{} youtube-dl "${BASE_OPTIONS[@]}" "${DOWNLOAD_OPTIONS[@]}" "{}"
299305
fi
300306
else
301307
if $SILENT; then

0 commit comments

Comments
 (0)