PowerShell标准错误输出中的随机换行符

z32*_*7ul 5 powershell powershell-1.0 io-redirection redirectstandardoutput handbrake

我想用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"
Run Code Online (Sandbox Code Playgroud)

批处理文件:

"%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"
Run Code Online (Sandbox Code Playgroud)

两个脚本都创建相同的.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
Run Code Online (Sandbox Code Playgroud)

批处理文件:

[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
Run Code Online (Sandbox Code Playgroud)

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

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

问题:如果没有这些随机换行符,我可以做些什么来从PowerShell获取标准错误输出?

Bur*_*ris 1

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

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

我不确定这方面的具体细节,但我很高兴听到它似乎为您解决了问题,就像为我解决了问题一样。