首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell在FileSystemWatcher中显示来自HandBrakeCLI的适当输出数据

PowerShell在FileSystemWatcher中显示来自HandBrakeCLI的适当输出数据
EN

Stack Overflow用户
提问于 2016-03-28 05:23:33
回答 1查看 547关注 0票数 0

我正在编写一个简单的脚本,用于监视文件夹中的大型视频文件,如果它更大,那么它应该将其发送到HandBrake进行转换。

脚本的主要功能已经完成并或多或少地工作了,但我在显示来自HandBrake的适当输出时遇到了困难。

以下是我到目前为止所写的:

代码语言:javascript
复制
$Folder             = "E:\Series"                                   #Folder to be monitored for large video files.
$Output             = "E:\Encoded"                                  #Folder where encoding jobs go to, and that will be monitored for completed jobs to replace the original file with.
$MaxMB          = "15"                                          #Max MB a minute
$MI_CLI         = "C:\Program Files\MediaInfoCLI\MediaInfo.exe" #Location oo MediaInfoCLI
$HB_CLI             = "C:\Program Files\Handbrake\HandBrakeCLI.exe" #Location of HandBrakeCLI
$HB_Container       = "Copy"                                        #HandBrake container output


$Filter             = '*.*'     
Write-Host "Monitoring "
$fsw = New-Object IO.FileSystemWatcher $Folder, $Filter 
$fsw.IncludeSubdirectories = $true          
Register-ObjectEvent $fsw Changed -SourceIdentifier FileUpdated -Action {
    Start-Sleep -s 1
    $FilePath =  $EventArgs.FullPath
    $FileName =  $FilePath.split('\')[-1]
    if($FilePath -imatch '\.(?:mp4|mkv)'){
        if((Test-Path -LiteralPath $filePath) -and -not (Test-Path -LiteralPath "$Output\$FileName")){
            if(Test-FileReady $FilePath){
                $fileSize = (Get-Item $FilePath).length
                if($fileSize -ge 734003200){
                    Write-Host ""
                    Write-Host "Large Video Detected: `"$($FileName)`""
                    Write-Host "Sending To HandBrake..."
                    HB-Convert $FilePath $Output
                }
             }
         }
    }
}


function HB-Convert{
    param ([parameter(Mandatory=$true)][string]$source,[parameter(Mandatory=$true)][string]$dest)
    if(-not (Test-Path $source) -or -not (Test-Path $dest)) {return}
    $FileName =  $source.split('\')[-1]
    start-process $HB_CLI -ArgumentList "-i `"$source`" -t 13 --angle 1 -c 1 -o `"$dest\$FileName`"  -f mkv  -w 1280 --crop 0:0:0:0 --loose-anamorphic  --modulus 2 -e x265 -q 20 --vfr -a 1 -E copy -6 dpl2 -R Auto -B 160 -D 0 --gain 0 --audio-fallback ac3 --encoder-preset=faster  --verbose=0 2> log.txt" -wait -nonewwindow
    #HandBrakeCLI -i `"$source`" -t 13 --angle 1 -c 1 -o `"$dest\$FileName`"  -f mkv  -w 1280 --crop 0:0:0:0 --loose-anamorphic  --modulus 2 -e x265 -q 20 --vfr -a 1 -E copy -6 dpl2 -R Auto -B 160 -D 0 --gain 0 --audio-fallback ac3 --encoder-preset=faster  --verbose=0 2> log.txt
}


function Test-FileReady {
        Param([parameter(Mandatory=$true)]$path)
        if (Test-Path  -LiteralPath $path) {
            trap {
                return $false
            }
            $stream = New-Object system.IO.StreamReader $path
            if ($stream) {
                $stream.Close()
                return $true
            }
        }
}

现在在中,HB-转换函数中有2行调用HandBrakeCLI。

一次通过启动进程

还有一个使用HandBrakeCLI(I的用户在我的系统环境变量中添加了手制动器dir )

后一项现在已被划出。

当我在命令提示符中手动调用HB-转换(使用HandBrakeCLI,而不是启动进程)时,一切都像它一样工作,应该只显示手刹的进度。

编码:任务1,74.66 % (26.78 fps,avg 47.17 fps,ETA 00h05m40)

现在,当通过FileSystemWatcher调用它时,它将不会显示手制动中的任何内容,它将只显示来自它自己的脚本的输出。

检测到大视频:"FileName“ 发送到HandBrake..。

它将挂在那里,直到编码完成。

现在,当FileSystemWatcher调用HB-转换与启动进程手制动器将输出所有数据,而不仅仅是进展,这是非常恼人的。

那么,当它被称为真FileSystemWatcher时,我如何才能让它只显示进度。

几个小时以来,我一直在努力让它工作,它让我发疯了。希望这里有人能修好它。

对此,我只学到了一点PS,所以当我说我是PS中的一个核心时,这是一种轻描淡写的说法。

EN

回答 1

Stack Overflow用户

发布于 2016-03-29 00:06:33

好的,我开始工作了

将其添加到脚本中

代码语言:javascript
复制
function global:HB-UpdateProgress{
  Process{
    $position = $host.ui.rawui.cursorposition
    $position.X = 0
    $host.ui.rawui.cursorposition = $position
    Write-Host @($input)[0] -NoNewline
  }
}

然后在调用HandBrakeCLI时使用它作为管道正确地显示输出

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36256473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档