Search code examples
iphoneiosipadh.264

How can we get H.264 encoded video stream from iPhone Camera?


I am using following to get video sample buffer:

- (void) writeSampleBufferStream:(CMSampleBufferRef)sampleBuffer ofType:(NSString *)mediaType

Now my question is that how can I get h.264 encoded NSData from above sampleBuffer. Please suggest.


Solution

  • Update for 2017:

    You can do streaming Video and Audio now by using the VideoToolbox API. Read the documentation here: VTCompressionSession

    Original answer (from 2013):

    Short: You can't, the sample buffer you receive is uncompressed.

    Methods to get hardware accelerated h264 compression:

    As you can see both write to a file, writing to a pipe does not work as the encoder updates header information after a frame or GOP has been fully written. So you better don't touch the file while the encoder writes to it as it does randomly rewrite header information. Without this header information the video file will not be playable (it updates the size field, so the first header written says the file is 0 bytes). Directly writing to a memory area is not supported currently. But you can open the encoded video-file and demux the stream to get to the h264 data (after the encoder has closed the file of course)