1

I have a trouble while reading the sample buffers from a file using AVAssetreader. The timestamps of the samples read seem to be out of order. Is this the expected result or is there something I am doing wrong. Below is the reading and writing code I am using -

[assetWriterInput requestMediaDataWhenReadyOnQueue:serializationQueue usingBlock:^{
    if (finished)
        return;

    BOOL completedOrFailed = NO;

    // Read samples in a loop as long as the asset writer input is ready
    while ([assetWriterInput isReadyForMoreMediaData] && !completedOrFailed)
    {
        CMSampleBufferRef sampleBuffer = [assetReaderOutput copyNextSampleBuffer];
        CMTime originalTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);

        if (sampleBuffer != NULL)
        {

            CMTime cmm1   =   CMSampleBufferGetOutputDuration(sampleBuffer);

            NSLog(@"Timestamps ==  %f",time);


            CMSampleBufferRef newSampleBuffer;
            CMSampleTimingInfo sampleTimingInfo;

            sampleTimingInfo.duration = cmm1;
            float milliseconds = videoPTM * 600;
            sampleTimingInfo.presentationTimeStamp = CMTimeMake((int)milliseconds, 600);
            sampleTimingInfo.decodeTimeStamp = kCMTimeInvalid;

            CMSampleBufferCreateCopyWithNewTiming(kCFAllocatorDefault,
                                                  sampleBuffer,
                                                  1,
                                                  &sampleTimingInfo,
                                                  &newSampleBuffer);

            BOOL success = YES;
            success  = [assetWriterInput appendSampleBuffer:newSampleBuffer];

            CFRelease(newSampleBuffer);
            newSampleBuffer = NULL;

            CFRelease(sampleBuffer);
            sampleBuffer = NULL;

            completedOrFailed = !success;

            videoPTM+=CMTimeGetSeconds(cmm1);

        }
        else
        {
            completedOrFailed = YES;
        }
    }

    if (completedOrFailed)
        [self callCompletionHandlerIfNecessary];

When I print the timestamps of the samples read they are not in order like this...

            Video Timestamps ==  6.971667
            Video Timestamps ==  7.055000
            Video Timestamps ==  7.220000
            Video Timestamps ==  7.136667
            Video Timestamps ==  7.386667
            Video Timestamps ==  7.303333
            Video Timestamps ==  7.470000
            Video Timestamps ==  7.635000

Please let me know the reason of the same and how I can create a copy of the samples with a different presentation time.

borncrazy
  • 1,589
  • 1
  • 12
  • 9

0 Answers0