【问题标题】:Reading samples via AVAssetReader通过 AVAssetReader 读取样本
【发布时间】:2010-10-29 05:23:03
【问题描述】:

您如何通过 AVAssetReader 读取样本?我发现了使用 AVAssetReader 进行复制或混合的示例,但这些循环始终由 AVAssetWriter 循环控制。是否可以只创建一个 AVAssetReader 并通读它,获取每个样本?

谢谢。

【问题讨论】:

    标签: objective-c iphone cocoa-touch avfoundation avassetreader


    【解决方案1】:

    您的问题不清楚您是在谈论视频样本还是音频样本。要阅读视频样本,您需要执行以下操作:

    1. 构造一个 AVAssetReader:

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

    2. 从您的资产中获取视频轨道:

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

    3. 设置所需的视频帧格式:

      NSMutableDictionary* 字典 = [[NSMutableDictionary alloc] init];
      [字典 setObject:[NSNumber numberWithInt:] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey];

      请注意,某些视频格式无法正常工作,如果您正在执行实时操作,某些视频格式的性能会比其他格式更好(例如,BGRA 比 ARGB 更快)。

    4. 构造实际的轨道输出并将其添加到资产阅读器:

      AVAssetReaderTrackOutput*asset_reader_output = [[AVAssetReaderTrackOutput alloc] initWithTrack:video_track outputSettings:dictionary];
      [asset_reader addOutput:asset_reader_output];
    5. 启动资产阅读器:

      [asset_reader startReading];

    6. 读出样本:

      CMSampleBufferRef 缓冲区; while ( [asset_reader status]==AVAssetReaderStatusReading ) 缓冲区 = [asset_reader_output copyNextSampleBuffer];

    【讨论】:

    • 谢谢达米安。我实际上是在问音频。我可以将音频放入缓冲区,但不知道如何获取缓冲区中的样本。
    • 我有视频样本阅读工作,但我经常(或所有时间,对于某些视频)最终得到空白视频帧。没有错误,只是空白帧。有什么想法吗?
    • 不确定,抱歉。也许检查样品上的演示时间戳?你能想出一种方法来始终如一地制作一直空白的视频吗?也许这会有所帮助?
    • @damian 无论如何我可以从“缓冲区”获取帧捕获时间戳
    • @Mr.G 是的,试试 CMSampleBufferGetPresentationTimeStamp()。此处的文档:developer.apple.com/library/ios/documentation/CoreMedia/…
    【解决方案2】:

    damian 对视频的回答对我有用,还有一个小修改:在第 3 步中,我认为字典需要是可变的:

    NSMutableDictionary* dictionary = [[NSMutableDictionary alloc] init];
    

    【讨论】:

    • 无需使其可变。只需使用现代 Objective-C 的:@{(NSString*)kCVPixelBufferPixelFormatTypeKey : @(<format code from CVPixelBuffer.h>)} 或经典的 [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:<format code from CVPixelBuffer.h>] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey]。在初始化时传递键/对象不需要可变性。
    • 感谢 Regexidnet 的澄清。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    相关资源
    最近更新 更多