13

How do you read samples via AVAssetReader? I've found examples of duplicating or mixing using AVAssetReader, but those loops are always controlled by the AVAssetWriter loop. Is it possible just to create an AVAssetReader and read through it, getting each sample?

Thanks.

Maxim Pavlov
  • 2,962
  • 1
  • 23
  • 33
Eric Christensen
  • 819
  • 3
  • 9
  • 21

2 Answers2

27

It's unclear from your question whether you are talking about video samples or audio samples. To read video samples, you need to do the following:

  1. Construct an AVAssetReader:

    asset_reader = [[AVAssetReader alloc] initWithAsset:asset error:&error]; (error checking goes here)

  2. Get the video track(s) from your asset:

    NSArray* video_tracks = [asset tracksWithMediaType:AVMediaTypeVideo]; AVAssetTrack* video_track = [video_tracks objectAtIndex:0];

  3. Set the desired video frame format:

    NSMutableDictionary* dictionary = [[NSMutableDictionary alloc] init];
    [dictionary setObject:[NSNumber numberWithInt:<format code from CVPixelBuffer.h>] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey];

    Note that certain video formats just will not work, and if you're doing something real-time, certain video formats perform better than others (BGRA is faster than ARGB, for instance).

  4. Construct the actual track output and add it to the asset reader:

    AVAssetReaderTrackOutput* asset_reader_output = [[AVAssetReaderTrackOutput alloc] initWithTrack:video_track outputSettings:dictionary];
    [asset_reader addOutput:asset_reader_output];
  5. Kick off the asset reader:

    [asset_reader startReading];

  6. Read off the samples:

    CMSampleBufferRef buffer;
    while ( [asset_reader status]==AVAssetReaderStatusReading )
          buffer = [asset_reader_output copyNextSampleBuffer];
    
damian
  • 3,604
  • 1
  • 27
  • 46
  • Thanks damian. I was actually asking about audio. I can get the audio into the buffer, but don't know how to get at the samples within the buffer. – Eric Christensen Feb 11 '11 at 18:23
  • I have video sample reading working, but I end up with blank video frames every so often (or all the time, for some videos). No errors, just blank frames. Any ideas? – Dave Branton Jan 25 '14 at 00:45
  • Not sure, sorry. Maybe check the Presentation Time Stamp on the samples? Can you figure out a way to consistently create videos that are blank all the time - maybe that will help? – damian Jan 27 '14 at 15:22
  • @damian is there anyway that i can get frame captured time stamp from "buffer" – Mr.G Mar 27 '15 at 09:13
  • @Mr.G yes, try CMSampleBufferGetPresentationTimeStamp(). Docs here: https://developer.apple.com/library/ios/documentation/CoreMedia/Reference/CMSampleBuffer/index.html#//apple_ref/c/func/CMSampleBufferGetPresentationTimeStamp – damian Jul 13 '15 at 08:14
2

damian's answer worked for me with video, and one minor modification: In step 3, I think the dictionary needs to be mutable:

NSMutableDictionary* dictionary = [[NSMutableDictionary alloc] init];
Matt Wallis
  • 873
  • 5
  • 25
  • 2
    No need to make it mutable. Just use either modern Objective-C's: `@{(NSString*)kCVPixelBufferPixelFormatTypeKey : @()}` or the classical `[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey]`. Passing the key/object on initialization requires no mutability. – Regexident Sep 22 '12 at 23:27
  • Thanks for that clarification Regexidnet. – Matt Wallis Nov 21 '12 at 23:28