【发布时间】:2021-03-06 20:21:48
【问题描述】:
我有一组用于归档 youtube 频道的脚本,其中包括:
- 一个 awk 脚本,它读取通道列表和归档参数并使用
system()运行... - ...一个 bash 脚本,它接受这些参数并调用
youtube-dl(并执行一些其他操作)
我已修改 awk 脚本以在它从 bash 脚本获得非零返回码时退出,因为我希望能够在 youtube-dl 下载时终止整个进程(如果我不这样做这个,它只会移动到下一个频道)。
但是,我发现 youtube-dl 在收到 ctrl-c 或遇到其他错误时会给出相同的非零返回码。这导致我的脚本提前退出,因为我正在归档的某些频道包含 youtube-dl 跳过的私人视频,这似乎算作错误。
如何让我的 bash 脚本区分 youtube-dl 被 ctrl-c 杀死和其他错误?
示例
youtube-dl版本:
(youtube-dl-venv)$ youtube-dl --version
2021.03.03
youtube-dl 被 Ctrl-C 杀死时以 1 退出:
(youtube-dl-venv)$ youtube-dl --force-ipv4 --sleep-interval 15 --ignore-errors --no-continue --no-overwrites -r 1M https://www.youtube.com/channel/UCcHpteNEx4B221FIkGOlhnQ
[youtube:tab] UCcHpteNEx4B221FIkGOlhnQ: Downloading webpage
[download] Downloading playlist: Kaypro Due - Home
[youtube:tab] playlist Kaypro Due - Home: Downloading 1 videos
[download] Downloading video 1 of 1
[youtube:tab] UCcHpteNEx4B221FIkGOlhnQ: Downloading webpage
[download] Downloading playlist: Kaypro Due - Videos
[youtube:tab] playlist Kaypro Due - Videos: Downloading 1 videos
[download] Downloading video 1 of 1
[youtube] X2nrfbXdfRU: Downloading webpage
[youtube] X2nrfbXdfRU: Downloading MPD manifest
[download] Sleeping 15 seconds...
^C
ERROR: Interrupted by user
(youtube-dl-venv)$ echo $?
1
youtube-dl遇到私密视频时退出1:
(youtube-dl-venv)$ youtube-dl --force-ipv4 --sleep-interval 15 --ignore-errors --no-continue --no-overwrites -r 1M https://www.youtube.com/playlist?list=PLfu3xDF_96dO_ce4vEzhzAkZ-H1glnOjf
[youtube:tab] PLfu3xDF_96dO_ce4vEzhzAkZ-H1glnOjf: Downloading webpage
[download] Downloading playlist: Test Playlist
[youtube:tab] playlist Test Playlist: Downloading 2 videos
[download] Downloading video 1 of 2
[youtube] 6JlpzbgouUE: Downloading webpage
ERROR: Private video
Sign in if you've been granted access to this video
[download] Downloading video 2 of 2
[youtube] oN0G-ebwmZ8: Downloading webpage
[youtube] oN0G-ebwmZ8: Downloading MPD manifest
[download] Test Video 2-oN0G-ebwmZ8.mp4 has already been downloaded and merged
[download] Finished downloading playlist: Test Playlist
(youtube-dl-venv)$ echo $?
1
youtube-dl在成功下载频道时以0退出:
(youtube-dl-venv)$ youtube-dl --force-ipv4 --sleep-interval 15 --ignore-errors --no-continue --no-overwrites -r 1M https://www.youtube.com/channel/UCcHpteNEx4B221FIkGOlhnQ
[youtube:tab] UCcHpteNEx4B221FIkGOlhnQ: Downloading webpage
[download] Downloading playlist: Kaypro Due - Home
[youtube:tab] playlist Kaypro Due - Home: Downloading 1 videos
[download] Downloading video 1 of 1
[youtube:tab] UCcHpteNEx4B221FIkGOlhnQ: Downloading webpage
[download] Downloading playlist: Kaypro Due - Videos
[youtube:tab] playlist Kaypro Due - Videos: Downloading 1 videos
[download] Downloading video 1 of 1
[youtube] X2nrfbXdfRU: Downloading webpage
[youtube] X2nrfbXdfRU: Downloading MPD manifest
[download] Sleeping 15 seconds...
[download] Destination: Panasonic Whisper Series Bath Fan Slow Startup-X2nrfbXdfRU.f136.mp4
[download] 100% of 32.61MiB in 00:33
[download] Sleeping 15 seconds...
[download] Destination: Panasonic Whisper Series Bath Fan Slow Startup-X2nrfbXdfRU.f140.m4a
[download] 100% of 1.40MiB in 00:01
[ffmpeg] Merging formats into "Panasonic Whisper Series Bath Fan Slow Startup-X2nrfbXdfRU.mp4"
Deleting original file Panasonic Whisper Series Bath Fan Slow Startup-X2nrfbXdfRU.f136.mp4 (pass -k to keep)
Deleting original file Panasonic Whisper Series Bath Fan Slow Startup-X2nrfbXdfRU.f140.m4a (pass -k to keep)
[download] Finished downloading playlist: Kaypro Due - Videos
[download] Finished downloading playlist: Kaypro Due - Home
(youtube-dl-venv)$ echo $?
0
【问题讨论】:
-
我使用here 的技巧有用吗?也就是说,让脚本在后台运行
youtube-dl,捕获 SIGINT 并让陷阱同时杀死youtube-dl并注意它已被中断。
标签: bash scripting exit-code youtube-dl