Save MPEG-TS (h264 or HEVC) video stream using AVAssetWriter.

I'm capturing video stream from GoPro camera (I demux UDP MPEG-TS packets) and create CMSampleBuffers from them, this works fine when I display them using CMSampleBufferLayer.

However when I dump them to disk using AVAssetWriter and then playback it with AVPlayer, AVPlayer has problems with scrubbing, it also cannot render previous frames, it needs to go back to key frames. Also thumbnails generated with AVAssetImageGenerator are mostly distorted and green, even though I set the requestedTimeToleranceAfter longer than the key frames frequency.

When I re-encode saved video once again with AVAssetExportSession and play it back then I can scrub the video just fine.

Is it because re-transcoding adds additional metadata to enable generating frames when rewinding the video and scrubbing? If so is there a way to achieve it with AVAssetWriter without much time penalty? I need the dump/save operation to be very fast.

I also considered the following: Instead of de-muxing video and creating CMSampleBuffers, maybe I could directly dump the stream to disk and somehow add moov atoms with timing information. Would this approach work? If so where I can find information how to do it?

Thank you!

Answered by galad87 in 837140022

Do you set the CMSampleBuffer kCMSampleBufferAttachmentKey* keys for keyframes? I don't think AVAssetWriter will scan the bitstream for that info, it's something you will have to provide yourself.

Accepted Answer

Do you set the CMSampleBuffer kCMSampleBufferAttachmentKey* keys for keyframes? I don't think AVAssetWriter will scan the bitstream for that info, it's something you will have to provide yourself.

Thank you this worked!!! Amazing!

I set a combination of: kCMSampleAttachmentKey_NotSync, kCMSampleBufferAttachmentKey_ForceKeyFrame and kCMSampleAttachmentKey_DependsOnOthers depending on key frame status and it works fine. It also solved broken/greenish thumbnails.

I'm so happy:)

Save MPEG-TS (h264 or HEVC) video stream using AVAssetWriter.
 
 
Q