-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranscodeVideo.ps1
More file actions
232 lines (202 loc) · 9 KB
/
TranscodeVideo.ps1
File metadata and controls
232 lines (202 loc) · 9 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
[CmdletBinding()]
param(
[Parameter(Position = 0, Mandatory)][String]$SourcePath,
[String]$HandbrakeCliPath = "C:\Program Files\Handbrake\Handbrakecli.exe",
[String]$TranscodedFileDir = "",
[String]$MoveSourceFileToDir = "",
[Switch]$DontMoveSourceFile,
[Switch]$Recurse
)
Set-StrictMode -Version Latest
$Error.Clear()
$InformationPreference = "Continue"
Import-Module ".\Modules\MHP-ErrorChecking" -Force
Import-Module ".\Modules\MHP-FileOperations" -Force
Import-Module ".\Modules\MHP-ProcessOperations" -Force
function IsFileTranscodable(
[CmdletBinding()]
[Parameter(Mandatory=$true)][String]$Path
) {
Write-Host "Checking transcodability for $Path"
[String]$fileName = Split-Path -Path $Path -Leaf
if ($fileName -ilike "*- bluray.mkv") {
return $true
}
<# elseif ($fileName -ilike "*- uhd.mkv") {
return $true
}
#>
elseif ($fileName -ilike "*- dvd.mkv") {
return $true
}
return $false
}
function GetDestinationFileName(
[CmdletBinding()]
[Parameter(Mandatory=$true)] [String]$SourceFullPath
) {
[String]$fileName = (Split-Path -Path $SourceFullPath -Leaf).Trim()
if ($fileName -ilike "*- bluray.mkv") {
return $fileName.Substring(0, $fileName.Length - 10) + "H264CQ22.mkv"
}
<# elseif ($fileName -ilike "*- uhd.mkv") {
return $fileName.Substring(0, $fileName.Length - 7) + "AV1CQ23.mkv"
}
#>
elseif ($fileName -ilike "*- dvd.mkv") {
return $fileName.SubString(0, $fileName.Length - 7) + "H264CQ18.mkv"
}
else {
throw "Unknown extension"
}
}
function GetHandbrakePreset(
[CmdletBinding()]
[Parameter(Mandatory=$true)] [String]$SourceFullPath
) {
[String]$fileName = (Split-Path -Path $SourceFullPath -Leaf).Trim()
if ($fileName -ilike "*- bluray.mkv") {
return "H.264 - Source res and frame rate - CQ22"
}
<# elseif ($fileName -ilike "*- uhd.mkv") {
return "AV1 - Source res and frame rate - CQ23"
}
#>
elseif ($fileName -ilike "*- dvd.mkv") {
return "H.264 - Source res and frame rate - CQ18"
}
else {
throw "Unknown extension"
}
}
function GetDestinationDirectory(
[CmdletBinding()]
[Parameter(Mandatory=$true)] [String]$SourceFullPath
) {
if (Test-Path -Path $SourceFullPath -PathType Leaf) {
return Split-Path -Path $SourceFullPath -Parent
} elseif (Test-Path -Path $SourceFullPath -PathType Container) {
return $SourceFullPath
} else {
throw "Cannot calculate destination directory for nonexistent source file $SourceFullPath"
}
}
function GetOriginalFileDestinationDirectory(
[CmdletBinding()]
[Parameter(Mandatory=$true)][String]$SourceFullPath
) {
[String]$sourceDir = Split-Path -Path $SourceFullPath -Parent
[String]$drivePrefix = Split-Path $SourceFullPath -Qualifier
[String]$sourcePathWithoutDrive = $sourceDir.Substring($drivePrefix.Length, $sourceDir.Length - $drivePrefix.Length)
[String]$finalDir = Join-Path -Path $drivePrefix -ChildPath "\Originals\"
$finalDir = Join-Path -Path $finalDir -ChildPath $sourcePathWithoutDrive
return $finalDir
}
function GetDestinationTempFileFullPath(
[CmdletBinding()]
[Parameter(Mandatory=$true)] [String]$DestinationFullPath
) {
[String]$drivePrefix = Split-Path $DestinationFullPath -Qualifier
[String]$tempDir = Join-Path -Path $drivePrefix -ChildPath "\HandbrakeTemp\"
CreateDirectory -Path $tempDir
[String]$filename = Split-Path $DestinationFullPath -Leaf
[String]$tempFullPath = Join-Path -Path $tempDir -ChildPath $filename
return $tempFullPath
}
function TranscodeFile(
[CmdletBinding()]
[Parameter(Mandatory=$true)] [String]$SourceFullPath,
[Parameter(Mandatory=$true)] [String]$TranscodedDestinationDirPath
) {
[String]$transcodedDestinationFilename = GetDestinationFileName($SourceFullPath)
[String]$transcodedDestinationFullPath = Join-Path -Path $TranscodedDestinationDirPath -ChildPath $transcodedDestinationFilename
if (Test-Path -Path $transcodedDestinationFullPath) {
throw "Transcode destination file $transcodedDestinationFullPath already exists"
}
Write-Host "File being transcoded: $SourceFullPath"
Write-Host "Transcode destination file: $transcodedDestinationFullPath"
CreateDirectory -Path $transcodedDestinationDirPath -CreateParentDirectoriesIfNeeded
[String]$transcodedDestinationTempFullPath = GetDestinationTempFileFullPath($transcodedDestinationFullPath)
Write-Host "Will transcode into temporary location: $transcodedDestinationTempFullPath"
[String]$originalDestinationDirPath = ""
if ($DontMoveSourceFile) {
Write-Host "Source file will not be moved."
}
else {
$originalDestinationDirPath = $MoveSourceFileToDir
if ([String]::IsNullOrEmpty($originalDestinationDirPath)) {
$originalDestinationDirPath = GetOriginalFileDestinationDirectory($SourceFullPath)
}
Write-Host "File being transcoded will be moved to: $originalDestinationDirPath"
CreateDirectory -Path $originalDestinationDirPath -CreateParentDirectoriesIfNeeded
}
[String]$handbrakePreset = GetHandbrakePreset($SourceFullPath)
Write-Host "Handbrake preset: $handbrakePreset"
[String[]]$handbrakeArgumentList = "--preset-import-gui", "--preset", "`"$handbrakePreset`"", "-i", "`"$SourceFullPath`"", "-o", "`"$transcodedDestinationTempFullPath`""
Write-Host "Starting Handbrake..."
[String]$processDescription = "Transcoding $SourceFullPath to $transcodedDestinationTempFullPath"
[String]$handbrakeOutput = RunProcessAndWait -ProcessName $HandbrakeCliPath -ArgumentList $handbrakeArgumentList -CheckExitCode -DontFailOnErrorOutput -ReturnErrorOutput -ReduceProcessPriority -SecondsBetweenPolls 10 -WindowStyle Normal -MoveProcessWindowToEdge -OutputProgress -ProgressProcessDescription $processDescription
[Boolean]$handbrakeSucceeded = (0 -le $handbrakeOutput.IndexOf("libhb: work result = 0"))
if (-not $handbrakeSucceeded) {
throw "Handbrake failed"
}
if (-not (Test-Path -Path $transcodedDestinationTempFullPath)) {
throw "Handbrake did not create file as expected: $transcodedDestinationFullPath"
}
Write-Host "Success!"
Write-Host "Moving transcoded file to $transcodedDestinationFullPath"
MoveFile -Path $transcodedDestinationTempFullPath -DestinationFilePath $transcodedDestinationFullPath
if (-not $DontMoveSourceFile) {
Write-Host "Moving original file to $originalDestinationDirPath"
MoveFile -Path $SourceFullPath -DestinationDirectoryPath $originalDestinationDirPath
}
Write-Host "Done transcoding $SourceFullPath"
}
function TranscodeDirectory(
[CmdletBinding()]
[Parameter(Mandatory=$true)] [String]$SourceDirPath,
[Parameter(Mandatory=$true)] [String]$TranscodedDestinationDirPath,
[Parameter(Mandatory=$true)] [Boolean]$Recurse
) {
Write-Host "Transcoding directory $SourceDirPath"
Write-Host "Destination for transcoded files is $TranscodedDestinationDirPath"
$filesInDir = Get-ChildItem -Path $SourceDirPath | Where-Object { $_.PSIsContainer -eq $false }
foreach ($file in $filesInDir) {
[String]$filePath = $file.FullName
Write-Host "Found file $filePath"
if (IsFileTranscodable($filePath)) {
Write-Host "Found transcodable file $filePath"
TranscodeFile -SourceFullPath $filePath -TranscodedDestinationDirPath $TranscodedDestinationDirPath
}
else {
Write-Host "Found non-transcodable file $filePath"
}
}
if ($Recurse)
{
$dirsInDir = Get-ChildItem -Path $SourceDirPath | Where-Object {$_.PSIsContainer -eq $true}
foreach ($dir in $dirsInDir) {
[String]$dirPath = $dir.FullName
[String]$dirName = $dir.Name
[String]$newTranscodedFileDirPath = Join-Path -Path $TranscodedDestinationDirPath -ChildPath $dirName
Write-Host "Recursing into subdirectory $dirPath"
Write-Host "New destination for transcoded files is $newTranscodedFileDirPath"
TranscodeDirectory -SourceDirPath $dirPath -TranscodedDestinationDirPath $newTranscodedFileDirPath -Recurse $true
}
}
}
ValidateFullPath -Path $SourcePath -ThrowIfInvalid -ThrowDescription "SourcePath parameter"
ValidateFullPath -Path $HandbrakeCliPath -PathType Leaf -ThrowIfInvalid -ThrowDescription "HandbrakeCliPath parameter"
[String]$transcodedDestinationDirPath = $TranscodedFileDir
if ([String]::IsNullOrEmpty($transcodedDestinationDirPath)) {
$transcodedDestinationDirPath = GetDestinationDirectory($SourcePath)
}
if (Test-Path -Path $SourcePath -PathType Leaf) {
TranscodeFile -SourceFullPath $SourcePath -TranscodedDestinationDirPath $transcodedDestinationDirPath
}
elseif (Test-Path -Path $SourcePath -PathType Container) {
TranscodeDirectory -SourceDirPath $SourcePath -TranscodedDestinationDirPath $transcodedDestinationDirPath -Recurse $Recurse.ToBool()
}
else {
throw "Source file not found or unexpected type: $SourcePath"
}