2016-03-28 57 views
0

我正在處理一個簡單的腳本,監視大型視頻文件的文件夾,如果它大於設置的大小,它應該將它發送給HandBrake進行轉換。PowerShell顯示來自HandBrakeCLI裏面正確的輸出數據FileSystemWatcher

該腳本的主要功能已完成,工作或多或少,但我無法正確顯示來自HandBrake的輸出。

這是我到目前爲止寫:

$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啓動過程
和一個(我已經添加手剎DIR到我的系統環境變量)
後者現在被標記出來。

當我在命令提示符下手動調用HB-Convert(使用HandBrakeCLI而不是Start-Process)時,所有工作都像是應該只顯示手剎的進度。

編碼:任務1中1,74.66%(26.78幀,平均47.17幀,ETA 00h05m40s)

現在,當這是通過FileSystemWatcher的叫它不會顯示從手剎任何它會只顯示從腳本它自我

大型視頻檢測的輸出:「文件名」
發送手剎......

它會掛在那裏,直到編碼完成

現在當FileSystemWatcher的調用與開始處理手剎HB-轉換將輸出的所有數據不只是進度女巫是很煩人的。

所以我怎麼會得到它只顯示進度時,其稱爲真正的FileSystemWatcher。

我一直在努力讓它工作幾個小時,它驅使我瘋了。希望這裏有人能解決它。
我只學會了這一點PS的,所以當我說我在PS一個結點那是一個輕描淡寫:d

回答

0

好,我得到它的工作

加入這劇本

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

然後使用它作爲管道時調用HandBrakeCLI將正確顯示輸出