我知道对于单个播放列表,我们可以在下载 n 次后--playlist-end NUMBER
退出youtube-dl
。
但是,我的问题是我正在使用批处理文件。
看起来batch-file.txt
像
https://www.youtube.com/playlist?list=PLUiueC0kTFqLvpFk_Zg55geh_TBTKnbnA
https://www.youtube.com/playlist?list=PL11ZiN8vMsDpdgDscEZZrCfR5vQT_EzvG
https://www.youtube.com/playlist?list=PLgCYzUzKIBE_ZuZzgts135GuLQNX5eEPk
试运行看起来像:
$ youtube-dl -i -c --no-warnings --console-title --batch-file='batch-file.txt' -s -o '%(playlist_title)s/%(title)s.%(ext)s'
[youtube:tab] PLUiueC0kTFqLvpFk_Zg55geh_TBTKnbnA: Downloading webpage
[download] Downloading playlist: Testing Flutter Apps
[youtube:tab] playlist Testing Flutter Apps: Downloading 6 videos
[download] Downloading video 1 of 6
[youtube] eLMa2jgp_bw: Downloading webpage
[youtube] eLMa2jgp_bw: Downloading MPD manifest
[download] Downloading video 2 of 6
[youtube] nuMqDNUb7f0: Downloading webpage
[youtube] nuMqDNUb7f0: Downloading MPD manifest
[download] Downloading video 3 of 6
[youtube] Vfi0uyDWIuM: Downloading webpage
[youtube] Vfi0uyDWIuM: Downloading MPD manifest
[download] Downloading video 4 of 6
[youtube] 225MFHjQokw: Downloading webpage
[youtube] 225MFHjQokw: Downloading MPD manifest
[download] Downloading video 5 of 6
[youtube] uB5_REsREtU: Downloading webpage
[youtube] uB5_REsREtU: Downloading MPD manifest
[download] Downloading video 6 of 6
[youtube] YjtGT7Cccd0: Downloading webpage
[youtube] YjtGT7Cccd0: Downloading MPD manifest
[download] Finished downloading playlist: Testing Flutter Apps
[youtube:tab] PL11ZiN8vMsDpdgDscEZZrCfR5vQT_EzvG: Downloading webpage
[download] Downloading playlist: Funny Readings
[youtube:tab] playlist Funny Readings: Downloading 7 videos
[download] Downloading video 1 of 7
[youtube] EShUeudtaFg: Downloading webpage
[download] Downloading video 2 of 7
[youtube] 15nNY7uofNw: Downloading webpage
[download] Downloading video 3 of 7
[youtube] 4nxsCZ2SEcQ: Downloading webpage
[download] Downloading video 4 of 7
[youtube] lhLHXjZyr8I: Downloading webpage
[download] Downloading video 5 of 7
[youtube] NmnGt4LBxcQ: Downloading webpage
ERROR: Private video
Sign in if you've been granted access to this video
[download] Downloading video 6 of 7
[youtube] feXHNClEZdI: Downloading webpage
[download] Downloading video 7 of 7
[youtube] GH5M4trXnTU: Downloading webpage
[youtube] GH5M4trXnTU: Downloading player b2e56c01
[download] Finished downloading playlist: Funny Readings
[youtube:tab] PLgCYzUzKIBE_ZuZzgts135GuLQNX5eEPk: Downloading webpage
[download] Downloading playlist: UI Testing for Beginners
[youtube:tab] playlist UI Testing for Beginners: Downloading 20 videos
[download] Downloading video 1 of 20
[youtube] j8jQq-kYgDU: Downloading webpage
[youtube] j8jQq-kYgDU: Downloading MPD manifest
[download] Downloading video 2 of 20
[youtube] NTBj_0tIlQc: Downloading webpage
[youtube] NTBj_0tIlQc: Downloading MPD manifest
[download] Downloading video 3 of 20
[youtube] zJuFYqnBcgQ: Downloading webpage
[youtube] zJuFYqnBcgQ: Downloading MPD manifest
[download] Downloading video 4 of 20
[youtube] GDTJHaR5yIs: Downloading webpage
[youtube] GDTJHaR5yIs: Downloading MPD manifest
[download] Downloading video 5 of 20
[youtube] ZpFgY6qc_ZY: Downloading webpage
[youtube] ZpFgY6qc_ZY: Downloading MPD manifest
[download] Downloading video 6 of 20
[youtube] KOC_wC3LAoA: Downloading webpage
[youtube] KOC_wC3LAoA: Downloading MPD manifest
[download] Downloading video 7 of 20
[youtube] VuPU6yG0uwg: Downloading webpage
[youtube] VuPU6yG0uwg: Downloading MPD manifest
[download] Downloading video 8 of 20
[youtube] QmH-B_UYIyA: Downloading webpage
[youtube] QmH-B_UYIyA: Downloading MPD manifest
[download] Downloading video 9 of 20
[youtube] CpQQ6-vo5bw: Downloading webpage
[youtube] CpQQ6-vo5bw: Downloading MPD manifest
[download] Downloading video 10 of 20
[youtube] xxO0Z9GzcBU: Downloading webpage
[youtube] xxO0Z9GzcBU: Downloading MPD manifest
[download] Downloading video 11 of 20
[youtube] HRRn6m5hUoA: Downloading webpage
[youtube] HRRn6m5hUoA: Downloading MPD manifest
[download] Downloading video 12 of 20
[youtube] L037q8MGkGA: Downloading webpage
[youtube] L037q8MGkGA: Downloading MPD manifest
[download] Downloading video 13 of 20
[youtube] IaxMgpKwRFY: Downloading webpage
[youtube] IaxMgpKwRFY: Downloading MPD manifest
[download] Downloading video 14 of 20
[youtube] A88735Xv108: Downloading webpage
[youtube] A88735Xv108: Downloading MPD manifest
[download] Downloading video 15 of 20
[youtube] _96FT7E6PL4: Downloading webpage
[youtube] _96FT7E6PL4: Downloading MPD manifest
^[[B[download] Downloading video 16 of 20
[youtube] BPlHmKjQUWo: Downloading webpage
[youtube] BPlHmKjQUWo: Downloading MPD manifest
[download]
第一个播放列表有 6 个视频,第二个播放列表有 7 个视频,第三个播放列表有 20 个视频。
我下载了总共 18 个视频后想退出下载。
我发现这三个播放列表中总共有 33 个视频。Downloading video
在标准输出中出现 33 次。
$ youtube-dl -i -c --no-warnings -s --console-title -o '%(playlist_title)s/%(title)s.%(ext)s' --batch-file='batch-file.txt' | grep -o "Downloading video" | wc -l
33
所以,如果我在Downloading video
第 19 次出现时退出下载,那么它就会成功。
我成立那:
要在给出特定输出后终止进程,可以使用
mylongrunningtool | stdbuf -o0 egrep '(term1|term2)' >&-
不过这段代码有一个问题。它不显示标准输出。
我想我必须做类似的事情下列的:
var=0
my_program | while read line ; do
if echo "$line" | grep "Downloading video"
then
((i=i+1))
fi
if var=20 exit my_program
done
或者下列的可能也有帮助:
my_program | awk 'BEGIN{s=0} /error message/{s=1} 1; END{exit(s)}'
但我不知道该怎么做。我能做些什么?
答案1
根据对您的问题的评论:youtube-dl
有一个限制下载总数的特定选项:
--max-downloads NUMBER
下载 NUMBER 个文件后中止
使用问题中的命令和批处理文件:
youtube-dl -i -c --max-downloads=18 --no-warnings --console-title \
--batch-file='batch-file.txt' -o '%(playlist_title)s/%(title)s.%(ext)s
从第一个播放列表下载 6 个文件,从第二个播放列表下载 6 个文件(其中一个因“错误:私人视频”而失败),从第三个播放列表下载 6 个文件。总共18个。