开发者

H264 stream writing using AVAssetWriter issue

开发者 https://www.devze.com 2023-04-04 07:11 出处:网络
i am working on an application that receives the H264 stream from network and writes that stream to the MOV file. For writing t开发者_如何学JAVAo MOV I am using AVAssetWriter(with AVAssetWriterInput s

i am working on an application that receives the H264 stream from network and writes that stream to the MOV file. For writing t开发者_如何学JAVAo MOV I am using AVAssetWriter(with AVAssetWriterInput settings nil). Although I am able to dump and play the stream in MOV format using AVAsssetWriter in Quicktime, if the movie is seeked in QUicktime while playing the movie gets fully distorted until the next I frame occurs. With MOV files such behavior is not expected.

Also tried with specifying the AVAssetWriterInput settings to QuicktimeMovie, Width, Height, Bitrate. But this fails while writing from the first frame. Also observed that if raw frames are used instead of H264 stream and specify output settings, seeing is working correctly.

Also tried with specifying the following Attachments to CMSampleBuffer before giving it to AVAssetWriterInput. But still seeking is not proper with the MOV file.

kCMSampleAttachmentKey_IsDependedOnByOthers 

for I frame and

kCMSampleAttachmentKey_DependsOnOthers
kCMSampleAttachmentKey_NotSync

for P frames

Please give suggestion on how to correct the seeking for MOV file using AVAssetWriter. I guess there is some sync frame offset table problem when using AVAssetWriter for already encoded H264 frames.


I worked on an iOS camera app and built a video recording pipeline using VTCompressionSession for H264 encoding and AVAssetWriter for muxing frames into an MP4 container. I experienced the same issue as you with the output video file. When I played it on iPhone's Photo app and the movie was seeking on the timeline view there were frames distorted and the preview was choppy. There are no technical differences in writing video into MP4 and MOV using AVAssetWriter. And root reason for that was that keyframes were not written into the container file correctly. According to Apple's document, all samples are assumed to be sync samples by default, so we have to add not sync info to samples. Here is the link URL and sample code in Objective C. https://developer.apple.com/documentation/coremedia/cmsamplebuffer-u71

if (frame->type != VideoFrameType::SYNC) {
    CFArrayRef sampleAttachments = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true);
    CFMutableDictionaryRef sampleAttachmentsDic = (CFMutableDictionaryRef)CFArrayGetValueAtIndex(sampleAttachments, 0);
    CFDictionarySetValue(sampleAttachmentsDic, kCMSampleAttachmentKey_NotSync, kCFBooleanTrue);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消