Skip to content

Instantly share code, notes, and snippets.

@petershine
Created October 29, 2015 06:02
Show Gist options
  • Save petershine/de5e3d8487f4cfca0a1d to your computer and use it in GitHub Desktop.
Save petershine/de5e3d8487f4cfca0a1d to your computer and use it in GitHub Desktop.
Essential snippet for compressing original CMSampleBufferRef into H.264 sample
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer);
CMVideoDimensions videoDimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
static VTCompressionSessionRef compressionSession;
if (compressionSession == NULL) {
VTCompressionSessionCreate
(NULL,
videoDimensions.width,
videoDimensions.height,
kCMVideoCodecType_H264,
NULL,
NULL,
NULL,
NULL,
NULL,
&compressionSession);
}
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CMTime presentationTimestamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
CMTime duration = CMSampleBufferGetDuration(sampleBuffer);
VTCompressionSessionEncodeFrameWithOutputHandler
(compressionSession,
pixelBuffer,
presentationTimestamp,
duration,
NULL,
NULL,
^(OSStatus status,
VTEncodeInfoFlags infoFlags,
CMSampleBufferRef _Nullable compressedSample) {
//MARK: The compressedSample can also be used for display
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment