如果 Ubuntu 论坛不适合讨论这个问题,我很抱歉。首先我要说的是,我是一名初级程序员。我对 bash 了解不少,但并不比普通人多多少。我目前正在尝试编写一个脚本,以便以有序的方式下载整个 youtube 频道,并带有标签文件夹。
这是我的脚本,尚未完成。
#!/bin/bash
# script for downloading youtube channels.
youtube-dl --format best --ignore-errors --geo-bypass --no-overwrites --write-description --verbose --output "home/(myname)/Videos/Youtube/%(title)s-%(id)s.%(ext)s"
我的问题是:
我测试了这个脚本,但它似乎不起作用,尽管输出它实际上已经起作用了。我指定的文件夹是空的。
我怎样才能让它在命令行中接受参数,而不必将其写入文本文件本身?我想我必须对 stdin 做些什么,对吗?
我可以把不同的选项放在不同的行上吗?我试过了,但输出非常奇怪。如果需要的话我会发布它。
感谢您的帮助。非常感谢您提供改进建议!
输出如下:
[debug] System config: []
[debug] User config: []
[debug] Custom config: []
[debug] Command-line args: [u'https://www.youtube.com/watch?v=5Z0wEVxL30Q', u'--format', u'best', u'--ignore-errors', u'--geo-bypass', u'--no-overwrites', u'--write-description', u'--verbose', u'--output', u'home/daniel/Videos/Youtube/%(title)s-%(id)s.%(ext)s']
[debug] Encodings: locale UTF-8, fs UTF-8, out UTF-8, pref UTF-8
[debug] youtube-dl version 2019.09.28
[debug] Python version 2.7.17 (CPython) - Linux-5.0.0-37-generic-x86_64-with-Ubuntu-18.04-bionic
[debug] exe versions: ffmpeg 3.4.6, ffprobe 3.4.6, phantomjs 2.1.1, rtmpdump 2.4
[debug] Proxy map: {}
[youtube] 5Z0wEVxL30Q: Downloading webpage
[youtube] 5Z0wEVxL30Q: Downloading video info webpage
[youtube] {18} signature length 108, html5 player vfl22ubNH
[youtube] {22} signature length 108, html5 player vfl22ubNH
[youtube] {43} signature length 108, html5 player vfl22ubNH
[youtube] {137} signature length 104, html5 player vfl22ubNH
[youtube] {248} signature length 108, html5 player vfl22ubNH
[youtube] {136} signature length 108, html5 player vfl22ubNH
[youtube] {247} signature length 108, html5 player vfl22ubNH
[youtube] {135} signature length 108, html5 player vfl22ubNH
[youtube] {244} signature length 104, html5 player vfl22ubNH
[youtube] {397} signature length 108, html5 player vfl22ubNH
[youtube] {134} signature length 104, html5 player vfl22ubNH
[youtube] {243} signature length 108, html5 player vfl22ubNH
[youtube] {396} signature length 104, html5 player vfl22ubNH
[youtube] {133} signature length 108, html5 player vfl22ubNH
[youtube] {242} signature length 104, html5 player vfl22ubNH
[youtube] {395} signature length 108, html5 player vfl22ubNH
[youtube] {160} signature length 108, html5 player vfl22ubNH
[youtube] {278} signature length 104, html5 player vfl22ubNH
[youtube] {394} signature length 108, html5 player vfl22ubNH
[youtube] {140} signature length 108, html5 player vfl22ubNH
[youtube] {249} signature length 104, html5 player vfl22ubNH
[youtube] {250} signature length 108, html5 player vfl22ubNH
[youtube] {251} signature length 108, html5 player vfl22ubNH
[youtube] {256} signature length 104, html5 player vfl22ubNH
[youtube] {258} signature length 108, html5 player vfl22ubNH
[info] Writing video description to: home/daniel/Videos/Youtube/Skyfall - Glass Marksman Shot (1080p)-5Z0wEVxL30Q.description
[debug] Invoking downloader on u'https://r1---sn-5goeen7r.googlevideo.com/videoplayback?expire=1577358216&ei=KD8EXpPXBIrFyQWU-YywAg&ip=141.98.255.143&id=o-AKv0MWwSrx9RvyT95fq0Q_YtOhrP5TbXi6Q0sjSMgIjX&itag=22&source=youtube&requiressl=yes&mm=31%2C29&mn=sn-5goeen7r%2Csn-5go7yne6&ms=au%2Crdu&mv=m&mvi=0&pl=24&initcwndbps=1542500&mime=video%2Fmp4&ratebypass=yes&dur=230.016&lmt=1576681868572996&mt=1577336499&fvip=1&fexp=23842630&c=WEB&txp=5535432&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cmime%2Cratebypass%2Cdur%2Clmt&lsparams=mm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AHylml4wRAIgP-oMuqfqPY881XjbcdN6zXRrP_J9JyvEgV4IycmrZ5YCIFbDK3B6ZjlE9bidVpMUKgrGPB8gwPoT7eHsc0CwS-J0&sig=ALgxI2wwRQIhALG8pUVKSEm97ndTb_i8Vd_uqcVshLbbbfNuYoV9PBWLAiAy92m5Qqh3i0DK05AdaCbQohVT8nNtam_Pf-cYKDJRPg=='
[download] Destination: home/daniel/Videos/Youtube/Skyfall - Glass Marksman Shot (1080p)-5Z0wEVxL30Q.mp4
[download] 100% of 15.78MiB in 00:39
答案1
您应该向脚本传递一些参数
参见:https://stackoverflow.com/questions/18003370/script-parameters-in-bash
示例脚本如下所示ytchandl.sh
:
#!/bin/bash
# If no Argument found then don't proceed:exit
if [ -z "$1" ]; then exit 1; fi
ChanURL=$1
youtube-dl -f best --geo-bypass --write-description -civw \
-o "/home/$USER/Video/Youtube/%(title)s-%(id)s.%(ext)s" \
"$ChanURL"
然后使用以下方式调用脚本
./ytchandl.sh https://www.youtube.com/user/ubuntudevelopers
当然,您可以对参数添加更多测试来检查 URL 是否存在等等...
您也可以使用嵌入uploader
和playlist
输出模板
获取更多信息:http://manpages.ubuntu.com/manpages/bionic/man1/youtube-dl.1.html
# Download all playlists of YouTube channel/user keeping each playlist in separate directory:
$ youtube-dl -o '%(uploader)s/%(playlist)s/%(playlist_index)s - %(title)s.%(ext)s' https://www.youtube.com/user/ubuntudevelopers/playlists