/
GIFGenerator.swift
461 lines (394 loc) Β· 13 KB
/
GIFGenerator.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import Foundation
import AVFoundation
import FirebaseCrashlytics
private var conversionCount = 0
final class GIFGenerator {
private var gifski: Gifski?
private var progress: Progress!
private(set) var sizeMultiplierForEstimation = 1.0
deinit {
cancel()
}
/**
Converts a movie to GIF.
- Parameter completionHandler: Guaranteed to be called on the main thread
*/
func run(
_ conversion: Conversion,
isEstimation: Bool,
completionHandler: @escaping (Result<Data, Error>) -> Void
) {
// For debugging.
conversionCount += 1
let jobKey = "Conversion \(conversionCount)"
progress = Progress(parent: .current())
let completionHandlerOnce = Once().wrap { [weak self] (_ result: Result<Data, Error>) in
// Ensure Gifski finishes no matter what.
self?.gifski = nil
DispatchQueue.main.async {
guard
let self,
!self.progress.isCancelled
else {
completionHandler(.failure(.cancelled))
return
}
completionHandler(result)
}
}
gifski = Gifski(
dimensions: conversion.dimensions,
quality: conversion.quality,
loop: conversion.loop
)
gifski?.onProgress = { [weak self] in
self?.progress.completedUnitCount += 1
}
DispatchQueue.global(qos: .utility).async { [weak self] in
self?.generateData(
for: conversion,
isEstimation: isEstimation,
jobKey: jobKey,
completionHandler: completionHandlerOnce
)
}
}
/**
Generates GIF data for the provided conversion.
- Parameters:
- conversion: The source information of the conversion.
- isEstimation: Whether the frame is part of a size estimation job.
- jobKey: The string used to identify the current conversion job.
- completionHandler: Closure called when the data conversion completes or an error is encountered.
*/
private func generateData(
for conversion: Conversion,
isEstimation: Bool,
jobKey: String,
completionHandler: @escaping (Result<Data, Error>) -> Void
) {
let generator: AVAssetImageGenerator
var times: [CMTime]
let fps: Int
switch imageGenerator(for: conversion, jobKey: jobKey) {
case .success(let result):
generator = result.generator
times = result.times
fps = result.fps
case .failure(let error):
completionHandler(.failure(error))
return
}
// TODO: The whole estimation thing should be split out into a separate method and the things that are shared should also be split out.
if isEstimation {
let originalCount = times.count
if originalCount > 25 {
times = times
.chunked(by: 5)
.sample(length: 5)
.flatten()
}
sizeMultiplierForEstimation = Double(originalCount) / Double(times.count)
}
progress.cancellationHandler = generator.cancelAllCGImageGeneration
progress.totalUnitCount = Int64(totalFrameCount(for: conversion, sourceFrameCount: times.count))
let startTime = times.first?.seconds ?? 0
generator.generateCGImagesAsynchronously(forTimePoints: times) { [weak self] imageResult in
guard
let self,
!self.progress.isCancelled
else {
completionHandler(.failure(.cancelled))
return
}
let frameResult = processFrame(
for: imageResult,
at: .seconds(startTime),
frameRate: fps,
conversion: conversion,
isEstimation: isEstimation,
jobKey: jobKey
)
switch frameResult {
case .success(let finished):
if finished {
guard let gifski else {
completionHandler(.failure(.cancelled))
return
}
let result = Result<Data, Swift.Error> {
try gifski.finish()
}
.mapError(Error.writeFailed)
completionHandler(result)
}
case .failure(let error):
completionHandler(.failure(error))
}
}
}
/**
Creates an image generator for the provided conversion.
- Parameters:
- conversion: The conversion source of the image generator.
- jobKey: The string used to identify the current conversion job.
- Returns: An `AVAssetImageGenerator` along with the times of the frames requested by the conversion.
*/
private func imageGenerator(
for conversion: Conversion,
jobKey: String
) -> Result<(generator: AVAssetImageGenerator, times: [CMTime], fps: Int), Error> {
let asset = conversion.asset
record(
jobKey: jobKey,
key: "Is readable?",
value: asset.isReadable
)
record(
jobKey: jobKey,
key: "First video track",
value: asset.firstVideoTrack
)
record(
jobKey: jobKey,
key: "First video track time range",
value: asset.firstVideoTrack?.timeRange
)
record(
jobKey: jobKey,
key: "Duration",
value: asset.duration.seconds
)
record(
jobKey: jobKey,
key: "AVAsset debug info",
value: asset.debugInfo
)
guard
asset.isReadable,
let assetFrameRate = asset.frameRate,
let firstVideoTrack = asset.firstVideoTrack,
// We use the duration of the first video track since the total duration of the asset can actually be longer than the video track. If we use the total duration and the video is shorter, we'll get errors in `generateCGImagesAsynchronously` (#119).
// We already extract the video into a new asset in `VideoValidator` if the first video track is shorter than the asset duration, so the handling here is not strictly necessary but kept just to be safe.
let videoTrackRange = firstVideoTrack.timeRange.range
else {
// This can happen if the user selects a file, and then the file becomes
// unavailable or deleted before the "Convert" button is clicked.
return .failure(.unreadableFile)
}
record(
jobKey: jobKey,
key: "AVAsset debug info2",
value: asset.debugInfo
)
let generator = AVAssetImageGenerator(asset: asset)
generator.appliesPreferredTrackTransform = true
generator.requestedTimeToleranceBefore = .zero
generator.requestedTimeToleranceAfter = .zero
// We are intentionally not setting a `generator.maximumSize` as it's buggy: https://github.com/sindresorhus/Gifski/pull/278
// Even though we enforce a minimum of 3 FPS in the GUI, a source video could have lower FPS, and we should allow that.
var fps = (conversion.frameRate.map(Double.init) ?? assetFrameRate).clamped(to: 0.1...Constants.allowedFrameRate.upperBound)
fps = min(fps, assetFrameRate)
print("FPS:", fps)
// TODO: Instead of calculating what part of the video to get, we could just trim the actual `AVAssetTrack`.
let videoRange = conversion.timeRange?.clamped(to: videoTrackRange) ?? videoTrackRange
let startTime = videoRange.lowerBound
let duration = videoRange.length
let frameCount = Int(duration * fps)
let timescale = firstVideoTrack.naturalTimeScale
guard frameCount >= 2 else {
return .failure(.notEnoughFrames(frameCount))
}
print("Frame count:", frameCount)
let frameStep = 1 / fps
var frameForTimes: [CMTime] = (0..<frameCount).map { index in
let presentationTimestamp = startTime + (frameStep * Double(index))
return CMTime(
seconds: presentationTimestamp,
preferredTimescale: timescale
)
}
// We don't do this when "bounce" is enabled as the bounce calculations are not able to handle this.
if !conversion.bounce {
// Ensure we include the last frame. For example, the above might have calculated `[..., 6.25, 6.3]`, but the duration is `6.3647`, so we might miss the last frame if it appears for a short time.
frameForTimes.append(CMTime(seconds: duration, preferredTimescale: timescale))
}
record(
jobKey: jobKey,
key: "fps",
value: fps
)
record(
jobKey: jobKey,
key: "videoRange",
value: videoRange
)
record(
jobKey: jobKey,
key: "frameCount",
value: frameCount
)
record(
jobKey: jobKey,
key: "frameForTimes",
value: frameForTimes.map(\.seconds)
)
return .success((generator, frameForTimes, Int(fps)))
}
/**
Generates image data from an image frame and sends that data to the Gifski image API for processing.
- Parameters:
- result: The image size.
- startTime: The start time of all the frames being processed. (Not the time of the current frame).
- frameRate: The frames per second of the job.
- conversion: The source information of the conversion.
- isEstimation: Whether the frame is part of a size estimation job.
- jobKey: The string used to identify the current conversion job.
- Returns: A result containing whether an error occurred or if the frame is the last frame in the conversion.
*/
private func processFrame(
for result: Result<AVAssetImageGenerator.CompletionHandlerResult, Swift.Error>,
at startTime: Duration,
frameRate: Int,
conversion: Conversion,
isEstimation: Bool,
jobKey: String
) -> Result<Bool, Error> {
switch result {
case .success(let result):
let totalFrameCount = totalFrameCount(for: conversion, sourceFrameCount: result.totalCount)
progress.totalUnitCount = Int64(totalFrameCount)
// This happens if the last frame in the video failed to be generated.
if result.isFinishedIgnoreImage {
return .success(true)
}
if !isEstimation, result.completedCount == 1 {
record(
jobKey: jobKey,
key: "CGImage",
value: result.image.debugInfo
)
}
// TODO: This is just a workaround. Look into the cause of this.
// https://github.com/sindresorhus/Gifski/pull/262
// Skip incorrect out-of-range frames.
if result.actualTime.seconds < startTime.toTimeInterval {
return .success(true)
}
do {
let frameNumber = result.completedCount - 1
assert(result.actualTime.seconds > 0 || frameNumber == 0)
try gifski?.addFrame(
result.image,
presentationTimestamp: max(0, result.actualTime.seconds - startTime.toTimeInterval)
)
if conversion.bounce, !result.isFinished {
/*
Inserts the frame again at the reverse index of the natural order.
For example, if this frame is at index 2 of 5 in its natural order:
```
β
0, 1, 2, 3, 4
```
Then the frame should be inserted at 6 of 9 in the reverse order:
```
β
0, 1, 2, 3, 4, 3, 2, 1, 0
```
*/
let reverseFrameNumber = totalFrameCount - frameNumber - 1
// Determine the reverse timestamp by finding the expected timestamp (frame number / frame rate) and adjusting for the image generator's slippage (actualTime - requestedTime)
let expectedReverseTimestamp = TimeInterval(reverseFrameNumber) / TimeInterval(frameRate)
let timestampSlippage = result.actualTime - result.requestedTime
let actualReverseTimestamp = max(0, expectedReverseTimestamp + timestampSlippage.seconds)
try gifski?.addFrame(
result.image,
frameNumber: reverseFrameNumber,
presentationTimestamp: actualReverseTimestamp
)
}
} catch {
return .failure(.addFrameFailed(error))
}
return .success(result.isFinished)
case .failure where result.isCancelled:
return .failure(.cancelled)
case .failure(let error):
return .failure(.generateFrameFailed(error))
}
}
private func totalFrameCount(for conversion: Conversion, sourceFrameCount: Int) -> Int {
/*
Bouncing doubles the frame count except for the frame at the apex (middle) of the bounce.
For example, a sequence of 5 frames becomes a sequence of 9 frames when bounced:
```
0, 1, 2, 3, 4
β
0, 1, 2, 3, 4, 3, 2, 1, 0
```
*/
conversion.bounce ? (sourceFrameCount * 2 - 1) : sourceFrameCount
}
func cancel() {
progress?.cancel()
}
}
extension GIFGenerator {
/**
- Parameter frameRate: Clamped to `5...30`. Uses the frame rate of `input` if not specified.
- Parameter loopGif: Whether output should loop infinitely or not.
- Parameter bounce: Whether output should bounce or not.
*/
struct Conversion {
let asset: AVAsset
let sourceURL: URL
var timeRange: ClosedRange<Double>?
var quality: Double = 1
var dimensions: (width: Int, height: Int)?
var frameRate: Int?
var loop: Gifski.Loop
var bounce: Bool
var gifDuration: Duration {
guard let duration = (timeRange ?? asset.firstVideoTrack?.timeRange.range)?.length else {
return .zero
}
return .seconds(bounce ? (duration * 2) : duration)
}
}
}
extension GIFGenerator {
enum Error: LocalizedError {
case invalidSettings
case unreadableFile
case notEnoughFrames(Int)
case generateFrameFailed(Swift.Error)
case addFrameFailed(Swift.Error)
case writeFailed(Swift.Error)
case cancelled
var errorDescription: String? {
switch self {
case .invalidSettings:
return "Invalid settings."
case .unreadableFile:
return "The selected file is no longer readable."
case .notEnoughFrames(let frameCount):
return "An animated GIF requires a minimum of 2 frames. Your video contains \(frameCount) frame\(frameCount == 1 ? "" : "s")."
case .generateFrameFailed(let error):
return "Failed to generate frame: \(error.localizedDescription)"
case .addFrameFailed(let error):
return "Failed to add frame, with underlying error: \(error.localizedDescription)"
case .writeFailed(let error):
return "Failed to write, with underlying error: \(error.localizedDescription)"
case .cancelled:
return "The conversion was cancelled."
}
}
}
}
private func record(jobKey: String, key: String, value: Any?) {
debugPrint("\(jobKey): \(key): \(value ?? "nil")")
Crashlytics.record(
key: "\(jobKey): \(key)",
value: value
)
}