-
Notifications
You must be signed in to change notification settings - Fork 0
/
Process-HandbrakeBatcher.ps1
181 lines (160 loc) · 7.77 KB
/
Process-HandbrakeBatcher.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
.\Update-HandbrakeCLI.ps1 # Update HandbrakeCLI before processing.
Import-Module -Name Get-MediaInfo
$QueuePath = "C:\Scripts\HandBrakeBatcher\Queue"
$QueueRejectsPath = "C:\Scripts\HandBrakeBatcher\QueueRejects"
$LogPath = "C:\Scripts\HandBrakeBatcher\Logs"
$DestinationPath = "D:\Conversions"
$HandBrakeExe = "C:\Scripts\HandBrakeBatcher\HandBrakeCLI\HandBrakeCLI.exe"
# Original H264 profile
#$PresetFile = "C:\Scripts\HandBrakeBatcher\HandbrakePreset.json"
#$PresetName = "CustomFast1080p30"
# Newer H265/HEVC profile
$PresetFile = "C:\Scripts\HandBrakeBatcher\H265HandbrakePreset.json"
$PresetFileNight = "C:\Scripts\HandBrakeBatcher\H265HandbrakePresetNight.json"
$PresetName = "Apple1080pHEVCq38"
function Start-SleepUntil($waketime) {
$currentTime = Get-Date
$snoozeTime = New-TimeSpan -Start $currentTime -End $waketime
Write-Host "$($currentTime.TolongTimeString()) - Sleeping until" $processingStartTime
while ($waketime -gt $currentTime) {
$timeRemaing = New-TimeSpan -Start $currentTime -End $waketime
$percent = ($snoozeTime.TotalSeconds - $timeRemaing.TotalSeconds) / $snoozeTime.TotalSeconds * 100
$ProgressArguments = @{
Activity = "Sleeping"
Status = "Sleeping for $($timeRemaing.Hours) Hours $($timeRemaing.Minutes) Minutes $($timeRemaing.Seconds) Seconds "
SecondsRemaining = $timeRemaing.TotalSeconds
PercentComplete = $percent
}
Write-Progress @ProgressArguments
[System.Threading.Thread]::Sleep(500)
$currentTime = Get-Date
}
Write-Progress -Activity "Sleeping" -Status "Waking up... " -SecondsRemaining 0 -Completed
}
Function Get-HandbrakeProgress {
param(
[Parameter(Mandatory, ValueFromPipeline)]
$InputObject
)
begin {
[string]$status = ''
[double]$percent = 0
[double]$fps = 0
[string]$ETA = ''
}
process {
$Host.PrivateData.ProgressBackgroundColor = 'Cyan'
$Host.PrivateData.ProgressForegroundColor = 'Black'
$data = $InputObject -split ' '
If (![String]::IsNullOrEmpty("$($data[0])")) {
$status = ($data[0]).Split(':')[0]
}
If (![String]::IsNullOrEmpty("$($data[5])")) {
$percent = $data[5]
}
If (![String]::IsNullOrEmpty("$($data[10])")) {
$fps = $data[10]
}
If (![String]::IsNullOrEmpty("$($data[13])")) {
$ETA = ($data[13]).Split(')')[0]
}
Write-Progress -Id 1 -Activity " Currently $status $SourceFile"`
-Status "ETA: $ETA Complete: $percent% $fps fps"`
-PercentComplete $percent
}
}
$filecount = 0
$FileList = Get-ChildItem -Path $QueuePath | Sort-Object LastWriteTime
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host "*******************************************************************************"
Write-Host " Starting Handbrake Batch Encoding"
Write-Host " Process-HandbrakeBatcher by Paul Turpie" $PSCommandPath
Write-Host " $($FileList.Count) files to be processed"
Write-Host "*******************************************************************************"
while ($null -ne $FileList) {
$FileToProcess = Get-ChildItem -LiteralPath (Get-Content $FileList[0].FullName)
$totalFiles = $filecount + $FileList.Count
$filecount++
$SourceFile = $FileToProcess.FullName
$DestinationFile = (Join-Path -Path $DestinationPath -ChildPath ($FileToProcess.BaseName)) + ".mp4"
# Check current time and run the Night profile for higher performance
$currentTime = Get-Date
if (($currentTime.Hour -lt 23) -and ($currentTime.Hour -ge 6)) {
$processingStartTime = Get-Date -Hour 23 -Minute 59 -Second 0
Start-SleepUntil ($processingStartTime)
}
Write-Progress -Id 0 -Activity "Handbrake Batch Video Conversion in Progress" -Status "Processed $filecount of $totalFiles" -PercentComplete ($filecount / $totalFiles * 100)
Write-Host "-------------------------------------------------------------------------------"
Write-Host " Queue file: " $FileList[0]
Write-Host " Processing - $SourceFile"
Write-Host " to - $DestinationFile"
Write-Host "Processed $filecount files"
Write-Host " $($FileList.Count - 1) files remaining"
Write-Host "-------------------------------------------------------------------------------"
#TODO Validate the destination file before removing queuefile
# maybe move queuefile to a new directory
if (Test-Path -Path $DestinationFile) {
$MediaInfo = Get-MediaInfo -Path $DestinationFile
if ( $MediaInfo.Duration -gt 0) {
# Destination File exists and seems to be a valid video file, so delete the queue file.
Add-Content -Path (Join-Path -Path $LogPath -ChildPath "Completed.log") -Value (Get-Content -Path $FileList[0].FullName)
}
else {
# Destination file seems invalid so delete it and keep queue file to try again later.
Remove-Item $DestinationFile
}
}
&$HandBrakeExe --preset-import-file $PresetFileNight -Z $PresetName -i "$SourceFile" -o "$DestinationFile" 2>(Join-Path -Path $LogPath -ChildPath "Error.log") | Get-HandbrakeProgress
#TODO Validate the destination file before removing queuefile
# maybe move queuefile to a new directory
if (Test-Path -Path $DestinationFile) {
$MediaInfo = Get-MediaInfo -Path $DestinationFile
if ( $MediaInfo.Duration -gt 0) {
# Destination File exists and seems to be a valid video file, so delete the queue file.
Add-Content -Path (Join-Path -Path $LogPath -ChildPath "Completed.log") -Value (Get-Content -Path $FileList[0].FullName)
Write-Host "Processing completed, removing queue file."
Remove-Item $FileList[0].FullName
}
else {
# Destination file seems invalid so delete it and keep queue file to try again later.
Write-Host "Destination file seems invalid so delete it and keep queue file to try again later."
Write-Host " DestinationFile name=" $DestinationFile
Write-Host " Queue filename: " $FileList[0].FullName
Write-Host " Original Lastwritetime:" (Get-Item -Path $FileList[0].FullName).LastWriteTime
Remove-Item $DestinationFile
(Get-Item -Path $FileList[0].FullName).LastWriteTime = Get-Date
Write-Host " New Lastwritetime:" (Get-Item -Path $FileList[0].FullName).LastWriteTime
Move-Item -Path $FileList[0].FullName -Destination $QueueRejectsPath
}
}
else {
Write-Host "************* where did the destination file go!!!!!!!!!!!!!!!!!!!! *************"
Write-Host " DestinationFile name=" $DestinationFile
Write-Host " Queue filename: " $FileList[0].FullName
Write-Host " Original Lastwritetime:" (Get-Item -Path $FileList[0].FullName).LastWriteTime
(Get-Item -Path $FileList[0].FullName).LastWriteTime = Get-Date
Write-Host " New Lastwritetime:" (Get-Item -Path $FileList[0].FullName).LastWriteTime
Move-Item -Path $FileList[0].FullName -Destination $QueueRejectsPath
}
# Refresh the FileList incase more files have been queued.
$FileList = Get-ChildItem -Path $QueuePath | Sort-Object LastWriteTime
}
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host "*******************************************************************************"
Write-Host " Completed Handbrake Batch Encoding"
Write-Host " Process-HandbrakeBatcher by Paul Turpie"
Write-Host " $($totalFiles) files were processed"
Write-Host "*******************************************************************************"
Pause