I think I'm looking for a way to check if a video settings key is valid, so I can exclude it if it isn't. But I'm more curious why some keys are valid sometimes, inconsistently. Let me explain.
With my macOS app, I'm trying to write a video with compression quality set, like so
let videoWriter = AVAssetWriterInput(
mediaType: .video,
outputSettings:
[
AVVideoWidthKey: 1000,
AVVideoHeightKey: 1000,
AVVideoCodecKey: AVVideoCodecType.h264,
AVVideoCompressionPropertiesKey: [
AVVideoQualityKey: 0.4
]
]
)
It works on my M1Max (13.4, 13.5), Intel Mac (12.0.1), but for one (for now?) of my users (on 13.5/Intel, Macbook16,1) this line throws an exception and crashes the app.
This is the output
*** -[AVAssetWriterInput initWithMediaType:outputSettings:sourceFormatHint:]
Compression property Quality is not supported for video codec type avc1
Reproducing sample app
This tiny app reproduces the crash for my user (but still, not for me) https://github.com/mortenjust/CompressionCrashTest/blob/main/CompressionCrashTest/ContentView.swift
What I tried
I tried looking into catching objc exceptions (from AV Foundation) with Swift via a wrapper, but it looks like it's a bad idea, and not recommended by Apple.
I also tried changing the codec to .hevc
and that also crashes, saying that Quality
is not a supported key.
I tried asking my user to reboot, no effect.
I tried exporting a video with compression quality 0.1 on my Mac to verify that it's a valid key, and the output video was compressed, small file size and visually distorted.
I tried upgrading from 13.4 to 13.5 to match his specs, but it still doesn't crash for me.
I really wish there was a way to validate a video settings dictionary without crashing.
I found AVCaptureMovieFileOutput / supportedOutputSettingsKeys(for:) that vaguely resembles what I need, but it's made for recording from a camera.