VideoToolboxPlus is a collection of VideoToolbox helpers and extensions. It also adds some Swift-y syntax to the VideoToolbox API.
Simply add the VideoToolboxPlus.swift file to your project.
Turn code like this:
varsession:VTCompressionSession?=nil
letstatus=VTCompressionSessionCreate(nil,
width,
height,
kCMVideoCodecType_H264,
nil,
attributes,
nil,
callback,
unsafeBitCast(self,to:UnsafeMutablePointer<Void>.self),
&session)
varproperties:[NSString:NSObject]=[
kVTCompressionPropertyKey_RealTime:kCFBooleanTrue,
kVTCompressionPropertyKey_ProfileLevel:profileLevel,
kVTCompressionPropertyKey_AverageBitRate:bitrate,
kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration:maxKeyFrameIntervalDuration
]
VTSessionSetProperties(_session!,properties)
VTCompressionSessionPrepareToEncodeFrames(_session!)
letimageBuffer:CVImageBuffer!=CMSampleBufferGetImageBuffer(sampleBuffer)
letstatus=VTCompressionSessionEncodeFrame(session,
imageBuffer,
presentationTimeStamp,
presentationDuration,
nil,
nil,
&flags)
if status==noErr{
// handle error
}else{
// handle success
}
Into:
varsession:VTCompressionSession!=VTCompressionSession.new(width:width,
height:height,
codec:kCMVideoCodecType_H264,
callback:callback)
session.isRealtime=true
session.profile=profileLevel
session.averageBitrate=bitrate
session.maxKeyframeIntervalDuration=maxKeyframeIntervalDuration
session.prepare()
do{
trysession.encode(buffer:sampleBuffer)
}catch{
// handle error
}
// handle success
VideoToolboxPlus is released under the MIT license.
Any questions or comments should be made into issues, or pull requests if you want to write some code.