Skip to content

Instantly share code, notes, and snippets.

@samsonjs
Last active June 29, 2024 05:10
Show Gist options
  • Save samsonjs/d05923e4e020670ea1a1542a5233a04a to your computer and use it in GitHub Desktop.
Save samsonjs/d05923e4e020670ea1a1542a5233a04a to your computer and use it in GitHub Desktop.
Exploring possible modern APIs for something like AVAssetExportSession
// Old way
let session = VFAExportSession(asset: asset)
session.timeRange = CMTimeRange(start: .seconds(1), duration: .seconds(3))
session.audioMix = audioMix
session.audioOutputConfiguration = [
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVNumberOfChannelsKey: NSNumber(value: 2),
AVSampleRateKey: NSNumber(value: 44_100.0),
]
session.videoComposition = videoComposition
session.videoOutputConfiguration = [
AVVideoCodecKey: AVVideoCodecType.h264.rawValue,
AVVideoWidthKey: NSNumber(value: Int(size.width)),
AVVideoHeightKey: NSNumber(value: Int(size.height)),
AVVideoCompressionPropertiesKey: [
AVVideoAverageBitRateKey: NSNumber(value: videoBitrate),
AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel as String,
] as [String: Any],
// Strip out HDR since Mastodon doesn't support it
AVVideoColorPropertiesKey: [
AVVideoColorPrimariesKey: AVVideoColorPrimaries_ITU_R_709_2,
AVVideoTransferFunctionKey: AVVideoTransferFunction_ITU_R_709_2,
AVVideoYCbCrMatrixKey: AVVideoYCbCrMatrix_ITU_R_709_2,
],
]
session.optimizeForNetworkUse = true
// New way?
let session = VFAExportSession(asset: asset)
.timeRange(CMTimeRange(start: .seconds(1), duration: .seconds(3)))
.audio(mix: audioMix, output: .format(.m4a).channels(2).sampleRate(44_100))
.video(
composition: videoComposition,
output: .format(.h264(profile: .highAuto))
.dimensions(width: 1920, height: 1080)
.bitrate(5_000_000)
.color(.ITU_R_709)
)
.optimizeForNetworkUse()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment