Skip to content

Commit

Permalink
Warn if codec is not supported for trimming operation instead of cras…
Browse files Browse the repository at this point in the history
…hing

See sindresorhus#254
  • Loading branch information
codetheweb committed Oct 3, 2021
1 parent c2c1dfa commit ea3f790
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Gifski/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ extension AVAssetTrack {
case videoTrackIsEmpty
case assetIsMissingVideoTrack
case compositionCouldNotBeCreated
case codecNotSupported
}

/**
Expand All @@ -612,6 +613,16 @@ extension AVAssetTrack {
This can be useful to trim blank frames from files produced by tools like the iOS simulator screen recorder.
*/
func trimmingBlankFrames() throws -> AVAssetTrack {
// See https://github.com/sindresorhus/Gifski/issues/254 for context.
// TL;DR is that some codecs seem to always report a buffer size of 0
// when reading, breaking this function.
let buggyCodecs = ["v210", "BGRA"]
if let codecIdentifier = self.codecIdentifier {
if buggyCodecs.contains(codecIdentifier) {
throw VideoTrimmingError.codecNotSupported
}
}

// Create new composition
let composition = AVMutableComposition()
guard
Expand Down Expand Up @@ -665,6 +676,8 @@ extension AVAssetTrack.VideoTrimmingError: LocalizedError {
return "Asset is missing video track."
case .compositionCouldNotBeCreated:
return "Composition could not be created."
case .codecNotSupported:
return "Video codec is not supported."
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions Gifski/VideoValidator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ struct VideoValidator {
do {
let trimmedAsset = try newAsset.trimmingBlankFramesFromFirstVideoTrack()
return .success(trimmedAsset, newVideoMetadata)
} catch AVAssetTrack.VideoTrimmingError.codecNotSupported {
NSAlert.showModal(
for: window,
title: "Could not trim empty leading frames from video.",
message: "We can't trim empty leading frames from this codec, sorry. You can still create a GIF."
)

// Allow user to continue
return .success(newAsset, newVideoMetadata)
} catch {
NSAlert.showModalAndReportToCrashlytics(
for: window,
Expand Down

0 comments on commit ea3f790

Please sign in to comment.