-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamswap.go
160 lines (137 loc) · 4.88 KB
/
camswap.go
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
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
"github.com/fatih/color"
"github.com/google/subcommands"
)
type camswapCmd struct {
restore bool
suffix bool
outDir string
verbose bool
verbose2 bool
newCamModel string
appConfig AppConfig
}
func (*camswapCmd) Name() string { return "camswap" }
func (*camswapCmd) Synopsis() string { return "Swap in a different camera name." }
func (*camswapCmd) Usage() string {
return `camswap [-c CAM_MODEL|-c CAM_ALIAS] [-r] [-s] [-d out_dir] [-v|-vv] file1.jpg [file2.nef ...]:
Swaps a different camera model into the given photos' EXIF data.
Persists the original name in an XMP attribute for restoration with the -r flag.
Exactly one of (-c, -r) is required.
`
}
func (p *camswapCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&p.suffix, "s", false, "Write modified images to new files named with a suffix derived from the camera name/alias, rather than to the originals.")
f.StringVar(&p.outDir, "d", "", "Write modified images to this directory.")
f.BoolVar(&p.verbose, "v", false, "Print full exiftool output for each image.")
f.BoolVar(&p.verbose2, "vv", false, "Print exiftool commands and full exiftool output.")
f.StringVar(&p.newCamModel, "c", "", "Camera model to swap in (or alias defined in camswap_aliases).")
f.BoolVar(&p.restore, "r", false, "Restore the original camera name from xtool's XMP attribute.")
}
func (p *camswapCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
if p.verbose2 {
p.verbose = true
}
if len(f.Args()) == 0 || (!p.restore && p.newCamModel == "") || (p.restore && p.newCamModel != "") {
f.Usage()
return subcommands.ExitUsageError
}
p.appConfig = AppConfigFromCtx(ctx)
exiftoolConfigFilename, err := getExiftoolConfigFileName()
if err != nil {
ErrPrint(ctx, err)
return subcommands.ExitFailure
}
//goland:noinspection GoUnhandledErrorResult
defer os.Remove(exiftoolConfigFilename)
var exiftoolArgs []string
if p.restore {
exiftoolArgs = []string{
"-config", exiftoolConfigFilename,
"-Model<XtoolOriginalCameraModel",
"-XtoolOriginalCameraModel=",
"-if", "$XtoolOriginalCameraModel",
}
if p.outDir != "" && p.suffix {
exiftoolArgs = append(exiftoolArgs, "-o", fmt.Sprintf("%s%s%%f_unswap.%%e", p.outDir, string(os.PathSeparator)))
} else if p.suffix {
exiftoolArgs = append(exiftoolArgs, "-o", "%d%f_unswap.%e")
} else if p.outDir != "" {
exiftoolArgs = append(exiftoolArgs, "-o", fmt.Sprintf("%s%s", p.outDir, string(os.PathSeparator)))
}
} else {
newModel := p.newCamModel
if p.appConfig.CamswapAliases[p.newCamModel] != "" {
newModel = p.appConfig.CamswapAliases[p.newCamModel]
}
exiftoolArgs = []string{
"-config", exiftoolConfigFilename,
"-XtoolOriginalCameraModel<Model",
fmt.Sprintf("-Model=%s", newModel),
"-if", "not $XtoolOriginalCameraModel",
}
suffixSafeCamModel := strings.Replace(p.newCamModel, " ", "-", -1)
if p.outDir != "" && p.suffix {
exiftoolArgs = append(exiftoolArgs, "-o", fmt.Sprintf("%s%s%%d%%f_%s.%%e", p.outDir, string(os.PathSeparator), suffixSafeCamModel))
} else if p.suffix {
exiftoolArgs = append(exiftoolArgs, "-o", fmt.Sprintf("%%d%%f_%s.%%e", suffixSafeCamModel))
} else if p.outDir != "" {
exiftoolArgs = append(exiftoolArgs, "-o", fmt.Sprintf("%s%s", p.outDir, string(os.PathSeparator)))
}
}
successes, failures := ExiftoolProcess(
ctx,
exiftoolArgs,
f.Args(),
p.appConfig,
p.verbose,
p.verbose2,
)
boldWhitePrintf := color.New(color.Bold, color.FgWhite).PrintfFunc()
boldRedPrintf := color.New(color.Bold, color.FgRed).PrintfFunc()
boldWhitePrintf("\ncamswap: successfully processed %d images.\n", len(successes))
if len(failures) != 0 {
boldRedPrintf("Errors:\n")
for filename, err := range failures {
errString := err.Error()
if strings.Contains(err.Error(), "failed condition") {
if p.restore {
errString = "no camera swap metadata attached"
} else {
errString = "has already been camswapped"
}
}
fmt.Printf("- %s %s\n", color.MagentaString("%s:", filename), errString)
}
return subcommands.ExitFailure
}
return subcommands.ExitSuccess
}
func getExiftoolConfigFileName() (string, error) {
exiftoolConfigFile, err := os.CreateTemp("", "xtool_xmp")
if err != nil {
return "", fmt.Errorf("failed to create exiftool XMP config file: %w", err)
}
exiftoolConfigFilename := exiftoolConfigFile.Name()
if _, err = exiftoolConfigFile.Write([]byte(exiftoolXtoolXmpConfig)); err != nil {
return "", fmt.Errorf("failed to write exiftool XMP config file: %w", err)
}
if err = exiftoolConfigFile.Close(); err != nil {
return "", fmt.Errorf("failed to close exiftool XMP config file: %w", err)
}
return exiftoolConfigFilename, nil
}
const exiftoolXtoolXmpConfig = `
%Image::ExifTool::UserDefined = (
'Image::ExifTool::XMP::xmp' => {
XtoolOriginalCameraModel => { },
},
);
1;
`