1

I am working on an application that plays back video and allows the user to scrub forwards and backwards in the video. The scrubbing has to happen smoothly, so we always re-write the video with SDAVAssetExportSession with the video compression property AVVideoMaxKeyFrameIntervalKey:@1 so that each frame will be a keyframe and allow smooth reverse scrubbing. This works great and provides smooth playback. The application uses video from a variety of sources and can be recorded on android or iOS devices and even downloaded from the web and added to the application, so we end up with quite different encodings, some of which are already suited for scrubbing (each frame is a keyframe). Is there a way to detect the keyframe interval of a video file so I can avoid needless video processing? I have been through much of AVFoundation's docs and don't see an obvious way to get this information. Thanks for any help on this.

3 Answers 3

2

If you can quickly parse the file without decoding the images by creating an AVAssetReaderTrackOutput with nil outputSettings. The frame sample buffers you encounter have an attachment array containing a dictionary with useful information, include whether the frame depends on other frames, or whether other frames depend on it. I would interpret that former as indicating a keyframe, although it gives me some low number (4% keyframes in one file?). Anyway, the code:

let asset = AVAsset(url: inputUrl)
let reader = try! AVAssetReader(asset: asset)

let videoTrack = asset.tracks(withMediaType: AVMediaTypeVideo)[0]
let trackReaderOutput = AVAssetReaderTrackOutput(track: videoTrack, outputSettings: nil)

reader.add(trackReaderOutput)
reader.startReading()

var numFrames = 0
var keyFrames = 0

while true {
    if let sampleBuffer = trackReaderOutput.copyNextSampleBuffer() {
        // NB: not every sample buffer corresponds to a frame!
        if CMSampleBufferGetNumSamples(sampleBuffer) > 0 {
            numFrames += 1
            if let attachmentArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, false) as? NSArray {
                let attachment = attachmentArray[0] as! NSDictionary
                // print("attach on frame \(frame): \(attachment)")
                if let depends = attachment[kCMSampleAttachmentKey_DependsOnOthers] as? NSNumber {
                    if !depends.boolValue {
                        keyFrames += 1
                    }
                }
            }
        }
    } else {
        break
    }
}

print("\(keyFrames) on \(numFrames)")

N.B. This only works for local file assets.

p.s. you don't say how you're scrubbing or playing. An AVPlayerViewController and an AVPlayer?

5
  • Thanks for the answer. I implemented this today (objective c though). I am using a custom UIView with an AVPlayer and AVPlayerLayer. This seems to be fairly accurate and returns about 96% keyframes in videos that I encoded to use all keyframes. Much less for videos with other compression settings. The scrubber is a wheel (scrollView) that translates the scrollspeed into a scrub loop that steps one frame at a time. I may post my method as an answer to a Q and I'll link if I do. I will also post the Objective c version of this.
    – johnrechd
    Apr 14, 2017 at 0:13
  • Cool! I look forward to seeing it. That's odd about the missing 4%. I wonder what the explanation could be. Apr 14, 2017 at 0:48
  • Unfortunately, it doesn't always work. In the Apple's documentation it says: kCMSampleAttachmentKey_DependsOnOthers "This key has no default value. If this key is not present, dependency information for the sample is unknown.". I have a couple of videos which shows 0 keyframes using that key, but when using ffprobe it shows valid key-frames.
    – bojan
    Aug 16, 2020 at 14:33
  • Can you share an example file? Aug 16, 2020 at 15:28
  • I think kCMSampleAttachmentKey_NotSync = false would be a more reliable indicator. Jun 19, 2022 at 8:19
1

Here is the Objective C version of the same answer. After implementing this and using it, Videos that should have all keyframes are returning about 96% keyframes from this code. I'm not sure why, so I am using that number as a determining factor even though I would like it to be more accurate. I am also only looking through the first 600 frames or the end of the video (whichever comes first) since I don't need to read through a whole 20 minute video to make this determination.

+ (BOOL)videoNeedsProcessingForSlomo:(NSURL*)fileUrl {
    BOOL needsProcessing = YES;
    AVAsset* anAsset = [AVAsset assetWithURL:fileUrl];
    NSError *error;
    AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:anAsset error:&error];
    if (error) {
        DLog(@"Error:%@", error.localizedDescription);
        return YES;
    }

    AVAssetTrack *videoTrack = [[anAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

    AVAssetReaderTrackOutput *trackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:videoTrack outputSettings:nil];
    [assetReader addOutput:trackOutput];

    [assetReader startReading];

    float numFrames = 0;
    float keyFrames = 0;

    while (numFrames < 600) { // If the video is long - only parse through 20 seconds worth.
        CMSampleBufferRef sampleBuffer = [trackOutput copyNextSampleBuffer];
        if (sampleBuffer) {
            // NB: not every sample buffer corresponds to a frame!
            if (CMSampleBufferGetNumSamples(sampleBuffer) > 0) {
                numFrames += 1;

                NSArray *attachmentArray = ((NSArray*)CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, false));
                if (attachmentArray) {
                    NSDictionary *attachment = attachmentArray[0];
                    NSNumber *depends = attachment[(__bridge NSNumber*)kCMSampleAttachmentKey_DependsOnOthers];
                    if (depends) {
                        if (depends.boolValue) {
                            keyFrames += 1;
                        }
                    }
                }
            }
        }
        else {
            break;
        }
    }

    needsProcessing = keyFrames / numFrames < 0.95f; // If more than 95% of the frames are keyframes - don't decompress.

    return needsProcessing;
}
2
  • sorry - I didn't know you were using the objective c, the question wasn't tagged Apr 14, 2017 at 23:28
  • No worries, didn't matter much on the language for me. Thanks for your help on this. I had something similar and had been reading through tons of documentation but didn't know the kCMSampleAttachmentKey_DependsOnOthers could be used to determine this.
    – johnrechd
    Apr 15, 2017 at 0:01
0

Using kCMSampleAttachmentKey_DependsOnOthers was giving me 0 key frames in some cases, when ffprobe would return key frames. To get the same number of key frames as ffprobe shows, I used:

if attachment[CMSampleBuffer.PerSampleAttachmentsDictionary.Key.notSync] == nil {
                            keyFrames += 1
                        }

In the CoreMedia header it says:

/// Boolean (absence of this key implies Sync)
public static let notSync: CMSampleBuffer.PerSampleAttachmentsDictionary.Key

for dependsOnOthers key it says:

/// `true` (e.g., non-I-frame), `false` (e.g. I-frame), or absent if
/// unknown
public static let dependsOnOthers: CMSampleBuffer.PerSampleAttachmentsDictionary.Key

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.