通过 AVAssetReader 读取示例

发布于 2024-09-29 11:51:25 字数 137 浏览 4 评论 0原文

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

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

む无字情书 2024-10-06 11:51:25

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

  1. 构建 AVAssetReader:

    asset_reader = [[AVAssetReader alloc] initWithAsset:资产错误:&错误];
    (错误检查在此处)

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

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

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

    NSMutableDictionary* 字典 = [[NSMutableDictionary alloc] init];
    [dictionary 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 )
          buffer = [asset_reader_output copyNextSampleBuffer];
    

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];
    
北城孤痞 2024-10-06 11:51:25

达米安的答案对我来说适用于视频,并进行了一个小的修改:在步骤 3 中,我认为字典需要是可变的:

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文