在 PowerShell 中运行命令时出现问题

在 PowerShell 中运行命令时出现问题

我在 PS 脚本中拥有这个;它是对程序 Ffmpeg 的调用,带有开关、变量输入/输出文件名和重定向错误输出到文件:

C:\Users\User\Documents\Computer\FfMPEG\bin\ffmpeg -loglevel error -hide_banner -nostats -y -itsoffset 0.2 -i $videofile.Name -i $videofile.Name -map 0:0 -map 1:1 -vf yadif -c:v:0 libx264 -ar 44100 -scodec copy $newmp4filename 2> C:\Users\User\Documents\Computer\FfMPEG\ffreport.txt

错误输出以此开头:

At C:\Users\User\Documents\Computer\FfMPEG\TStoMP4.ps1:120 char:1
+ C:\Users\User\Documents\Computer\FfMPEG\bin\ffmpeg -loglevel error -h ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: ([mpeg2video @ 0...dimensions 0x0.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError 

但随后命令继续,其正常处理错误输出继续。请注意,PowerShell“未指定”错误中显示的 Ffmpeg 转换错误。脚本的变量或任何其他部分都没有问题。

它确实有效(令人惊讶),但我不确定它是否应该这样实现。我尝试使用方法Start-ProcessInvoke-Expression和,<referenced line as above> | Invoke-Expression&在使用这些 cmdlet 时遇到了问题。错误输出似乎不规则/混乱,我不知道发生了什么。

该行所在的函数如下:

function StarTrek {
# Star Trek TS to MP4 conversion and SRT extraction...
#
$videofile = Get-ChildItem Star*.ts
$newmp4filename = [io.path]::ChangeExtension($videofile.Name, '.mp4')
$newsrtfilename = [io.path]::ChangeExtension($videofile.Name, '.srt')
Stop-Transcript
Start-Transcript -Path C:\Users\User\Videos\StarTrek.log -Append
Write-Host "`nInput:" $videofile.Name # Working .ts file name
# ---INPUT FILE NAME CANNOT HAVE ANY SPACES!!!!!
#
# **** FFMPEG MP4 CONVERSION WITH OFFSET AUDIO ****
C:\Users\User\Documents\Computer\FfMPEG\bin\ffmpeg -loglevel error -hide_banner -nostats -y -itsoffset 0.2 -i $videofile.Name -i $videofile.Name -map 0:0 -map 1:1 -vf yadif -c:v:0 libx264 -ar 44100 -scodec copy $newmp4filename 2> C:\Users\User\Documents\Computer\FfMPEG\ffreport.txt
#
Write-Host "-loglevel error -hide_banner -nostats"
Write-Host "`nOutput: $newmp4filename" # Name from this function
#
# **** FFMPEG SRT EXTRACTION (offset time not needed) ****
Write-Host "`nNext below is SRT extraction..."
C:\Users\User\Documents\Computer\FfMPEG\bin\ffmpeg -loglevel error -hide_banner -nostats -y -f lavfi -i movie=$newmp4filename[out0+subcc] -vn -an -codec:s subrip -map_metadata -1 $newsrtfilename
#
Write-Host "-loglevel error -hide_banner -nostats"
Write-Host "SRT output: $newsrtfilename" # Name from this function
move star*.ts C:\Users\User\Videos\z_Hold_Trash
#
Powershell.exe -ExecutionPolicy Unrestricted -command ". C:\Users\User\Documents\Computer\FfMPEG\StarTrek_scrape_rename.ps1"
# **Moving of st*.mp4 and st*.srt happens with weekly batch file (separate task)**
Write-Host "`nExiting StarTrek function"
Stop-Transcript
#Write-Host "Exiting Task..."
powercfg /setactive 5972a947-7da1-45c0-8a89-0b0cc0cfa3be # powercfg My Custom Plan
#stop-process -name sendscroll; $ss=0 # Turn off SendScroll
&C:\Users\User\Documents\Computer\FfMPEG\SendScrollStop.exe; $ss=0
&C:\Users\User\Documents\Computer\FfMPEG\ScrollLock.exe off
#taskkill /IM sendscroll.exe /F; $ss=0
EXIT
#pause
}

上面引用的行是在 CMD 中使用的,而我天真地在 PowerShell 中做了同样的事情。更正:它是在没有2>重定向的情况下使用的。自从使用2>重定向以来,我相信Start-ProcessInvoke-Expression和方法就停止工作了。<referenced line as above> | Invoke-Expression&

我应该如何/哪种方法来最好地执行该命令?

Win10专业版, PS v.5

顺便说一句,现在注意到,Ffmeg 上可能缺少 .exe。当时也尝试/删除了双引号。

答案1

我用了:

Start-Process -NoNewWindow -Wait "C:\Users\User\Documents\Computer\FfMPEG\bin\ffmpeg.exe" -ArgumentList "-loglevel error -hide_banner -nostats -y -itsoffset 0.2 -i $videofilename -i $videofilename -map 0:0 -map 1:1 -vf yadif -c:v:0 libx264 -ar 44100 -scodec copy $newmp4filename" -RedirectStandardError "C:\Users\User\Documents\Computer\FfMPEG\ffreport.txt"

注意,在我的使用中,-NoNewWindow 和 -Wait 是必需的。另外,输入变量不能对对象使用 .Name 操作。

答案2

您正在使用的命令是以下命令:

C:\Users\User\Documents\Computer\FfMPEG\bin\ffmpeg.exe

以上是您可以在 powershell 中使用的方法,因此您的命令变成:

C:\Users\User\Documents\Computer\FfMPEG\bin\ffmpeg.exe -loglevel error -hide_banner -nostats -y -itsoffset 0.2 -i $videofile.Name -i $videofile.Name -map 0:0 -map 1:1 -vf yadif -c:v:0 libx264 -ar 44100 -scodec copy $newmp4filename 2> C:\Users\User\Documents\Computer\FfMPEG\ffreport.txt

你当然也可以这样做:

cd C:\Users\User\Documents\Computer\FfMPEG\bin\

.\ffmpeg.exe -loglevel error -hide_banner -nostats -y -itsoffset 0.2 -i $videofile.Name -i $videofile.Name -map 0:0 -map 1:1 -vf yadif -c:v:0 libx264 -ar 44100 -scodec copy $newmp4filename 2> ..\ffreport.txt

或者如果你想让它更具可读性,也可以这样:

cd C:\Users\User\Documents\Computer\FfMPEG\bin\

.\ffmpeg.exe -loglevel error `
             -hide_banner `
             -nostats `
             -y `
             -itsoffset 0.2 `
             -i $videofile.Name `
             -i $videofile.Name `
             -map 0:0 `
             -map 1:1 `
             -vf yadif `
             -c:v:0 libx264 `
             -ar 44100 `
             -scodec copy $newmp4filename `
             2> ..\ffreport.txt

注意换行符 ` 字符的使用,需要告诉 powershell 命令在下一行继续。

相关内容