-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmpcopy.ps1
235 lines (208 loc) · 9.82 KB
/
xmpcopy.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
235
#requires -version 3
# Copy XMP info from e.g. Capture One's XMP-files to DigiKam-compliant XMP files.
# WARNING: QUICK AND DIRTY SOLUTION! USE AT YOUR OWN RISK!
# DEFINITION: Get all error-outputs in English:
[Threading.Thread]::CurrentThread.CurrentUICulture = 'en-US'
# DEFINITION: Hopefully avoiding errors by wrong encoding now:
$OutputEncoding = New-Object -TypeName System.Text.UTF8Encoding
[Console]::InputEncoding = New-Object -TypeName System.Text.UTF8Encoding
Write-Host "This tool copies XMP info from e.g. Capture One's XMP-files to e.g. DigiKam-compliant XMP files."
Write-Host "WARNING: QUICK AND DIRTY SOLUTION! USE AT YOUR OWN RISK!"
Start-Sleep -Seconds 2
$paths = @("D:\Bilder\_CANON\Privat","D:\Bilder\_CANON\Professionell")
$paths += "D:\Bilder\_CANON\Projekte"
$Script:InfoPreference = 0
$XMP = @()
foreach($i in $paths){
Push-Location $i
$XMP += @(Get-ChildItem .\ -Filter *.xmp -Recurse | Where-Object {$_.FullName -notmatch '^.*\.\w{3}\.xmp$' -and $_.Extension -notmatch '^\.xmp_original$'} | ForEach-Object {
[PSCustomObject]@{
XMP = $_.FullName
original = $null
}
})
Pop-Location
}
Write-Host "In: $($XMP.Length)"
$XMP | ForEach-Object {
[array]$original = @(Get-Item $($_.XMP.Replace(".xmp",".*")) | Where-Object {$_.Extension -notmatch '^\.xmp.*$'} | Select-Object -ExpandProperty FullName)
if($original.Count -gt 1){
$original = @($original | Where-Object {$_ -notmatch '^.*\.jpg$'})
$original | Out-Null
if($original.Count -gt 1){
$original = @($original | Where-Object {$_ -notmatch '^.*\.psd$'})
$original | Out-Null
if($original.Count -gt 1){
$original = @($original | Where-Object {$_ -notmatch '^.*\.tif$'})
$original | Out-Null
if($original.Count -gt 1){
$original = @($original | Where-Object {$_ -notmatch '^.*\.psb$'})
$original | Out-Null
if($original.Count -gt 1){
$original = @($original | Where-Object {$_ -notmatch '^.*\.png$'})
$original | Out-Null
}}}}
}
$_.original = $($original)
}
$XMP | Out-Null
# $XMP | Where-Object {$_.original -eq $null} | Format-List; exit
$XMP = @($XMP | Where-Object {$_.original -ne $null})
$XMP | Out-Null
$XMP = @($XMP | Sort-Object -Property XMP,original)
$XMP | Out-Null
Write-Host "Out: $($XMP.Length)"
Pause
# $XMP | Format-List; $XMP.Length; exit
# $XMP | Format-List | Out-File D:\xmp.txt; exit
Function Start-EXIFManipulation(){
param(
[ValidateNotNullOrEmpty()]
[array]$WorkingFiles = $(throw 'WorkingFiles is required by Start-EXIFManipulation')
)
[int]$errorcounter = 0
[int]$successcounter = 0
# DEFINITION: Create Exiftool process:
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = "C:\FFMPEG\binaries\exiftool.exe"
$psi.Arguments = "-stay_open True -charset utf8 -@ -"
$psi.UseShellExecute = $false
$psi.RedirectStandardInput = $true
$psi.RedirectStandardOutput = $true
$psi.RedirectStandardError = $true
# CREDIT: To get asymmetric buffer readout running (ak.a. unlimited processing) (1/2): https://stackoverflow.com/a/24371479/8013879
[int]$exiftoolInstanceCount = [Math]::Ceiling($WorkingFiles.Length/100)
$exiftoolproc = @{}
$exiftoolStdOutBuilder = @{}
$exiftoolStdErrBuilder = @{}
$exiftoolScripBlock = @{}
$exiftoolStdOutEvent = @{}
$exiftoolStdErrEvent = @{}
for($i=0; $i -lt $exiftoolInstanceCount; $i++){
try{
$exiftoolproc[$i] = New-Object -TypeName System.Diagnostics.Process -Verbose
$exiftoolproc[$i].StartInfo = $psi
}catch{
write-host "Failed to create System.Diagnostics.Process #$($i.ToString())!" -ForegroundColor Red
return 1
}
try{
# Creating string builders to store StdOut and StdErr:
$exiftoolStdOutBuilder[$i] = New-Object -TypeName System.Text.StringBuilder -Verbose
$exiftoolStdErrBuilder[$i] = New-Object -TypeName System.Text.StringBuilder -Verbose
}catch{
write-host "Failed to create System.Text.StringBuilder #$($i.ToString())!" -ForegroundColor Red
return 1
}
try{
# Adding event handers for StdOut and StdErr:
$exiftoolScripBlock[$i] = {
if(-not [String]::IsNullOrEmpty($EventArgs.Data)){
$Event.MessageData.AppendLine($EventArgs.Data)
}
}
}catch{
write-host "Failed to create Event.MessageData.AppendLine #$($i.ToString())!" -ForegroundColor Red
return 1
}
try{
$exiftoolStdOutEvent[$i] = Register-ObjectEvent -InputObject $exiftoolproc[$i] -Action $exiftoolScripBlock[$i] -EventName 'OutputDataReceived' -MessageData $exiftoolStdOutBuilder[$i] -Verbose
$exiftoolStdErrEvent[$i] = Register-ObjectEvent -InputObject $exiftoolproc[$i] -Action $exiftoolScripBlock[$i] -EventName 'ErrorDataReceived' -MessageData $exiftoolStdErrBuilder[$i] -Verbose
}catch{
write-host "Failed to create Register-ObjectEvent #$($i.ToString())!" -ForegroundColor Red
return 1
}
try{
[Void]$exiftoolproc[$i].Start()
$exiftoolproc[$i].BeginOutputReadLine()
$exiftoolproc[$i].BeginErrorReadLine()
}catch{
write-host "Failed to create exiftool-instance #$($i.ToString())!" -ForegroundColor Red
return 1
}
if($script:InfoPreference -gt 0){
write-host "exiftool instance #$i created!" -ForegroundColor Gray
}
}
# DEFINITION: Set arguments for different purposes:
[array]$exiftoolArgList = @()
for($i=0; $i -lt $WorkingFiles.Length; $i++){
#$exiftoolArgList += "-charset`nfilename=utf8`n-tagsFromFile`n$($WorkingFiles[$i].original)`n-charset`nfilename=utf8`n-tagsfromfile`n$($WorkingFiles[$i].xmp)`n-charset`nfilename=utf8`n$($WorkingFiles[$i].original).xmp"
$exiftoolArgList += "-charset`nfilename=utf8`n-tagsFromFile`n$($WorkingFiles[$i].xmp)`n-overwrite_original`n-charset`nfilename=utf8`n$($WorkingFiles[$i].original).xmp"
}
$sw = [diagnostics.stopwatch]::StartNew()
$k = ($exiftoolInstanceCount - 1)
# DEFINITION: Pass arguments to Exiftool:
for($i=0; $i -lt $exiftoolArgList.Length; $i++){
if($sw.Elapsed.TotalMilliseconds -ge 750){
Write-Progress -Activity "$choiceString..." -Status "File # $i - $($WorkingFiles[$i].xmp)" -PercentComplete $($i * 100 / $WorkingFiles.Length)
$sw.Reset()
$sw.Start()
}
if($script:InfoPreference -gt 0){
[string]$inter = $exiftoolArgList[$i].ToString()
[string]$inter += "`n-verbose"
$exiftoolArgList[$i] = $inter
write-host "exiftool $($exiftoolArgList[$i].Replace("`n"," ").Replace("$((Get-Location).Path)","."))" -ForegroundColor DarkGray
}
# $inter = $exiftoolproc[$k]
try{
$exiftoolproc[$k].StandardInput.WriteLine("$($exiftoolArgList[$i])`n-execute`n")
$successcounter++
}catch{
write-host "Failed to write StandardInput #$($i.ToString()) to exiftool #$($k.ToString())!" -ForegroundColor Red
$errorcounter++
}
if($k -gt 0){
$k--
}else{
$k = ($exiftoolInstanceCount - 1)
}
}
Write-Progress -Activity "$choiceString..." -Status "Complete!" -Completed
write-host "Close exiftool down..." -ForegroundColor DarkGray
# CREDIT: To get asymmetric buffer readout running (ak.a. unlimited processing) (2/2): https://stackoverflow.com/a/24371479/8013879
[array]$outputerror = @()
[array]$outputout = @()
for($i=0; $i -lt $exiftoolInstanceCount; $i++){
# Close exiftool:
try{
$exiftoolproc[$i].StandardInput.WriteLine("-stay_open`nFalse`n")
$exiftoolproc[$i].WaitForExit()
}catch{
write-host "Failed to exit exiftool #$($i.ToString())!" -ForegroundColor Red
$errorcounter++
}
# Unregistering events to retrieve process output.
try{
Unregister-Event -SourceIdentifier $exiftoolStdOutEvent[$i].Name
Unregister-Event -SourceIdentifier $exiftoolStdErrEvent[$i].Name
}catch{
write-host "Failed to Unregister-Event #$($i.ToString())!" -ForegroundColor Red
$errorcounter++
}
# Read StdErr and StrOut of exiftool, then print it:
$outputerror += @($exiftoolStdErrBuilder[$i].ToString().Trim().Split("`r`n",[System.StringSplitOptions]::RemoveEmptyEntries))
$outputout += @($($exiftoolStdOutBuilder[$i].ToString().Trim().Replace("======== ","").Replace("[1/1]",'').Replace("{ready}","").Replace("1 image files updated","").Replace(" ","").Replace(" ","").Replace("`r`n`r`n","").Split("`r`n",[System.StringSplitOptions]::RemoveEmptyEntries)))
if($exiftoolproc[$i].ExitCode -ne 0){
write-host "exiftool #$i's exit code was not 0 (zero)!" -ForegroundColor Magenta
$errorcounter++
}
}
foreach($i in $outputerror){
if($outputerror[$i].Length -gt 0){
write-host "$($outputerror[$i])`t" -ForegroundColor Red -NoNewline
$errorcounter++
}
}
foreach($i in $outputout){
if($outputout[$i].Length -gt 0){
write-host "$($outputout[$i])" -ForegroundColor Yellow
}
}
write-host "Successfully manipulated $successcounter file(s)." -ForegroundColor Gray
}
Start-EXIFManipulation -WorkingFiles $XMP
if((Read-Host "Delete original XMPs?") -eq 1){
Remove-Item $XMP.xmp
}