2020-03-06 00:43:30 +01:00
# Converts videos to HEVC for given path:
# https://github.com/rigaya/NVEnc/releases
2020-03-23 16:45:28 +01:00
#-----------------------------------------------------------------------
# Edit the $videoPath variable to point to your video-files folder:
2020-04-09 15:39:52 +02:00
$videoPath = 'M:\2_serien\_main.series\'
2020-03-05 20:48:33 +01:00
2020-03-23 16:45:28 +01:00
#-----------------------------------------------------------------------
# HEVC profiles: main, main10, main444
2020-03-06 00:43:30 +01:00
$profile = 'main10'
2020-03-05 20:48:33 +01:00
2020-03-06 00:43:30 +01:00
$NVEncoder = " $PSScriptRoot \encoder\NVEncC64.exe "
2020-03-23 14:50:22 +01:00
$fileTester = " $PSScriptRoot \mediainfo.exe "
2020-03-05 20:48:33 +01:00
2020-03-25 18:30:50 +01:00
#############################################################################################################
# Testing and conversion preparations #
#############################################################################################################
2020-03-06 00:43:30 +01:00
if ( ! ( Test-Path $NVEncoder -PathType leaf ) )
2020-03-05 20:48:33 +01:00
{
2020-03-06 00:43:30 +01:00
Write-Host " NVEncC64.exe not found, please check path in `" $NVEncoder `" . " -ForegroundColor Yellow
2020-04-09 15:39:52 +02:00
Read-Host -Prompt " Press Enter to exit "
2020-03-05 20:48:33 +01:00
exit
}
2020-03-23 14:50:22 +01:00
# Get video list from provided path:
2020-03-25 23:27:09 +01:00
$videos = Get-ChildItem -LiteralPath $videoPath -Name -Recurse -Include ( '*.mp4' , '*.mkv' ) #('*.avi', '*.mp4', '*.mkv') - avi currently doesn't work.
2020-03-25 18:30:50 +01:00
# Instantiating used variables:
2020-03-25 12:06:48 +01:00
$videoID = 1
2020-04-15 16:37:38 +02:00
$convertedVideos = 1
2020-03-23 14:50:22 +01:00
$notConvertedVideos = 0
$failedVideos = 0
2020-03-25 18:30:50 +01:00
# Square brackets are used as wildcards in Powershell.
# To recognize this as normal text, the LogWite function uses the -LiteralPath parameter instead of the -Path parameter.
$logFolderName = $ ( ( Get-Date ) . tostring ( " dd-MM-yyyy " ) + ' - [run started at ' + ( Get-Date ) . tostring ( " HH-mm " ) + ']' )
2020-03-05 20:48:33 +01:00
$count = $videos . Count
2020-03-25 18:30:50 +01:00
2020-03-05 20:48:33 +01:00
if ( $videos . Count -lt 1 )
{
2020-03-23 14:50:22 +01:00
Write-Host " No videos found in: $videoPath " -ForegroundColor Red
2020-04-09 15:39:52 +02:00
Read-Host -Prompt " Press Enter to exit "
exit
2020-03-05 20:48:33 +01:00
}
2020-03-25 18:30:50 +01:00
# Log write function:
Function LogWrite
{
Param ( [ string ] $logstring )
Add-content -LiteralPath $Logfile -value $logstring
}
# Create new log directory:
New-Item -itemType Directory -Path $PSScriptRoot \ logs \ -Name $logFolderName > $null
2020-03-25 23:27:09 +01:00
# Calculates the total size of the given directory in GB:
Write-Host " Inizialization in progress.. "
$folderSizeInGB = " {0:N2} GB " -f ( ( Get-ChildItem $videoPath -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop ) . Sum / 1 GB )
Clear-Host # Clears the upper message from the window
2020-03-25 18:30:50 +01:00
#############################################################################################################
# END - Testing and conversion preparations
2020-03-05 20:48:33 +01:00
2020-03-25 18:30:50 +01:00
#############################################################################################################
# Main conversion process #
#############################################################################################################
2020-03-25 23:27:09 +01:00
Write-Host " Found $count videos with a total size of $folderSizeInGB - starting converter.. " -ForegroundColor Cyan
2020-03-25 12:06:48 +01:00
Write-Host
Write-Host
Write-Host " ------------------------------------------------------------------------------------------------ "
2020-03-05 20:48:33 +01:00
foreach ( $video in $videos )
{
2020-03-23 14:50:22 +01:00
2020-03-05 20:48:33 +01:00
$video = $video . Replace ( " `" " , " " )
2020-03-06 00:43:30 +01:00
$inputFile = $videoPath + $video
2020-03-23 16:45:28 +01:00
# Get and check File info for codec, if it's already HEVC: - JSON query: (['media']['track'][1]['Format'])
2020-03-23 14:50:22 +01:00
$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:
2020-04-09 15:39:52 +02:00
if ( $video -match " 264 " )
2020-03-23 14:50:22 +01:00
{
2020-04-09 15:39:52 +02:00
$outputFile = $videoPath + $video . Replace ( " 264 " , " 265 " )
2020-03-23 14:50:22 +01:00
} else {
$outputFile = $videoPath + $video . Insert ( ( $video . Length - 4 ) , '-HEVC' )
}
2020-03-25 12:06:48 +01:00
Write-Host " Analyzing video ( $videoID of $count ), starting convertion of number: $convertedVideos please wait.. " -ForegroundColor Magenta
2020-03-23 14:50:22 +01:00
Write-Host " $video `n to: `n $outputFile " -ForegroundColor White
Write-Host
2020-03-25 12:06:48 +01:00
2020-03-25 18:30:50 +01:00
# If the Subtitles should not be copied, delete the "--sub-copy 1,2" argument.
$arguments = " --input `" $inputFile `" --codec hevc --audio-copy 1,2 --sub-copy 1,2 --profile $profile --output `" $outputFile `" "
2020-03-23 14:50:22 +01:00
2020-03-23 16:45:28 +01:00
Start-Process $NVEncoder -ArgumentList $arguments -WindowStyle Minimized
2020-03-05 20:48:33 +01:00
2020-03-23 14:50:22 +01:00
$processName = 'NVEncC64'
Start-Sleep -Seconds 8
# Sets to use 3 cores (always set to one less core that your CPU has)
# 2 Cores = 3, 3 Cores = 7, 4 cores = 15, 5 cores = 31, 6 cores = 63
# Code to calculate for your CPU:
# $noOfCores = Get-WmiObject Win32_Processor | Measure-Object NumberOfLogicalProcessors -Sum
# $noOfCores.Sum = $noOfCores.Sum - 1
# [math]::Pow(2,$($noOfCores).Sum) - 1
#
$process = Get-Process $processName ; $process . ProcessorAffinity = 7
Start-Sleep -Seconds 8
# Sets priorty to High
# Values: High, AboveNormal, Normal, BelowNormal, Low
$process = Get-Process -Id $process . Id
$process . PriorityClass = 'High'
# Waits for process to complete
$processID = ( Get-Process $processName ) . id
Wait-Process -Id $processID
2020-03-25 23:27:09 +01:00
if ( Test-Path -LiteralPath $outputFile )
# Specifies a path to be tested. Unlike Path, the value of the LiteralPath parameter is used exactly as it is typed.
#No characters are interpreted as wildcard characters.
2020-03-23 14:50:22 +01:00
{
Write-Host " CONVERSION DONE! FILE: `" $outputFile `" FOUND " -ForegroundColor Yellow
Start-Sleep -Seconds 4
2020-03-23 16:45:28 +01:00
# Check if File is valid; the duration of video must be the same! (['media']['track'][0]['Duration']):
2020-03-23 14:50:22 +01:00
$fileDetails_new = cmd / c $fileTester $outputFile - -Output = JSON | ConvertFrom-Json
2020-03-23 16:45:28 +01:00
# This code gets the duration and splits it in two parts, only the part before the "." is needed:
$StreamSize_old , $notused = $ ( $fileDetails . media . track [ 0 ] . Duration ) . split ( '.' )
$StreamSize_new , $notused = $ ( $fileDetails_new . media . track [ 0 ] . Duration ) . split ( '.' )
2020-03-23 14:50:22 +01:00
Write-Host
Write-Host " Old Duration was: $StreamSize_old `n new Duration is: $StreamSize_new " -ForegroundColor White
Write-Host
if ( $StreamSize_new -eq $StreamSize_old )
{
# Delete old video File!
2020-03-23 16:45:28 +01:00
Write-Host " Conversion Successful! - Deleting old file.. "
2020-03-25 12:06:48 +01:00
Write-Host
Write-Host " ------------------------------------------------------------------------------------------------ "
2020-03-25 23:27:09 +01:00
Remove-Item -LiteralPath $inputFile
2020-03-25 12:06:48 +01:00
$convertedVideos = $convertedVideos + 1
2020-03-25 18:30:50 +01:00
$Logfile = " $PSScriptRoot \logs\ $logFolderName \successfully_converted.log "
LogWrite " $outputFile "
2020-03-23 14:50:22 +01:00
} else
{
# Delete corrupt File!
2020-03-25 12:06:48 +01:00
Write-Host " Conversion Failded! - Deleting new converted file.. " -ForegroundColor Red
Write-Host
Write-Host " ------------------------------------------------------------------------------------------------ "
2020-03-25 23:27:09 +01:00
Remove-Item -LiteralPath $outputFile
2020-03-23 14:50:22 +01:00
$failedVideos = $failedVideos + 1
$notConvertedVideos = $notConvertedVideos + 1
2020-03-25 18:30:50 +01:00
$Logfile = " $PSScriptRoot \logs\ $logFolderName \error_during_conversion.log "
LogWrite " $inputFile "
2020-03-23 14:50:22 +01:00
}
}
2020-04-09 15:39:52 +02:00
#Start-Sleep -Seconds 4
2020-03-23 14:50:22 +01:00
} else
2020-03-06 00:43:30 +01:00
{
2020-03-25 12:06:48 +01:00
# Alert if video doesn't need conversion:
Write-Host " Analyzing video ( $videoID of $count ) - skip " -ForegroundColor Magenta
Write-Host " $video is already in correct format! " -ForegroundColor Green
Write-Host
Write-Host " ------------------------------------------------------------------------------------------------ "
2020-03-23 14:50:22 +01:00
# Increments not converted video counter:
$notConvertedVideos = $notConvertedVideos + 1
2020-03-25 18:30:50 +01:00
$Logfile = " $PSScriptRoot \logs\ $logFolderName \skipped_files.log "
LogWrite " $inputFile "
2020-03-06 00:43:30 +01:00
}
2020-03-25 12:06:48 +01:00
# Increments video counter:
$videoID = $videoID + 1
2020-03-25 18:30:50 +01:00
$outputFile = " " # Resets the variable to nothing, that in case of an error the old file is not affected.
2020-03-05 20:48:33 +01:00
}
2020-03-25 18:30:50 +01:00
#############################################################################################################
# END - Main conversion process
2020-03-05 20:48:33 +01:00
2020-03-25 18:30:50 +01:00
#############################################################################################################
# Evaluation part of the conversion script #
#############################################################################################################
2020-03-25 23:27:09 +01:00
Write-Host " Final calculations are in progress.. "
$folderSizeInGB2 = " {0:N2} GB " -f ( ( Get-ChildItem $videoPath -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop ) . Sum / 1 GB )
2020-03-23 14:50:22 +01:00
if ( $notConvertedVideos -eq 0 )
{
2020-03-25 12:06:48 +01:00
Write-Host " Finished converted $convertedVideos out of $count videos. " -ForegroundColor Green
2020-03-23 14:50:22 +01:00
} else
{
if ( $failedVideos -eq 0 )
{
2020-03-25 12:06:48 +01:00
Write-Host " Finished converted $convertedVideos out of $count videos. - $notConvertedVideos where not converted because of already correct codec! " -ForegroundColor Green
2020-03-23 14:50:22 +01:00
} else
{
2020-03-25 12:06:48 +01:00
Write-Host " Finished converted $convertedVideos out of $count videos. - $notConvertedVideos where not converted because error or already correct codec!! " -ForegroundColor Green
2020-03-23 14:50:22 +01:00
Write-Host " $failedVideos have failed! " -ForegroundColor Red
}
}
2020-03-25 23:27:09 +01:00
Write-Host " Directory size before conversion: $folderSizeInGB "
Write-Host " Directory size after conversion: $folderSizeInGB2 "
2020-03-05 20:48:33 +01:00
2020-03-25 18:30:50 +01:00
Read-Host -Prompt " Press Enter to exit "
#############################################################################################################
# END - conversion script