开发者

Basic AVAssetReader problems reading video samples

开发者 https://www.devze.com 2023-03-14 12:44 出处:网络
I\'m looking to read video samples via AVAssetReader and I\'m running into all kinds of road blocks. Can some one post 开发者_StackOverflowcode that sets up an AVAsset from a video named \'Movie.m4v\'

I'm looking to read video samples via AVAssetReader and I'm running into all kinds of road blocks. Can some one post 开发者_StackOverflowcode that sets up an AVAsset from a video named 'Movie.m4v' from the application bundle and uses AVAssetReader to loop through the video frames (CMSampleBufferRef).

Here's some of the code I'm using now that doesn't work:

NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Movie.m4v"];
AVAsset *avAsset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:path] options:nil];
NSError *error = nil;
AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:avAsset error:&error]; // crashing right around here!

// I've gotten avassetreader to not crash upon initialization by grabbing an asset from the ALAssetsLibrary but it says it has 0 tracks!

NSArray *videoTracks = [avAsset tracksWithMediaType:AVMediaTypeVideo]; 
AVAssetTrack *videoTrack = [videoTracks objectAtIndex:0];
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
AVAssetReaderTrackOutput *asset_reader_output = [[AVAssetReaderTrackOutput alloc] initWithTrack:videoTrack outputSettings:options];
[reader addOutput:asset_reader_output];
[reader startReading];

CMSampleBufferRef buffer;
while ( [reader status]==AVAssetReaderStatusReading ){

    buffer = [asset_reader_output copyNextSampleBuffer];
    NSLog(@"READING");



}


Most likely crashes because of the [NSURL URLWithString:path]. You should use [NSURL fileURLWithPath:path] instead.


you need to ask your AVAsset to asynchronously load its tracks property before you may use the tracks.

0

精彩评论

暂无评论...
验证码 换一张
取 消