微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何区分 youtube-dl 获得 ctrl-c 和在 shell 脚本中获得一些其他错误?

如何解决如何区分 youtube-dl 获得 ctrl-c 和在 shell 脚本中获得一些其他错误?

我有一组用于归档 YouTube 频道的脚本,其中包括

  1. 一个 awk 脚本,它读取通道列表和存档参数并使用 system() 运行...
  2. ...一个 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

解决方法

您可以将输出重定向到某个临时文件并检查该文件中的 Interrupted by user 字符串:

$ youtube-dl ... 2> >(tee /tmp/sometempfile.txt >&2)
....
^C
ERROR: Interrupted by user
$ grep 'ERROR: Interrupted by user' /tmp/sometempfile.txt

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。