为什么我的 powershell 脚本没有正确包装我的路径?

时间:2021-04-01 06:18:45

标签: windows powershell

我一直在修改 Powershell 脚本来自动化一些 Streamlink 的东西,而且一切顺利,但我在 streamlink 传递命令的方式上遇到了问题,特别是在包装文件路径方面。< /p>

这是代码(请原谅糟糕的格式,我不是专业人士,我只是初学者。)

Set-Location (Split-Path $MyInvocation.MyCommand.Path)
$scriptdir = Split-Path $MyInvocation.MyCommand.Path
$streamurl = Read-Host -Prompt 'Stream URL'
$channel = youtube-dl --cookies $scriptdir\Resources\cookies.txt --write-description --skip-download -o "%(uploader)s" --get-filename $streamurl
if (-not (Test-Path -LiteralPath $scriptdir\Recordings\$channel\)) {
    
    try {
        New-Item -Path $scriptdir\Recordings\$channel\ -ItemType Directory -ErrorAction Stop | Out-Null #-Force
    }
    catch {
        Write-Error -Message "Unable to create directory '$channel'. Error was: $_" -ErrorAction Stop
    }
    "Successfully created directory '$channel'."

}
streamlink-auth $scriptdir\Resources\cookies.txt --retry-streams 10 -o "$scriptdir\Recordings\$channel\stream.mp4" $streamurl best
$scriptdir = Where the script is running
$streamurl = Self explanatory
$channel = the channel name of the stream

我遇到的问题是,当将此命令传递给 streamlink 时,它似乎包装错误,因为当 streamlink 尝试运行该命令时,我收到以下错误:

streamlink args:  --retry-streams 10 -o W:\recordings\Recordings\【LIVE】新宿 大ガード交差点 Tokyo Shinjuku Live Ch\stream.mp4 https://www.youtube.com/watch?v=RQA5RcIZlAM best
usage: streamlink [OPTIONS] <URL> [STREAM]
streamlink: error: unrecognized arguments: Shinjuku Live Ch\stream.mp4 https://www.youtube.com/watch?v=RQA5RcIZlAM best

来源:【LIVE】新宿大ガード交差点东京新宿Live Ch

我还使用全英文频道(使用相同的脚本)对此进行了测试,结果如下:

streamlink args: --retry-streams 10 -o W:\recordings\Recordings\Watson Amelia Ch. hololive-EN\stream.mp4 https://www.youtube.com/watch?v=EeC6OHBRFTc best
usage: streamlink [OPTIONS] [STREAM]
streamlink: error: unrecognized arguments: hololive-EN\stream.mp4 https://www.youtube.com/watch?v=EeC6OHBRFTc best

资料来源:Watson Amelia Ch。 hololive-CN

似乎虽然可以很好地创建目录,但将变量传递到 streamlink 命令中会失败。这可以通过简单地移动到频道目录来解决:

cd $scriptdir\Recordings\$channel\

并在路径中没有额外变量的情况下运行命令,但这不是我想要的方式。我还想补充一点,youtube-dl 将文件写入使用此脚本创建的目录没有问题,因为我的后处理脚本可以毫无问题地在那里写入文件,而且 powershell 仍然可以删除那里的文件。

我已经问过 streamlink 的人,肯定不是他们的结局,所以很明显这是一个 powershell 的东西。

我该如何解决这个问题,因为我现在真的被难住了。

1 个答案:

答案 0 :(得分:1)

我很笨。

事实证明,当您将某些内容传递给命令时,必须先使用双引号传递,然后使用单引号传递。

所以代替:

streamlink-auth $scriptdir\Resources\cookies.txt --retry-streams 10 -o "$scriptdir\Recordings\$channel\stream.mp4" $streamurl best

必须是:

streamlink-auth $scriptdir\Resources\cookies.txt --retry-streams 10 -o "'$scriptdir\Recordings\$channel\stream.mp4'" $streamurl best

抱歉我是个笨蛋。

相关问题