This repository has been archived by the owner on Sep 17, 2022. It is now read-only.
/
MarkersExtractorCLI.swift
239 lines (202 loc) · 8.12 KB
/
MarkersExtractorCLI.swift
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
import ArgumentParser
import Foundation
import Logging
import MarkersExtractor
struct MarkersExtractorCLI: ParsableCommand {
static var configuration = CommandConfiguration(
abstract: "Tool to extract markers from FCPXML(D).",
version: "0.1.1"
)
@Option(
help: ArgumentHelp(
"Marker thumb image format.",
valueName: MarkerImageFormat.allCases.map { $0.rawValue }.joined(separator: ",")
)
)
var imageFormat: MarkerImageFormat = MarkersExtractorSettings.Defaults.imageFormat
@Option(
help: ArgumentHelp(
"Image quality percent for JPG.",
valueName: "0-100"
)
)
var imageQuality: Int = MarkersExtractorSettings.Defaults.imageQuality
@Option(help: ArgumentHelp("Limit image width keeping aspect ratio.", valueName: "w"))
var imageWidth: Int?
@Option(help: ArgumentHelp("Limit image height keeping aspect ratio.", valueName: "h"))
var imageHeight: Int?
@Option(
help: ArgumentHelp(
"Limit image size to % keeping aspect ratio. (default for GIF: 50)",
valueName: "%"
)
)
var imageSizePercent: Int?
@Option(help: ArgumentHelp("GIF frame rate.", valueName: "1-50"))
var gifFPS: Int = MarkersExtractorSettings.Defaults.gifFPS
@Option(help: ArgumentHelp("GIF capture span around marker.", valueName: "sec"))
var gifSpan: Int = MarkersExtractorSettings.Defaults.gifSpan
@Option(
help: ArgumentHelp(
"Marker naming mode.",
valueName: MarkerIDMode.allCases
.map { $0.rawValue }.joined(separator: ",")
)
)
var idNamingMode: MarkerIDMode = MarkersExtractorSettings.Defaults.idNamingMode
@Option(
name: [.customLong("label")],
help: ArgumentHelp(
"Label to put on a thumb image, can be used multiple times form multiple labels."
+ " Use --help-labels to get full list of available labels.",
valueName: "label"
)
)
var imageLabels: [MarkerHeader] = []
@Option(
name: [.customLong("label-copyright")],
help: ArgumentHelp(
"Copyright label, will be added after all other labels.",
valueName: "text"
)
)
var imageLabelCopyright: String?
@Option(
name: [.customLong("label-font")],
help: ArgumentHelp("Font for image labels", valueName: "name")
)
var imageLabelFont: String = MarkersExtractorSettings.Defaults.imageLabelFont
@Option(
name: [.customLong("label-font-size")],
help: ArgumentHelp(
"Maximum font size for image labels, "
+ "font size is automatically reduced to fit all labels.",
valueName: "pt"
)
)
var imageLabelFontMaxSize: Int = MarkersExtractorSettings.Defaults.imageLabelFontMaxSize
@Option(
name: [.customLong("label-opacity")],
help: ArgumentHelp("Label opacity percent", valueName: "0-100")
)
var imageLabelFontOpacity: Int = MarkersExtractorSettings.Defaults.imageLabelFontOpacity
@Option(
name: [.customLong("label-font-color")],
help: ArgumentHelp("Label font color", valueName: "#RRGGBB / #RGB")
)
var imageLabelFontColor: String = MarkersExtractorSettings.Defaults.imageLabelFontColor
@Option(
name: [.customLong("label-stroke-color")],
help: ArgumentHelp("Label stroke color", valueName: "#RRGGBB / #RGB")
)
var imageLabelFontStrokeColor: String = MarkersExtractorSettings.Defaults
.imageLabelFontStrokeColor
@Option(
name: [.customLong("label-stroke-width")],
help: ArgumentHelp("Label stroke width, 0 to disable. (default: auto)", valueName: "w")
)
var imageLabelFontStrokeWidth: Int?
@Option(
name: [.customLong("label-align-horizontal")],
help: ArgumentHelp(
"Horizontal alignment of image label.",
valueName: MarkerLabelProperties.AlignHorizontal.allCases
.map { $0.rawValue }.joined(separator: ",")
)
)
var imageLabelAlignHorizontal: MarkerLabelProperties.AlignHorizontal = MarkersExtractorSettings
.Defaults.imageLabelAlignHorizontal
@Option(
name: [.customLong("label-align-vertical")],
help: ArgumentHelp(
"Vertical alignment of image label.",
valueName: MarkerLabelProperties.AlignVertical.allCases
.map { $0.rawValue }.joined(separator: ",")
)
)
var imageLabelAlignVertical: MarkerLabelProperties.AlignVertical = MarkersExtractorSettings
.Defaults.imageLabelAlignVertical
@Flag(help: "Create 'done.txt' file in output directory on successful export.")
var createDoneFile = false
@Option(help: "Log file path.", transform: URL.init(fileURLWithPath:))
var log: URL?
@Option(
help: ArgumentHelp(
"Log level.",
valueName: Logger.Level.allCases.map { $0.rawValue }.joined(separator: ",")
)
)
var logLevel: Logger.Level = .info
@Flag(name: [.customLong("quiet")], help: "Disable log.")
var logQuiet = false
@Flag(help: "List all possible labels to use with --label.")
var helpLabels = false
@Argument(help: "Input FCPXML file / FCPXMLD bundle.", transform: URL.init(fileURLWithPath:))
var fcpxmlPath: URL
@Argument(help: "Output directory.", transform: URL.init(fileURLWithPath:))
var outputDir: URL
mutating func validate() throws {
if let log = log, !FileManager.default.isWritableFile(atPath: log.path) {
throw ValidationError("Cannot write log file at '\(log.path)'")
}
if imageFormat == .gif && imageSizePercent == nil {
imageSizePercent = MarkersExtractorSettings.Defaults.imageSizePercentGIF
}
}
mutating func run() throws {
initLogging(logLevel: logQuiet ? nil : logLevel, logFile: log)
let settings: MarkersExtractorSettings
do {
settings = try MarkersExtractorSettings(
imageFormat: imageFormat,
imageQuality: imageQuality,
imageWidth: imageWidth,
imageHeight: imageHeight,
imageSizePercent: imageSizePercent,
gifFPS: gifFPS,
gifSpan: gifSpan,
idNamingMode: idNamingMode,
imageLabels: imageLabels,
imageLabelCopyright: imageLabelCopyright,
imageLabelFont: imageLabelFont,
imageLabelFontMaxSize: imageLabelFontMaxSize,
imageLabelFontOpacity: imageLabelFontOpacity,
imageLabelFontColor: imageLabelFontColor,
imageLabelFontStrokeColor: imageLabelFontStrokeColor,
imageLabelFontStrokeWidth: imageLabelFontStrokeWidth,
imageLabelAlignHorizontal: imageLabelAlignHorizontal,
imageLabelAlignVertical: imageLabelAlignVertical,
createDoneFile: createDoneFile,
fcpxmlPath: fcpxmlPath,
outputDir: outputDir
)
} catch MarkersExtractorError.validationError(let error) {
throw ValidationError(error)
}
try MarkersExtractor.extract(settings)
}
private func initLogging(logLevel: Logger.Level?, logFile: URL?) {
LoggingSystem.bootstrap { label in
guard let logLevel = logLevel else {
return SwiftLogNoOpLogHandler()
}
var logHandlers: [LogHandler] = [
ConsoleLogHandler.init(label: label)
]
if let logFile = logFile {
do {
logHandlers.append(try FileLogHandler.init(label: label, localFile: logFile))
} catch {
print(
"Cannot write to log file '\(logFile.lastPathComponent)':"
+ " \(error.localizedDescription)"
)
}
}
for i in 0..<logHandlers.count {
logHandlers[i].logLevel = logLevel
}
return MultiplexLogHandler(logHandlers)
}
}
}