Skip to content

Commit

Permalink
Don't crash if trimming fails for some codecs (#257)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
codetheweb and sindresorhus committed Oct 4, 2021
1 parent 3ce0fe3 commit 33b6384
Show file tree
Hide file tree
Showing 2 changed files with 16 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.
// In short: Some codecs seem to always report a sample buffer size of 0 when reading, breaking this function. (macOS 11.6)
let buggyCodecs = ["v210", "BGRA"]
if
let codecIdentifier = codecIdentifier,
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
3 changes: 3 additions & 0 deletions Gifski/VideoValidator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ struct VideoValidator {
do {
let trimmedAsset = try newAsset.trimmingBlankFramesFromFirstVideoTrack()
return .success(trimmedAsset, newVideoMetadata)
} catch AVAssetTrack.VideoTrimmingError.codecNotSupported {
// Allow user to continue
return .success(newAsset, newVideoMetadata)
} catch {
NSAlert.showModalAndReportToCrashlytics(
for: window,
Expand Down

0 comments on commit 33b6384

Please sign in to comment.