Skip to content

Instantly share code, notes, and snippets.

@botsmack
Last active December 19, 2015 01:49
Show Gist options
  • Save botsmack/5878562 to your computer and use it in GitHub Desktop.
Save botsmack/5878562 to your computer and use it in GitHub Desktop.
PowerShell port of Don Melton's encode.sh script (https://gist.github.com/donmelton/5734177) to encode video files with HandbrakeCLI. Some things like subtitle support weren't migrated as I don't care for them to be burned in and the Apple TV doesn't support them otherwise. Don's settings are kept intact, with the exception of upping the bitrate…
<#
I added the optional decomb PowerShell parameter than activates "--decomb" in Handbrake.
I was running into interlace issues encoding an animated TV show - Sealab 2021. I don't
think you will need it unless you're enoding similar content. For everything else I
leave it off ($false) by default.
Improved the funcationality of the script to take either a single MKV file or a folder
of MKV files.
Usage:
encode.ps1 [source file]
encode.ps1 [source directory] -decomb:$true
#>
Param(
[Parameter(Mandatory = $true, HelpMessage="Please specify an MKV source file or directory.")]
[string]$source,
[switch]$decomb
)
[string]$handbrake = "C:\Program Files\HandBrake\HandBrakeCLI.exe"
[string]$mediainfo = "C:\Program Files\MediaInfo\MediaInfo.exe"
$script:handbrake_options = @("--markers", "--large-file", "--encoder", "x264", "--encopts", "vbv-maxrate=25000:vbv-bufsize=31250:ratetol=inf", "--crop", "0:0:0:0", "--strict-anamorphic")
if ($decomb) {
$script:handbrake_options += "--decomb"
}
function Encode-File {
Param(
[Parameter(Mandatory = $true)]
[string]$source_file
)
[string]$output_file = [io.path]::ChangeExtension($source_file, '.mp4')
[int]$width = & $mediainfo --Inform="Video;%Width%" $source_file
[int]$height = & $mediainfo --Inform="Video;%Height%" $source_file
if (($width -gt 1280) -or ($height -gt 720)) {
$max_bitrate = "6500"
} elseif (($width -gt 720) -or ($height -gt 576)) {
$max_bitrate = "4000"
} else {
$max_bitrate = "2200"
}
[int]$min_bitrate = $max_bitrate / 2
[decimal]$bitrate = & $mediainfo --Inform="Video;%BitRate%" $source_file
if (! $bitrate) {
$bitrate = & $mediainfo --Inform="General;%OverallBitRate%" $source_file
$bitrate = $bitrate / 10 * 9
}
if ($bitrate) {
$bitrate = [math]::floor($bitrate / 5 * 4 / 100000) * 100
if ($bitrate -gt $max_bitrate) {
$bitrate = $max_bitrate
} elseif ($bitrate -lt $min_bitrate) {
$bitrate = $min_bitrate
}
} else {
$bitrate = $min_bitrate
}
$script:handbrake_options += "--vb", $bitrate
[decimal]$frame_rate = & $mediainfo --Inform="Video;%FrameRate_Original%" $source_file
if (! $frame_rate) { $frame_rate = & $mediainfo --Inform="Video;%FrameRate%" $source_file }
if ($frame_rate -eq 29.970) {
$script:handbrake_options += "--rate", 23.976, "--detelecine"
} else {
$script:handbrake_options += "--rate", 30, "--pfr"
}
[int]$channels = (& $mediainfo --Inform="Audio;%Channels%" $source_file) -replace "[^0-9].*"
if ($channels -gt 2) {
$script:handbrake_options += "--aencoder", "faac,copy:ac3"
} elseif (((& $mediainfo --Inform="General;%Audio_Format_List%" $source_file) -replace " /.*") -eq "AAC") {
$script:handbrake_options += "--aencoder", "copy:aac"
}
& $handbrake $script:handbrake_options --input $source_file --output $output_file
}
if (Test-Path $source -PathType Container) {
$files = (Get-ChildItem "$source\*" -include *.mkv)
if ($files.length -ge 1) {
ForEach ($file in $files) {
[string]$mp4file = [io.path]::ChangeExtension($file, ".mp4")
Write-Host $mp4file
if (!(Test-Path $mp4file)) {
Write-Host "`n*******************************************************************************"
Write-Host "Encoding $file" -foreground green
Write-Host "*******************************************************************************`n"
Encode-File $file
} else {
Write-Host "$mp4file already exists"
}
}
} else {
Write-Host "`n*******************************************************************************"
Write-Host "No files are available to encode" -foreground yellow
Write-Host "*******************************************************************************`n"
}
} else {
Encode-File $source
}
@botsmack
Copy link
Author

botsmack commented Jul 1, 2013

I'm still learning PowerShell, so if you see an issue or a better method of doing something, just let me know. I have tried implementing file logging, but PowerShell seems to have an issue with capturing screen output and file output simultaneously. So I'll be looking at file logging in a more granular approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment