Skip to content

Instantly share code, notes, and snippets.

@sterling
Last active February 8, 2017 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sterling/9dacbac2f99a030f232a41a884cdc5de to your computer and use it in GitHub Desktop.
Save sterling/9dacbac2f99a030f232a41a884cdc5de to your computer and use it in GitHub Desktop.
Encode video files recursively while maintaining directory structure and file names on output. Uses Windows PowerShell and HandBrakeCLI.
gci . *.mp4 -R |
foreach-object {
$saveBase = "D:\handbrake"
$curDir = (Get-Item -Path ".\" -Verbose).FullName
$inName = $_.BaseName + $_.Extension
$inFile = $_.DirectoryName + "\" + $inName
$outFile = $saveBase + $inFile.replace($curDir, "")
$outDir = $outFile.replace($inName, "")
New-Item -path "$outDir" -type directory -force
if (Test-Path -LiteralPath $outFile) {
echo "Skipping $inFile"
} else {
echo "Working on $inFile"
echo "Saving to $outFile"
$exe = "C:\Program Files\HandBrake\HandBrakeCLI.exe"
$exeArgs = "-i `"$inFile`" -o `"$outFile`" --preset `"Very Fast 1080p30`""
$proc = Start-Process $exe $exeArgs -NoNewWindow -PassThru
if (!$proc.WaitForExit(600000)) { #10 min
echo "Timed out $inFile"
$proc.kill()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment