6

This has bothering me for a while. i have video convert to convert video into “.mp4” format. But there is a crash that happens on some video but not all.

here is the crash log

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVAssetWriterInput appendSampleBuffer:] 
Cannot append sample buffer: First input buffer must have an appropriate kCMSampleBufferAttachmentKey_TrimDurationAtStart since the codec has encoder delay'

here is my codes:

NSURL *uploadURL = [NSURL fileURLWithPath:[[NSTemporaryDirectory() stringByAppendingPathComponent:[self getVideoName]] stringByAppendingString:@".mp4"]];

AVAssetTrack *videoTrack = [[self.avAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
CGSize videoSize = videoTrack.naturalSize;
NSDictionary *videoWriterCompressionSettings =  [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1250000], AVVideoAverageBitRateKey, nil];
NSDictionary *videoWriterSettings = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecH264, AVVideoCodecKey, videoWriterCompressionSettings, AVVideoCompressionPropertiesKey, [NSNumber numberWithFloat:videoSize.width], AVVideoWidthKey, [NSNumber numberWithFloat:videoSize.height], AVVideoHeightKey, nil];
AVAssetWriterInput* videoWriterInput = [AVAssetWriterInput
                                        assetWriterInputWithMediaType:AVMediaTypeVideo
                                        outputSettings:videoWriterSettings];
videoWriterInput.expectsMediaDataInRealTime = YES;
videoWriterInput.transform = videoTrack.preferredTransform;

self.assetWriter = [[AVAssetWriter alloc] initWithURL:uploadURL fileType:AVFileTypeQuickTimeMovie error:nil];
[self.assetWriter addInput:videoWriterInput];

//setup video reader
NSDictionary *videoReaderSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
AVAssetReaderTrackOutput *videoReaderOutput = [[AVAssetReaderTrackOutput alloc] initWithTrack:videoTrack outputSettings:videoReaderSettings];
self.assetReader = [[AVAssetReader alloc] initWithAsset:self.avAsset error:nil];
[self.assetReader addOutput:videoReaderOutput];

//setup audio writer
AVAssetWriterInput* audioWriterInput = [AVAssetWriterInput
                                        assetWriterInputWithMediaType:AVMediaTypeAudio
                                        outputSettings:nil];

audioWriterInput.expectsMediaDataInRealTime = NO;
[self.assetWriter addInput:audioWriterInput];

//setup audio reader
AVAssetTrack* audioTrack = [[self.avAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
AVAssetReaderOutput *audioReaderOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:audioTrack outputSettings:nil];
AVAssetReader *audioReader = [AVAssetReader assetReaderWithAsset:self.avAsset error:nil];
[audioReader addOutput:audioReaderOutput];

[self.assetWriter startWriting];
[self.assetReader startReading];
[self.assetWriter startSessionAtSourceTime:kCMTimeZero];

dispatch_queue_t processingQueue = dispatch_queue_create("processingQueue1", NULL);
[videoWriterInput requestMediaDataWhenReadyOnQueue:processingQueue
                                        usingBlock:^{
     while ([videoWriterInput isReadyForMoreMediaData])
     {
         CMSampleBufferRef sampleBuffer = NULL;
         if ([self.assetReader status] == AVAssetReaderStatusReading &&
             (sampleBuffer = [videoReaderOutput copyNextSampleBuffer])) {
             [videoWriterInput appendSampleBuffer:sampleBuffer];
             CFRelease(sampleBuffer);
         }
         else
         {
             [videoWriterInput markAsFinished];
             if ([self.assetReader status] == AVAssetReaderStatusCompleted)
             {
                 //start writing from audio reader
                 [audioReader startReading];
                 [self.assetWriter startSessionAtSourceTime:kCMTimeZero];
                 dispatch_queue_t processingQueue = dispatch_queue_create("processingQueue2", NULL);
                 [audioWriterInput requestMediaDataWhenReadyOnQueue:processingQueue usingBlock:^{
                     while (audioWriterInput.readyForMoreMediaData)
                     {
                         CMSampleBufferRef sampleBuffer;
                         if ([audioReader status] == AVAssetReaderStatusReading &&
                             (sampleBuffer = [audioReaderOutput copyNextSampleBuffer]))
                         {
                             if (sampleBuffer) {
                                 [audioWriterInput appendSampleBuffer:sampleBuffer];
                             }
                             CFRelease(sampleBuffer);
                         }
                         else
                         {
                             [audioWriterInput markAsFinished];
                             if ([audioReader status] == AVAssetReaderStatusCompleted) {
                                 [self.assetWriter finishWritingWithCompletionHandler:^(){
                                     [self createLiveTrailerApiForVideoId:video.dbId];
                                 }];
                             }
                         }
                     }

                 }];
             }
         }
     }
 }];

and this is the part that causing the crash

CMSampleBufferRef sampleBuffer;
if ([audioReader status] == AVAssetReaderStatusReading &&
    (sampleBuffer = [audioReaderOutput copyNextSampleBuffer]))
{
    if (sampleBuffer) {
       [audioWriterInput appendSampleBuffer:sampleBuffer];
    }
    CFRelease(sampleBuffer);
}

I have been searching around, seems like I need to set the 'kCMSampleBufferAttachmentKey_TrimDurationAtStart' to first buffer, but can't find any example about how to set this value.

Please advise. Thanks!

Xu Yin
  • 3,932
  • 1
  • 26
  • 46

2 Answers2

2

Just like this:

CFDictionaryRef dict = NULL;
if (firstBuffer) {
   firstBuffer = NO;
   dict = CMTimeCopyAsDictionary(CMTimeMake(1024, 44100), kCFAllocatorDefault);
   CMSetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_TrimDurationAtStart, dict, kCMAttachmentMode_ShouldNotPropagate);
            }
ljsdaya
  • 121
  • 10
0
  • According to this thread on Apple Mailing lists I'd suggest you to check on CMAttachment Reference
  • just for clarity, you should rename your `sampleBuffer variable in the inner while
  • did you try to pass a dictionary containing in kCMSampleBufferAttachmentKey_TrimDurationAtStart when initializing your audioReaderOuput ? (not sure how it should be generated
AnderCover
  • 2,488
  • 3
  • 23
  • 42
  • Hello, AnderCover, I did find that mailing list thread too, but unfortunately, i still couldn't find a good way to solve my problem... – Xu Yin Feb 22 '16 at 09:17
  • i think i do have this AVAssetReaderOurput initialized, if you search AVAssetReaderOutput in the code i pasted, it's a little bit hard to put codes in comments. But i didn't pass anything related to kCMSampleBufferAttachmentKey_TrimDurationAtStart. Do you know can I do it? Thanks, – Xu Yin Feb 24 '16 at 17:05
  • Yes you initialize it but without any outputsettings adding a dictionary with the missing kCMSampleBufferAttachmentKey_TrimDurationAtStart key and its value should do the trick – AnderCover Feb 24 '16 at 17:40
  • my question is how to generate that dictionary.. can you give me some example codes? – Xu Yin Feb 24 '16 at 21:07
  • @XuYin did you ever find a solution to this? I am having the same issue – user3500462 Sep 27 '19 at 23:03