Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't crash if trimming fails for some codecs #257

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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