从AVAssetReader读取的样本不按顺序排列

时间:2014-03-21 09:31:14

标签: timestamp avassetwriter avassetreader

使用AVAssetreader从文件中读取样本缓冲区时遇到问题。读取的样本的时间戳似乎不正常。这是预期的结果还是我做错了什么。下面是我正在使用的读写代码 -

[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];

当我打印样本的时间戳时,它们不按照这样的顺序......

            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

请让我知道相同的原因,以及如何创建具有不同演示时间的样本副本。

0 个答案:

没有答案