-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompress-Videos.ps1
More file actions
239 lines (206 loc) · 7.53 KB
/
Copy pathCompress-Videos.ps1
File metadata and controls
239 lines (206 loc) · 7.53 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
233
234
235
236
237
238
239
######################################################################################################################
# Uses GPU accelerated HEVC/AV1 compression to reduce file size without visibly effecting quality.
# FFMPEG commands come from EposVox, who wrote a .bat version I based this script on.
# Only works on Nvidia GPUs with a hardware encoder (experimental support for Intel/AMD GPUs WiP).
# Written by RantingRaymond. My other scripts can be found at https://www.github.com/Theray070696/Powershell-Scripts
######################################################################################################################
[CmdletBinding()]
param
(
[string] $Path,
[switch] $Recurse,
[switch] $AutoDelete, # Note, I'd recommand leaving this off and manually checking the files so you're not left with a file that had a broken encode and no original. I haven't had this happen yet, but I'm not responsible for any loss caused by this switch.
[switch] $AV1,
[switch] $HEVC,
[switch] $AMD,
[switch] $Intel
)
$Version = 1.2.2
function Normalize-BaseName
{
param([string]$Base)
$n = $Base
do
{
$before = $n
# Strip encoding suffixes (_CRFxx, _crfxx, optional codec)
$n = $n -replace '(?i)_(CRF\d+|QP\d+)(_(AV1|HEVC))?$', ''
$n = $n -replace '(?i)_(AV1|HEVC)$', ''
} while($n -ne $before)
return $n
}
function CompleteNotification
{
[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
$objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon
$objNotifyIcon.Icon = [System.Drawing.SystemIcons]::Information
$objNotifyIcon.BalloonTipIcon = 'Info'
$objNotifyIcon.BalloonTipText = 'Compression Complete'
$objNotifyIcon.BalloonTipTitle = 'Video Compression Completed'
$objNotifyIcon.Visible = $True
$objNotifyIcon.ShowBalloonTip(10000)
}
function Compress-File($File)
{
if($HEVC)
{
if($File.Name.EndsWith('_HEVC.mp4') -or $File.Name.EndsWith('_AV1.mp4'))
{
Write-Host $File.Name is already compressed
return
}
$Encoder = "hevc_"
if($AMD)
{
$Encoder += "amf"
} elseif($Intel)
{
$Encoder += "qsv"
} else
{
$Encoder += "nvenc"
}
$EncoderLevel = 20
$OutPath = "$($File.Directory)\$(Normalize-BaseName $File.BaseName)_QP$($EncoderLevel)_HEVC.mp4"
if(-not $AMD)
{
if($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent)
{
ffmpeg -hwaccel auto -i $File.FullName -map 0:v -map 0:a? -c:v $Encoder -rc constqp -qp $EncoderLevel -b:v 0K -c:a aac -b:a 384k "$OutPath"
} else
{
ffmpeg -hwaccel auto -i $File.FullName -map 0:v -map 0:a? -c:v $Encoder -rc constqp -qp $EncoderLevel -b:v 0K -c:a aac -b:a 384k "$OutPath" -hide_banner -loglevel error -stats
}
} else # FUCK YOU BALTIMORE
{
if($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent)
{
ffmpeg -hwaccel auto -i $File.FullName -map 0:v -map 0:a? -c:v $Encoder -rc cqp -qp_i $EncoderLevel -qp_p $EncoderLevel -b:v 0K -c:a aac -b:a 384k "$OutPath"
} else
{
ffmpeg -hwaccel auto -i $File.FullName -map 0:v -map 0:a? -c:v $Encoder -rc cqp -qp_i $EncoderLevel -qp_p $EncoderLevel -b:v 0K -c:a aac -b:a 384k "$OutPath" -hide_banner -loglevel error -stats
}
}
if($AutoDelete -and $LastExitCode -eq 0)
{
$NewFile = Get-Item -Path "$OutPath"
if(Test-Path "$OutPath")
{
if($NewFile.Length -gt $File.Length)
{
Write-Host Original file is smaller
$NewFile | Remove-Item
Move-Item -Path $File.FullName -Destination "$OutPath"
} elseif($File.Length -gt $NewFile.Length)
{
Write-Host Compressed file is smaller
$File | Remove-Item
}
}
}
} elseif($AV1)
{
if($File.Name.EndsWith('_AV1.mp4'))
{
Write-Host $File.Name is already compressed
return
}
#Write-Host "AV1 Encoding is in very early testing. So early that it's not even implemented. Will update as I get GPUs."
#return
$Encoder = "av1_"
if($AMD)
{
$Encoder += "amf"
} elseif($Intel)
{
$Encoder += "qsv"
} else
{
$Encoder += "nvenc"
}
$EncoderLevel = 100
$OutPath = "$($File.Directory)\$(Normalize-BaseName $File.BaseName)_QP$($EncoderLevel)_AV1.mp4"
if($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent)
{
ffmpeg -hwaccel auto -i $File.FullName -map 0:v -map 0:a? -c:v $Encoder -rc constqp -qp $EncoderLevel -b:v 0K -c:a aac -b:a 384k "$OutPath"
} else
{
ffmpeg -hwaccel auto -i $File.FullName -map 0:v -map 0:a? -c:v $Encoder -rc constqp -qp $EncoderLevel -b:v 0K -c:a aac -b:a 384k "$OutPath" -hide_banner -loglevel error -stats
}
if($AutoDelete -and $LastExitCode -eq 0)
{
$NewFile = Get-Item -Path "$OutPath"
if(Test-Path "$OutPath")
{
if($NewFile.Length -gt $File.Length)
{
Write-Host Original file is smaller
$NewFile | Remove-Item
Move-Item -Path $File.FullName -Destination "$OutPath"
} elseif($File.Length -gt $NewFile.Length)
{
Write-Host Compressed file is smaller
$File | Remove-Item
}
}
}
}
}
function Compress-Folder([string]$Folder)
{
$ChildItems = Get-ChildItem -Path $Folder
ForEach($Item in $ChildItems)
{
if($Recurse -and $Item -is [System.IO.DirectoryInfo])
{
Write-Host Compressing directory $Item.FullName
Compress-Folder $Item.FullName
} elseif($Item.Extension -eq '.MP4' -or $Item.Extension -eq '.MKV' -or $Item.Extension -eq '.MOV' -or $Item.Extension -eq '.WMV')
{
Write-Host Compressing $Item.Name
Compress-File $Item
} elseif($Item.Name.EndsWith('_HEVC.mp4'))
{
Write-Host $Item.Name is already compressed
}
}
}
if($MyInvocation.InvocationName -ne '.' -and $MyInvocation.InvocationName -ne 'Import-Module')
{
if(-not (Get-Command ffmpeg))
{
# FFMPEG not found.
Write-Error FFMPEG not found, please install it and add it to your PATH.
return
}
if([string]::IsNullOrEmpty($Path))
{
$Path = Split-Path -Parent $MyInvocation.MyCommand.Definition
}
$Target = Get-Item $Path
if($Target -is [System.IO.DirectoryInfo])
{
# This is a directory
if($Recurse)
{
Write-Host Compressing directory $Target.FullName and sub-directories
} else
{
Write-Host Compressing directory $Target.FullName
}
Compress-Folder $Target.FullName
CompleteNotification
} elseif(($Target.Extension -eq '.MP4' -or $Target.Extension -eq '.MKV' -or $Target.Extension -eq '.MOV' -or $Target.Extension -eq '.WMV') -and -not $Target.Name.EndsWith('_HEVC.mp4'))
{
# This is a file
Write-Host Compressing $Target.Name
Compress-File $Target
CompleteNotification
} elseif($HEVC -and ($Target.Name.EndsWith('_HEVC.mp4') -or $Target.Name.EndsWith('_AV1.mp4')))
{
Write-Host $Target.Name is already compressed
} elseif($AV1 -and $Target.Name.EndsWith('_AV1.mp4'))
{
Write-Host $Target.Name is already compressed
}
}