这个 youtube-dl 自动脚本有什么问题? [英] What's wrong with this youtube-dl automatic script?
问题描述
首先,我对 bash 脚本很陌生,我刚刚开始学习,显然这个脚本有问题,但我不知道它是什么......
first of all, I'm quite new with bash scripting and I'm just starting to learn, evidently there's something wrong with this script, but I don't know what it is...
我创建了一个 bash 脚本来使用 youtube-dl 自动下载视频:
I created a bash script to automate downloading videos with youtube-dl:
#!/bin/bash
echo url:
read url
export url
youtube-dl -f 'bestvideo[height<=360]+worstaudio/worst[height<=360]' $url
这个想法是我在命令行中输入脚本的名称,例如:360",它会要求一个 URL(例如:一个 Youtube 视频),我粘贴它,youtube-dl 用规定的参数.它就像一个魅力......
The idea is that I type in the command line the name of the script, e.g.: "360" and it will ask for a url (e.g.: a Youtube video), I paste it and youtube-dl downloads it with the stated parameters. It works like a charm...
现在,我想让脚本更复杂,我想我需要将 youtube-dl 命令转换为变量(当然,作为一个新手,我可能是错的,但让我们假设我适合瞬间……)
Now, I want to make the script more complex and I think I need to convert the youtube-dl command to a variable (of course, being a newbie, I might be wrong, but let's assume I'm right for a moment...)
#!/bin/bash
video="youtube-dl -f 'bestvideo[height<=360]+worstaudio/worst[height<=360]'"
echo url:
read url
export url
$video $url
当我尝试这个时,它给我一个错误:错误:请求的格式不可用"我不知道出了什么问题......我想以尽可能少的代码更改来解决问题,我再说一遍,我想知道当前代码有什么问题,以便我可以从中学习.
When I try this, it throws me an error: "ERROR: requested format not available " I don't know what's wrong... I'd like to solve the problem with the least changes to the code as possible and I repeat, I'd like to know what's wrong with the current code so I can learn from it.
在此先非常感谢您!
推荐答案
这里有详细解释:我试图将命令放入变量中,但复杂的情况总是失败!
首先总是双引号你的变量,除非你确切地知道如果你不知道会发生什么.
First always double-quote your variables, unless you know exactly what will happen if you don't.
您不需要export
那个变量:您不需要调用任何其他需要使用它的程序.
You don't need to export
that variable: you're not invoking any other program that needs to use it.
当你想重用一个命令时,考虑把它放在一个函数中:
When you want to re-use a command, think about putting it in a function:
#!/bin/bash
function video {
youtube-dl -f 'bestvideo[height<=360]+worstaudio/worst[height<=360]' "$1"
}
read -p "url: " url
video "$url"
实际上,我会这样做:
- 将该函数添加到您的 ~/.bashrc 中,
- 源文件:
source ~/.bashrc
然后你可以从命令行使用它:
- add that function to your ~/.bashrc,
- source that file:
source ~/.bashrc
then you can use it from the command line:
video 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
这篇关于这个 youtube-dl 自动脚本有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!