Add codec checking, error-handling and automated file flushing

This commit is contained in:
Michael Reber 2020-03-23 14:50:22 +01:00
parent 3d5061d936
commit 9ad464f618
2 changed files with 108 additions and 44 deletions

View File

@ -3,14 +3,14 @@
# Ref: https://trac.ffmpeg.org/wiki/Encode/H.265
# https://github.com/rigaya/NVEnc/releases
$videoPath = 'M:\1_movies\kids.movies\Alvin und die Chipmunks (1-3)\'
$videoPath = 'M:\2_serien\_main.series\Luther\'
# HEVC : main, main10, main444
$profile = 'main10'
$NVEncoder = "$PSScriptRoot\encoder\NVEncC64.exe"
$arguments = ''
$fileTester = "$PSScriptRoot\mediainfo.exe"
if(!(Test-Path $NVEncoder -PathType leaf))
{
@ -18,25 +18,41 @@ if(!(Test-Path $NVEncoder -PathType leaf))
exit
}
# Gets Video List File Contents
# Get video list from provided path:
$videos = Get-ChildItem -Path $videoPath -Name -Recurse -Include ('*.avi', '*.mp4', '*.mkv')
$videoID = 1
$videoID = 0
$notConvertedVideos = 0
$failedVideos = 0
$count = $videos.Count
if($videos.Count -lt 1)
{
Write-Host "No videos found in: $PSScriptRoot\video_file_list.txt" -ForegroundColor Red
Write-Host "No videos found in: $videoPath" -ForegroundColor Red
exit
}
Write-Host "Converting $count videos.." -ForegroundColor Cyan
Write-Host "Found $count videos. - starting converter.." -ForegroundColor Cyan
foreach($video in $videos)
{
# Converts video using NVEncC64
$video = $video.Replace("`"", "")
$inputFile = $videoPath + $video
$outputFile = $videoPath + $video.Insert(($video.Length - 4), '(HEVC)')
# Get and check File info for codec, if it is already HEVC: - JSON link: (['media']['track'][1]['Format'])
$fileDetails = cmd /c $fileTester $inputFile --Output=JSON | ConvertFrom-Json
$codec = $fileDetails.media.track[1].Format
#echo $codec
# If not already HEVC, convert video using NVEncC64:
if($codec -ne "HEVC")
{
# Configure new file naming:
if ($video -match "x264")
{
$outputFile = $videoPath + $video.Replace("x264", "x265")
} else {
$outputFile = $videoPath + $video.Insert(($video.Length - 4), '-HEVC')
}
Write-Host
Write-Host "Converting video ($videoID of $count), please wait.." -ForegroundColor Magenta
Write-Host "$video `nto:`n$outputFile" -ForegroundColor White
@ -44,7 +60,7 @@ foreach($video in $videos)
$arguments = "--input `"$inputFile`" --codec hevc --audio-copy 1,2 --profile $profile --output `"$outputFile`""
Start-Process $NVEncoder -ArgumentList $arguments -WindowStyle Minimized
Start-Process $NVEncoder -ArgumentList $arguments #-WindowStyle Minimized
$processName = 'NVEncC64'
Start-Sleep -Seconds 8
@ -66,17 +82,65 @@ foreach($video in $videos)
$processID = (Get-Process $processName).id
Wait-Process -Id $processID
if(Test-Path $outputFile)
{
Write-Host "CONVERSION WORKED! FILE: `"$outputFile`" FOUND" -ForegroundColor Yellow
# TODO delete old and Move file name!
Write-Host "CONVERSION DONE! FILE: `"$outputFile`" FOUND" -ForegroundColor Yellow
Start-Sleep -Seconds 4
# Check if File is valid, with Duration of Video - it must be same! (['media']['track'][0]['Duration']):
$fileDetails_new = cmd /c $fileTester $outputFile --Output=JSON | ConvertFrom-Json
$StreamSize_old = $fileDetails.media.track[0].Duration
$StreamSize_new = $fileDetails_new.media.track[0].Duration
Write-Host
Write-Host "Old Duration was: $StreamSize_old `nnew Duration is: $StreamSize_new" -ForegroundColor White
Write-Host
if ($StreamSize_new -eq $StreamSize_old)
{
# Delete old video File!
Write-Host "Conversion Succesful! - Deleting old file.."
Remove-Item -Path $inputFile
} else
{
# Delete corrupt File!
Write-Host "Conversion Failded! - Deleting new converted file.."
Remove-Item -Path $outputFile
$failedVideos = $failedVideos + 1
$notConvertedVideos = $notConvertedVideos + 1
$videoID = $videoID - 1
}
}
# Increments video counter
# Increments video counter:
$videoID = $videoID + 1
Start-Sleep -Seconds 4
} else
{
# Increments not converted video counter:
$notConvertedVideos = $notConvertedVideos + 1
Start-Sleep -Seconds 4
}
Write-Host "Finished converted $count videos." -ForegroundColor Green
}
exit
# If confersion is done:
if($notConvertedVideos -eq 0)
{
Write-Host "Finished converted $videoID out of $count videos." -ForegroundColor Green
} else
{
if($failedVideos -eq 0)
{
Write-Host "Finished converted $videoID out of $count videos. - $notConvertedVideos where not converted because of already correct codec!" -ForegroundColor Green
} else
{
Write-Host "Finished converted $videoID out of $count videos. - $notConvertedVideos where not converted because error or already correct codec!!" -ForegroundColor Green
Write-Host "$failedVideos have failed!" -ForegroundColor Red
}
}
Read-Host -Prompt "Press Enter to exit"

BIN
mediainfo.exe Normal file

Binary file not shown.