5

我想使用 HandBrake 将许多 .iso 文件转换为 .mp4,所以我尝试使用命令行界面。我宁愿在 powershell 而不是批处理文件中为此编写脚本。但是,如果我使用 powershell,标准错误会在随机位置包含换行符。

为了进行故障排除,我在 powershell 和批处理中创建了一个简化的脚本。

电源外壳:

& "$Env:ProgramFiles\HandBrake\HandBrakeCLI.exe" @(
    '--input', 'V:\',
    '--title', '1', '--chapter', '1',
    '--start-at', 'duration:110', '--stop-at', 'duration:15',
    '--output', 'pmovie.mp4',
    '--format', 'av_mp4'
    ) > ".\pstd.txt" 2> ".\perr.txt"

批处理文件:

"%ProgramFiles%\HandBrake\HandBrakeCLI.exe" --input V:\ --title 1 --chapter 1 --start-at duration:110 --stop-at duration:15 --output ".\cmovie.mp4" --format av_mp4 > ".\cstd.txt" 2> ".\cerr.txt"

两个脚本都创建相同的 .mp4 文件,不同之处仅在于它们创建的标准错误输出:

电源外壳:

HandBrakeCLI.exe : [10:41:44] hb_init: starting libhb thread
At C:\Test\phandbrake.ps1:1 char:2
+ & <<<<  "$Env:ProgramFiles\HandBrake\HandBrakeCLI.exe" @(
    + CategoryInfo          : NotSpecified: ([10:41:44] hb_i...ng libhb thread 
   :String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

[10:41:44] thread 541fc20 started ("libhb")
HandBrake 1.1.2 (2018090500) - MinGW x86_64 - https://handbrake.fr
8 CPUs detected

O
pening V:\...

[10:41:44] CPU: Intel(R) Core(TM) i7-2600K CPU @ 3.40GHz

[10:41:44]  - Intel microarchitecture Sandy Bridge
[10:41:44]  - logical processor count: 8

[10:41:44] Intel Quick Sync Video support: no

[10:41:44] hb_scan: path=V:\, title_index=1

src/libbluray/disc/disc.c:424: error opening file BDMV\index.bdmv

src/libbluray/disc/disc.c:424: error opening file BDMV\BACKUP\index.bdmv

[10:41:44] bd: not a bd - trying as a stream/file instead

libdvdnav: Using dvdnav version 6.0.0

l
ibdvdnav: Unable to open device file V:\.
libdvdnav: vm: dvd_read_name failed
libdvdnav: DVD disk re
ports i
tself wi
th Region mask 0x
0000000
0. Reg
ions:
 1 2 3 4 5 
6 7 8

批处理文件:

[10:41:35] hb_init: starting libhb thread
[10:41:35] thread 5a2cc30 started ("libhb")
HandBrake 1.1.2 (2018090500) - MinGW x86_64 - https://handbrake.fr
8 CPUs detected
Opening V:\...
[10:41:35] CPU: Intel(R) Core(TM) i7-2600K CPU @ 3.40GHz
[10:41:35]  - Intel microarchitecture Sandy Bridge
[10:41:35]  - logical processor count: 8
[10:41:35] Intel Quick Sync Video support: no
[10:41:35] hb_scan: path=V:\, title_index=1
src/libbluray/disc/disc.c:424: error opening file BDMV\index.bdmv
src/libbluray/disc/disc.c:424: error opening file BDMV\BACKUP\index.bdmv
[10:41:35] bd: not a bd - trying as a stream/file instead
libdvdnav: Using dvdnav version 6.0.0
libdvdnav: Unable to open device file V:\.
libdvdnav: vm: dvd_read_name failed
libdvdnav: DVD disk reports itself with Region mask 0x00000000. Regions: 1 2 3 4 5 6 7 8

libdvdread: Attempting to retrieve all CSS keys
libdvdread: This can take a _long_ time, please be patient

libdvdread: Get key for /VIDEO_TS/VIDEO_TS.VOB at 0x00000130
libdvdread: Elapsed time 0

这让我很困扰,因为我想检查这些文本文件以确保在编码过程中没有错误。

我想这可能与写入同一流的线程之间缺乏同步有关,但我不确定。

问题:如果没有这些随机换行符,我该怎么做才能从 PowerShell 获取标准错误输出?

4

3 回答 3

1

您可以尝试使用Start-Process带有-RedirectStandardError
-RedirectStandardInput-Wait选项的命令。

与大多数 shell 一样,这些-Redirect...选项直接将 OS 级别的 I/O 重定向到目标文件。Start-Process据我了解,这不是 PowerShell 尖括号重定向的工作方式,而是尖括号将输出通过另一个 PowerShell 管道传递,使用Write-File(或其他)在它接收的字符串之间插入换行符。

我不确定这件事的确切细节,但我很高兴听到它似乎为你解决了这个问题,就像它对我一样。

于 2018-10-08T01:01:28.407 回答
1

我认为这里的问题是控制台有一定的宽度,控制台本身基本上被重定向到一个文件。

我对此的解决方案是将输出直接重定向到管道,使用:

2>&1 #Interpreted by the console
2>&1 | x #Output directly to x

然后使用Out-File可用的-Width参数:

$(throw thisisnotsometthingyoucanthrowbutisinfactaverylongmessagethatdemonstratesmypoint) 2>&1 |
 Out-File "test.txt" -Width 10000

在这种情况下,powershell 将在换行之前写入 10,000 个字符。

但是,您也有一些奇怪的换行符,我现在无法复制。也就是说,既然您知道如何通过管道发送输出,您可以使用其他方法来删除换行符。

例如,您可以使用此功能打印出导致换行符的确切控制字符。

$(throw error) 2>&1 | Out-String | Debug-String

然后,您可以检查输出并替换问题字符,如下所示:

$(throw error) 2>&1 | Out-String | % {$_ -replace "`r"} | Out-File "test.txt" -Width 10000
于 2018-10-08T15:43:02.693 回答
0

Burt Harris 的有用回答向您展示了一种避免该问题的方法,即 via Start-Process,但它要求您从根本上以不同的方式构建命令。

如果等效批处理文件产生的输出足够,则有一种更简单的方法:只需调用cmd /c并让cmd处理输出重定向,就像在批处理文件中一样:

cmd /c "`"`"$Env:ProgramFiles\HandBrake\HandBrakeCLI.exe`"`"" @(
    '--input', 'V:\',
    '--title', '1', '--chapter', '1',
    '--start-at', 'duration:110', '--stop-at', 'duration:15',
    '--output', 'pmovie.mp4',
    '--format', 'av_mp4'
    ) '> .\pstd.txt 2> .\perr.txt'

请注意如何将两个输出重定向作为单个带引号的字符串传递,以确保它们由 PowerShell 解释,cmd.exe而不是由 PowerShell 解释。

另请注意可执行路径周围嵌入的转义双引号 ( `"),以确保cmd.exe将整个路径视为单个双引号字符串。


至于您看到的额外换行符:我没有具体解释,但我可以告诉您在 PowerShell 中的不同方式>2>工作方式- 与cmd.exe(批处理文件)和Start-Processwith相比-RedirectStandard*

  • cmd.exe的重定向运算符 ( >) 将原始字节写入指定的目标文件,无论是在重定向 stdout (只是>或,明确地,1>)和 stderr ( 2>) 时;因此,外部程序输出的文本如原样HandBrakeCLI.exe传递。

  • Start-Process,它在后台使用 .NET API,在指定-RedirectStandardOutput和/或-RedirectStandardError参数时基本相同。

相比之下,Powershell 自己的>运算符功能不同:

  • PowerShell - 在内部(调用本机 PowerShell 命令时),它使用 PowerShell 丰富的输出格式化系统将输入对象(还不是字符串)转换为字符串,然后使用下面详述的字符编码将它们发送到输出文件。

  • 从外部程序接收的输出假定为text,其编码默认为系统的 OEM 字符编码,反映在[console]::OutputEncoding和中chcp。解码后的文本逐行加载到 .NET 字符串(本质上基于 UTF-16)中。

    • 对于重定向的stdout输出,这些字符串在输出到目标文件时被重新编码,默认使用以下编码:

      • Windows PowerShell:UTF-16LE(“Unicode”)
      • PowerShell 核心:没有 BOM的 UTF-8
      • 注意:只有在 Windows PowerShell v5.1 或更高版本和 PowerShell Core 中,您才能更改这些默认值 - 有关详细信息,请参阅此答案
    • 相比之下,当通过流(PowerShell 的错误流)重定向stderr输出时,字符串在输出之前被包装在错误对象(类型的实例)中,并且生成的对象根据 PowerShell 的输出格式系统转换为字符串,并且与上述相同的字符编码应用于目标文件的输出。2[System.Management.Automation.ErrorRecord]

      • 您可以在输出中看到包含额外信息和行的证据,例如HandBrakeCLI.exe : [10:41:44] hb_init: starting libhb threadand At C:\Test\phandbrake.ps1:1 char:2, ...
      • 这也意味着可以引入额外的换行符,因为输出格式系统生成的文本假定基于控制台窗口宽度的固定行宽。
      • 也就是说,这并不能解释你的情况下奇怪的换行符。
于 2018-10-14T19:57:26.793 回答