Skip to content

Commit

Permalink
Fix crash when the video duration cannot be determined
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jun 21, 2020
1 parent 648422e commit 97914d0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
18 changes: 2 additions & 16 deletions Gifski.xcodeproj/project.pbxproj
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 52;
objectVersion = 53;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -342,7 +342,7 @@
};
};
buildConfigurationList = E3AE627E1E5CD2F300035A2F /* Build configuration list for PBXProject "Gifski" */;
compatibilityVersion = "Xcode 11.0";
compatibilityVersion = "Xcode 11.4";
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
Expand Down Expand Up @@ -700,7 +700,6 @@
E3AE62911E5CD2F300035A2F /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Gifski/Gifski.entitlements;
Expand All @@ -724,12 +723,6 @@
"$(inherited)",
/usr/local/opt/gcc/lib/gcc/9,
);
OTHER_LDFLAGS = (
"-weak_framework",
Combine,
"-weak_framework",
SwiftUI,
);
PRODUCT_BUNDLE_IDENTIFIER = com.sindresorhus.Gifski;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand All @@ -744,7 +737,6 @@
E3AE62921E5CD2F300035A2F /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Gifski/Gifski.entitlements;
Expand All @@ -768,12 +760,6 @@
"$(inherited)",
/usr/local/opt/gcc/lib/gcc/9,
);
OTHER_LDFLAGS = (
"-weak_framework",
Combine,
"-weak_framework",
SwiftUI,
);
PRODUCT_BUNDLE_IDENTIFIER = com.sindresorhus.Gifski;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
7 changes: 6 additions & 1 deletion Gifski/VideoValidator.swift
Expand Up @@ -32,7 +32,12 @@ struct VideoValidator {
return .failure
}

let asset = AVURLAsset(url: inputUrl)
let asset = AVURLAsset(
url: inputUrl,
options: [
AVURLAssetPreferPreciseDurationAndTimingKey: true
]
)

Crashlytics.record(key: "AVAsset debug info", value: asset.debugInfo)

Expand Down
21 changes: 19 additions & 2 deletions Gifski/util.swift
Expand Up @@ -885,7 +885,7 @@ extension AVAsset {
Extension: \(describing: (self as? AVURLAsset)?.url.fileExtension)
Video codec: \(describing: videoCodec?.debugDescription)
Audio codec: \(describing: audioCodec)
Duration: \(describing: durationFormatter.string(from: duration.seconds))
Duration: \(describing: durationFormatter.stringSafe(from: duration.seconds))
Dimension: \(describing: dimensions?.formatted)
Frame rate: \(describing: frameRate?.rounded(toDecimalPlaces: 2).formatted)
File size: \(fileSizeFormatted)
Expand All @@ -903,7 +903,7 @@ extension AVAsset {
----
Type: \(track.mediaType.debugDescription)
Codec: \(describing: track.mediaType == .video ? track.codec?.debugDescription : track.codecString)
Duration: \(describing: durationFormatter.string(from: track.timeRange.duration.seconds))
Duration: \(describing: durationFormatter.stringSafe(from: track.timeRange.duration.seconds))
Dimensions: \(describing: track.dimensions?.formatted)
Natural size: \(describing: track.naturalSize)
Frame rate: \(describing: track.frameRate?.rounded(toDecimalPlaces: 2).formatted)
Expand Down Expand Up @@ -3023,3 +3023,20 @@ extension AVPlayer {
}
}
}


extension DateComponentsFormatter {
/// Like `string(from: TimeInterval)` but does not cause an `NSInternalInconsistencyException` exception for `NaN` and `Infinity`.
/// This is especially useful when formatting `CMTime#seconds` which can often be `NaN`.
func stringSafe(from timeInterval: TimeInterval) -> String? {
guard !timeInterval.isNaN else {
return "NaN"
}

guard timeInterval.isFinite else {
return "Infinity"
}

return string(from: timeInterval)
}
}

0 comments on commit 97914d0

Please sign in to comment.