Main problems with AVAssetReader when reading video samples

I am looking to read video samples through AVAssetReader, and I come across all kinds of road blocks. Is there any single zip code that installs AVAsset from a video named "Movie.m4v" from the application package and uses AVAssetReader to cycle through video frames (CMSampleBufferRef).

Here are some of the code I'm using now that does not 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"); } 
+6
source share
2 answers

Most likely, it crashes due to [NSURL URLWithString:path] . Instead, use [NSURL fileURLWithPath:path] .

+10
source

you must ask your AVAsset to load the tracks property asynchronously before you can use the tracks.

0
source

Source: https://habr.com/ru/post/890981/


All Articles