Created
December 12, 2021 20:34
-
-
Save Szeraax/c0af2e9f3a76b728ac89e9e93f1e1bfc to your computer and use it in GitHub Desktop.
PowerShell function that lets you encode videos via handbrake and move completed files to destinations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Invoke-HandbrakeEncoding { | |
<# | |
.SYNOPSIS | |
PowerShell function that lets you encode videos via handbrake and move completed files to destinations | |
.EXAMPLE | |
dir | Invoke-HandbrakeEncoding -Dest . -WhatIf | |
# Shows the commands that _would_ run if this were ran without the -WhatIf switch. | |
.EXAMPLE | |
dir | Invoke-HandbrakeEncoding -Dest foo | |
# Encodes all files in current directory and places their output in the subdirectory called 'foo' | |
.EXAMPLE | |
dir | Invoke-HandbrakeEncoding -Dest . -VerBose | |
# Displays all handbrake encoding info for each file as it encodes | |
.EXAMPLE | |
ls \\nas\GameCaptures\ | Invoke-HandbrakeEncoding -Destination . -InputMoveTo \\nas\GameCaptures\Completed\ -DestinationMoveTo \\nas\Plex-GameCaptures\ | |
# Encode all files in the \\nas\GameCaptures folder to the current directory. | |
# After successful encode, move the output file to the new in the "Plex-GameCaptures" share. | |
# Then move the original file that was used to the "Completed" folder on the share | |
.EXAMPLE | |
Invoke-HandbrakeEncoding -InputObject file.mkv -Destination . | |
# Encode the file "file.mkv" and place the encoded version in the current directory | |
.NOTES | |
Some helpful commands to keep in mind for HandbrakeCLI: | |
handbrakecli --help | |
handbrakecli --preset-list | |
#> | |
[cmdletbinding(SupportsShouldProcess)] | |
param( | |
[parameter( | |
Mandatory, | |
ValueFromPipeline, | |
ValueFromPipelineByPropertyName | |
)] | |
[alias("Fullname")] | |
# Files that you want to send to handbrake for encoding | |
[System.IO.FileInfo[]]$InputObject, | |
# Folder path where the created files should be placed in | |
[string]$Destination = ".", | |
# Folder to move input files to after completing the encode | |
[string]$InputMoveTo, | |
# Folder to move output files to after completing the encode | |
[string]$DestinationMoveTo, | |
# The preset to use for encoding. It IS case sensitive! | |
[string]$Preset = "Discord Nitro Medium 5-10 Minutes 720p30" | |
) | |
begin { | |
$count = 0 | |
} | |
process { | |
foreach ($item in $InputObject | Where-Object { $_.PSIsContainer -eq $false }) { | |
try { | |
$outputPath = "$Destination\Encoded-$($item.Name).mp4" | |
$argumentList = @( | |
# Arguments with a space MUST be wrapped in extra quotes. | |
# '"{0}"' -f "Discord Nitro" gets turned into this when passed: | |
# "Discord Nitro" | |
"--preset" | |
'"{0}"' -f $Preset | |
# "--optimize" # Discord Nitro profile already has optimize enabled | |
"--input" | |
'"{0}"' -f $item.FullName | |
"--output" | |
'"{0}"' -f $outputPath | |
) | |
Write-Host "Encoding file: $($item.Name)" -ForegroundColor Green | |
if ($PSCmdlet.ShouldProcess($item.FullName, "Encode file and conditionally move output file")) { | |
if ($VerbosePreference -ne "SilentlyContinue") { | |
HandBrakeCLI.exe $argumentList | |
} | |
else { | |
HandBrakeCLI.exe $argumentList 2>$null | |
} | |
if (Test-Path $outputPath) { | |
if ((Get-Item $outputPath).length -eq 0) { | |
throw "Output file created at $outputPath was empty. Investigate why handbrake didn't run properly" | |
} | |
} | |
else { | |
throw "No output file was created at $outputPath. Investigate why handbrake didn't run" | |
} | |
if ($DestinationMoveTo) { Move-Item -Path $outputPath -Destination $DestinationMoveTo -ea stop } | |
$count++ | |
} | |
if ($InputMoveTo) { Move-Item -Path $item.FullName -Destination $InputMoveTo -ea stop } | |
} | |
catch { | |
"Unable to encode file: $($item.name)" | |
$_ | |
} | |
} | |
} | |
end { | |
"Successfully encoded $count file(s)" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment