How to get the Start Timecode (SMPTE) of a Quicktime-Movie in Objective-C in 64-bit?

南笙酒味 提交于 2019-11-30 15:57:09

Use AVFoundation framework instead of QuickTime. The player initialisation is well explained in the documentation: https://developer.apple.com/library/mac/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/02_Playback.html#//apple_ref/doc/uid/TP40010188-CH3-SW2

Once your AVAsset is loaded in memory, you can extract the first sample frame number (timeStampFrame) by reading the content of the timecode track if present:

long timeStampFrame = 0;
for (AVAssetTrack * track in [_asset tracks]) {
    if ([[track mediaType] isEqualToString:AVMediaTypeTimecode]) {
        AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:_asset error:nil];
        AVAssetReaderTrackOutput *assetReaderOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:track outputSettings:nil]; 
        if ([assetReader canAddOutput:assetReaderOutput]) {
            [assetReader addOutput:assetReaderOutput];
            if ([assetReader startReading] == YES) {
                int count = 0;

                while ( [assetReader status]==AVAssetReaderStatusReading ) {
                    CMSampleBufferRef sampleBuffer = [assetReaderOutput copyNextSampleBuffer];
                    if (sampleBuffer == NULL) {
                        if ([assetReader status] == AVAssetReaderStatusFailed) 
                            break;
                        else    
                            continue;
                    }
                    count++;

                    CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);
                    size_t length = CMBlockBufferGetDataLength(blockBuffer);

                    if (length>0) {
                        unsigned char *buffer = malloc(length);
                        memset(buffer, 0, length);
                        CMBlockBufferCopyDataBytes(blockBuffer, 0, length, buffer);

                        for (int i=0; i<length; i++) {
                            timeStampFrame = (timeStampFrame << 8) + buffer[i];
                        }

                        free(buffer);
                    }

                    CFRelease(sampleBuffer);
                }

                if (count == 0) {
                    NSLog(@"No sample in the timecode track: %@", [assetReader error]);
                }

                NSLog(@"Processed %d sample", count);

            }

        }

        if ([assetReader status] != AVAssetReaderStatusCompleted)
            [assetReader cancelReading];
    }
}

This is a little more tricky than the QuickTime API and there must be some improvement to the code above but it works for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!