我创建了一个PowerShell脚本,以将下载的视频元数据连接到单个javascript文件中(用于本地html查看器文件中)。这是我的脚本:
Write-Host "Searching for and downloading videos . . . "
if(![System.IO.File]::Exists('.\database.js')){
Set-Content -Path .\database.js -Value "const database = [
]"
}
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
$i = 0;
$stopWatch = [system.diagnostics.stopwatch]::startNew()
.\youtube-dl.exe --print-json -a urls.txt --download-archive archive.txt -o Z://Downloads/ -i | foreach {
$stream = [IO.File]::OpenWrite('.\database.js')
$stream.SetLength($stream.Length - 4)
$stream.Close()
$stream.Dispose()
$_ + ",
]" | Add-Content .\database.js
$i++
}
$elapsed = $stopWatch.Elapsed.ToString("hh\:mm\:ss")
Write-Host "Downloaded $i videos. Took $elapsed."
Read-Host -Prompt "Done, press Enter to exit"
但是,如果下载视频时出错,则JSON仍会附加到我的database.js
文件中。为了对此进行测试,我特意将输出位置设置为不存在的驱动器(Z)。我的urls.txt文件中有两个视频网址。这是我的脚本的输出:
Searching for and downloading videos . . .
ERROR: unable to create directory [WinError 3] The system cannot find the path specified: 'Z:\\'
ERROR: unable to create directory [WinError 3] The system cannot find the path specified: 'Z:\\'
Downloaded 2 videos. Took 00:00:04.
Done, press Enter to exit:
youtube-dl正确识别下载视频时出错,并且视频ID未添加到archive.txt
。但是,仍将打印JSON并将其添加到database.js
中。如何检测youtube-dl何时遇到错误,因此JSON未添加到datbase.js
?