-
Notifications
You must be signed in to change notification settings - Fork 23
/
conv2mp4.ps1
234 lines (194 loc) · 9.25 KB
/
conv2mp4.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
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
<#======================================================================================================================
conv2mp4-docker v5.0.0 - https://gitlab.com/BrianDMG/conv2mp4-docker / https://hub.docker.com/r/bridmg/conv2mp4
This Powershell script will recursively search through a user-defined file path and convert all videos of user-specified
include_file_types to MP4 with H264 video and AAC audio using ffmpeg. If a conversion failure is detected, the script re-encodes
the file with HandbrakeCLI. Upon successful encoding, Plex libraries are (optionally) refreshed and source file is deleted.
The purpose of this script is to reduce the amount of transcoding CPU load on a Plex server.
========================================================================================================================#>
Set-Location -Path $PSScriptRoot
#Load properties file
$propFile = Convert-Path "files\prop\properties.yaml"
$prop = Get-Content "$propFile" | ConvertFrom-Yaml
Remove-Variable -Name propFile
#Load configuration
$cfgFile = Convert-Path "$($prop.paths.files.cfg)"
$cfg = Get-Content "$cfgFile" | ConvertFrom-Yaml
Remove-Variable -Name cfgFile
# Time and format used for timestamps in the log
$time = {Get-Date -format "MM/dd/yy HH:mm:ss"}
# Get current time to store as start time for script
$startScriptTime = (Get-Date)
# Initialize 'video length converted' to 0
$cumulativeVideoDuration = [timespan]::fromseconds(0)
#Execute preflight checks
$preflightPath = Convert-Path "$($prop.paths.init.preflight)"
. $preflightPath
#Build processing queue and list its contents
$buildQueuePath = Convert-Path "$($prop.paths.init.buildqueue)"
. $buildQueuePath
# Begin performing operations of files
ForEach ($file in $fileList) {
$sourceFile = Join-Path "$($file.DirectoryName)" "$($file.BaseName)$($file.Extension)"
$sourceFile = Convert-Path "$($sourceFile)"
If ($cfg.paths.use_out_path) {
$fileSubDirs = $file.DirectoryName.Substring( $cfg.paths.media.Length, ($file.DirectoryName.Length - $cfg.paths.media.Length) )
$targetPath = "$($cfg.paths.out_path)$($fileSubDirs)"
If (-Not (Test-Path $targetPath)) {
New-Item -Path $targetPath -ItemType 'directory' -Force
}
$targetPath = Convert-Path "$($cfg.paths.out_path)$($fileSubDirs)"
$targetFile = Convert-Path "$($targetPath)"
$targetFile = Join-Path "$($targetFile)" "$($file.BaseName).mp4.conv2mp4"
}
Else {
$targetFile = Convert-Path "$($file.DirectoryName)\"
$targetFile = Join-Path "$($targetFile)" "$($file.BaseName).mp4.conv2mp4"
}
$progress = ($(@($fileList).indexOf($file)+1) / $fileList.Count) * 100
$progress = [Math]::Round($progress,2)
Write-Progress -Activity "$sourceFile" -PercentComplete $progress -CurrentOperation "$($progress)% Complete"
Add-Log "$($prop.formatting.standard_divider)"
Add-Log "$($time.Invoke()) Processing - $($sourceFile)"
Add-Log "$($time.Invoke()) File $(@($fileList).indexOf($file)+1) of $($fileList.Count) - Total queue $($progress)%"
#Set targetFile final name
If ($cfg.paths.use_out_path) {
$targetFileRenamed = Convert-Path "$($targetPath)\"
$targetFileRenamed = Join-Path "$($targetFileRenamed)" "$($file.BaseName).mp4"
}
Else {
$targetFileRenamed = Convert-Path "$($file.DirectoryName)"
$targetFileRenamed = Join-Path "$($targetFileRenamed)" "$($file.BaseName).mp4"
}
<#Test if $targetFile (.mp4) already exists, if yes then delete $sourceFile (.mkv)
This outputs a more specific log message acknowleding the file already existed.#>
If ((Test-Path "$($targetFileRenamed)") -And $file.Extension -ne ".mp4") {
Remove-Item "$($sourceFile)" -Force
Add-Log "$($time.Invoke()) Already exists: $($targetFileRenamed)"
Add-Log "$($time.Invoke()) Deleted: $($sourceFile)."
$duplicatesDeleted += @($sourceFile)
}
Else {
#Codec discovery to determine whether video, audio, or both needs to be encoded
$getAudioCodec = Get-Codec -DiscoverType Audio
$getVideoCodec = Get-Codec -DiscoverType Video
$getVideoDuration = Get-Codec -DiscoverType Duration
# Video is already H264, Audio is already AAC
If (!$getAudioCodec -OR !$getVideoCodec) {
$failureCause = 'corruptCodec'
Write-EncodeFailure
Continue
}
Elseif ($getVideoCodec -eq 'h264' -AND $getAudioCodec -eq 'aac') {
If ($file.Extension -ne ".mp4") {
Add-Log "$($time.Invoke()) Video: $($getVideoCodec.ToUpper()), Audio: $($getAudioCodec.ToUpper()). Performing simple container conversion to MP4."
Convert-File -ConvertType Simple -KeepSubs:$cfg.subtitles.keep
$simpleConversion += @($sourceFile)
$skipFile = $False
}
Else {
$getVideoDuration = "00:00:00"
$fileCompliant += @($sourceFile)
$skipFile = $True
}
}
# Video is already H264, Audio is not AAC
ElseIf ($getVideoCodec -eq 'h264' -AND $getAudioCodec -ne 'aac') {
Add-Log "$($time.Invoke()) Video: $($getVideoCodec.ToUpper()), Audio: $($getAudioCodec.ToUpper()). Encoding audio to AAC"
Convert-File -ConvertType Audio -KeepSubs:$cfg.subtitles.keep
$audioConversion += @($sourceFile)
$skipFile = $False
}
# Video is not H264, Audio is already AAC
ElseIf ($getVideoCodec -ne 'h264' -AND $getAudioCodec -eq 'aac') {
Add-Log "$($time.Invoke()) Video: $($getVideoCodec.ToUpper()), Audio: $($getAudioCodec.ToUpper()). Encoding video to H264."
Convert-File -ConvertType Video -KeepSubs:$cfg.subtitles.keep
$videoConversion += @($sourceFile)
$skipFile = $False
}
# Video is not H264, Audio is not AAC
ElseIf ($getVideoCodec -ne 'h264' -AND $getAudioCodec -ne 'aac') {
Add-Log "$($time.Invoke()) Video: $($getVideoCodec.ToUpper()), Audio: $($getAudioCodec.ToUpper()). Encoding video to H264 and audio to AAC."
Convert-File -ConvertType Both -KeepSubs:$cfg.subtitles.keep
$bothConversion += @($sourceFile)
$skipFile = $False
}
If ($cfg.audio.force_stereo_clone) {
Copy-StereoStream
}
# Refresh Plex libraries
If ($cfg.plex.enable -AND (-Not($skipFile))) {
Update-Plex
}
#Begin file comparison between old file and new file to determine conversion success
If (-Not ($skipFile)) {
$sourceFileCompare = Get-Item "$($sourceFile)"
$targetFileCompare = Get-Item "$($targetFile)"
# If new file is the same size as old file, log status and delete old file
If ($targetFileCompare.length -eq $sourceFileCompare.length) {
Compare-IfSame
}
# If new file is larger than old file, log status and delete old file
Elseif ($targetFileCompare.length -gt $sourceFileCompare.length) {
Compare-IfLarger
}
# If new file is much smaller than old file (indicating a failed conversion), log status, delete new file, and re-encode with HandbrakeCLI
Elseif ($targetFileCompare.length -lt ($sourceFileCompare.length * $cfg.conversion.failover_threshold)) {
Write-EncodeError
#Begin Handbrake encode (lossy)
Convert-File -ConvertType Handbrake -KeepSubs:$cfg.subtitles.keep
# Load files for comparison
$sourceFileCompare = Get-Item "$($sourceFile)"
$targetFileCompare = Get-Item "$($targetFile)"
# If new file still exceeds failover threshold, leave original file in place and log failure
If ($targetFileCompare.length -lt ($sourceFileCompare.length * $cfg.conversion.failover_threshold)) {
$failureCause = 'encodeFailure'
Write-EncodeFailure
}
# If new file is the same size as old file, log status and delete old file
Elseif ($targetFileCompare.length -eq $sourceFileCompare.length) {
Compare-IfSame
}
# If new file is larger than old file, log status and delete old file
Elseif ($targetFileCompare.length -gt $sourceFileCompare.length) {
Compare-IfLarger
}
# If new file is smaller than old file, log status and delete old file
Elseif ($targetFileCompare.length -lt $sourceFileCompare.length) {
Compare-IfSmaller
}
}
# If new file is smaller than old file, log status and delete old file
Elseif ($targetFileCompare.length -lt $sourceFileCompare.length) {
Compare-IfSmaller
}
#If $sourceFile was an mp4, rename $targetFile to remove "-NEW"
$targetFileRenamed = "$($targetFile)" -replace ".conv2mp4",""
Move-Item "$($targetFile)" "$($targetFileRenamed)"
#If using out_path, delete empty source directories
If ($cfg.paths.use_out_path) {
If ($Null -eq (Get-ChildItem -Force $file.DirectoryName) -AND $file.DirectoryName -ne $cfg.paths.media) {
Remove-Item $file.DirectoryName
}
}
}
Else {
Add-Log "$($time.Invoke()) MP4 already compliant."
If ($cfg.logging.use_ignore_list) {
Add-Log "$($time.Invoke()) Added file to ignore list."
$fileToIgnore = $file.BaseName + $file.Extension
Add-IgnoreList "$($fileToIgnore)"
}
}
#Running tally of session container duration (cumulative length of video processed)
$script:cumulativeVideoDuration = $cumulativeVideoDuration + $getVideoDuration
}
} # End foreach loop
#Wrap-up
Write-Statistics
Write-Failures
If ($cfg.cleanup.enable) {
Remove-Garbage
}
Write-Output "`nFinished"
Remove-LockFile
Exit