AVFoundation Programming Guide---Using Assets

Assets可以来自用户的iPod库或照片库中的文件或媒体。当你创建一个 Assets 对象的时,可能想要检索的该项目的所有信息都不会立即可用。 一旦你有一个视频 asset,你可以从中提取静止图像,转码为另一种格式,或修剪内容。

创建一个 Asset 对象

要创建asset来表示可以使用URL标识的任何资源,请使用 AVURLAsset。最简单的情况是从一个文件创建一个资产:

NSURL *url = <#A URL that identifies an audiovisual asset such as a movie file#>;
AVURLAsset *anAsset = [[AVURLAsset alloc] initWithURL:url options:nil];

初始化 Asset 的选项

AVURLAsset初始化方法的第二个参数是一个可选字典。在这个字典中为一个的key是AVURLAssetPreferPreciseDurationAndTimingKey。对应的值是一个布尔值(包含在 NSValue 对象中)指明 asset 是否应该准备好指示精确的持续时间,并提供准确的随机访问时间。
获取 asset 的确切持续时间可能需要相当大的处理开销。使用大致的持续时间通常是更低廉的操作并且足够播放。因此:

  • 如果你只打算播放 asset, 传递nil代替字典,或者传递包含 key AVURLAssetPreferPreciseDurationAndTimingKey以及对应的 value 为NO。
  • 如果你想要添加 asset 到一个组合中(AVMutableComposition),你通常需要精确的随机访问。传递一个字典包含 key 为 AVURLAssetPreferPreciseDurationAndTimingKey 以及对应的 value 值为YES :
NSURL *url = <#A URL that identifies an audiovisual asset such as a movie file#>;
NSDictionary *options = @{ AVURLAssetPreferPreciseDurationAndTimingKey : @YES };
AVURLAsset *anAssetToUseInAComposition = [[AVURLAsset alloc] initWithURL:url options:options];

访问用户的 asset

要访问由iPod库或Photos应用程序管理的 asset,您需要获取所需资产的URL。

  • 为了访问iPod库,你要创建一个 MPMediaQuery 实例对象去寻找你想要的条目,然后使用 MPMediaItemPropertyAssetURL 获取它的URL
    了解更多 Media Library 相关信息,查看Multimedia Programming Guide.

  • 为了获取由 Photos app管理的 asset,可以使用 ALAssetsLibrary

以下示例展示了如果获得“已保存的照片相册”中的第一个视频的 asset :

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
 
// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
 
// Within the group enumeration block, filter to enumerate just videos.
[group setAssetsFilter:[ALAssetsFilter allVideos]];
 
// For this example, we're only interested in the first item.
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:0]
                        options:0
                     usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
 
                         // The end of the enumeration is signaled by asset == nil.
                         if (alAsset) {
                             ALAssetRepresentation *representation = [alAsset defaultRepresentation];
                             NSURL *url = [representation url];
                             AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
                             // Do something interesting with the AV asset.
                         }
                     }];
                 }
                 failureBlock: ^(NSError *error) {
                     // Typically you should handle an error more gracefully than this.
                     NSLog(@"No groups");
                 }];

准备一个 asset 以供使用

初始化一个 asset (或者轨道)并不一定意味着可以立即获得您可能希望检索的该项目的所有信息。计算一个项目的持续时间可能需要一些时间(例如,MP3文件可能不包含摘要信息)。当计算一个值的时候不要阻塞当前线程,你应该使用 AVAsynchronousKeyValueLoading 协议来请求值,并稍后通过您使用块定义的完成处理程序返回答案。(AVAsset 和 AVAssetTrack 遵循
AVAsynchronousKeyValueLoading 协议)
你可以通过方法 statusOfValueForKey:error: 测试属性的值是否加载好了。 当一个 asset 第一次加载的,大多数的属性的值为 AVKeyValueStatusUnknown。 为了加载其中一个或者多个属性值,调用 loadValuesAsynchronouslyForKeys:completionHandler: 。在完成处理程序中,根据属性的状态采取适当的操作。你应该始终准备好加载未成功完成,要么因为某些原因(例如基于网络的URL无法访问),要么因为加载被取消而失败。

NSURL *url = <#A URL that identifies an audiovisual asset such as a movie file#>;
AVURLAsset *anAsset = [[AVURLAsset alloc] initWithURL:url options:nil];
NSArray *keys = @[@"duration"];
 
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^() {
 
    NSError *error = nil;
    AVKeyValueStatus tracksStatus = [asset statusOfValueForKey:@"duration" error:&error];
    switch (tracksStatus) {
        case AVKeyValueStatusLoaded:
            [self updateUserInterfaceForDuration];
            break;
        case AVKeyValueStatusFailed:
            [self reportError:error forAsset:asset];
            break;
        case AVKeyValueStatusCancelled:
            // Do whatever is appropriate for cancelation.
            break;
   }
}];

如果您要准备 asset 进行播放,则应加载其 tracks 属性。有关播放资产的更多信息,请参阅播放 Playback

从视频中获取图片

从 asset 获取视频播放缩略图,要使用 AVAssetImageGenerator 对象。使用 asset 初始化一个图片生成器。 初始化可能成功可能失败,例如 asset 在初始化的时候没有视觉轨道,因此使用
tracksWithMediaCharacteristic: 测试 asset 是否有视觉轨道是很有必要的

AVAsset anAsset = <#Get an asset#>;
if ([[anAsset tracksWithMediaType:AVMediaTypeVideo] count] > 0) {
    AVAssetImageGenerator *imageGenerator =
        [AVAssetImageGenerator assetImageGeneratorWithAsset:anAsset];
    // Implementation continues...
}

可以配置图像生成器的几个方面,例如,您可以分别指定其生成的图像的最大尺寸和使用maximumSize和apertureMode的光圈模式。然后,您可以在给定的时间或一系列的图片。您必须确保您保留对图像生成器的强烈参考,直到生成所有图像。

生成单张图片

可以使用 copyCGImageAtTime:actualTime:error: 在一个指定的时间点生成单张图片。AVFoundation可能无法在您请求的时间产生图像,因此您可以将第二个参数作为指向CMTime的指针,返回时包含图像实际生成的时间。

AVAsset *myAsset = <#An asset#>];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:myAsset];
 
Float64 durationSeconds = CMTimeGetSeconds([myAsset duration]);
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
NSError *error;
CMTime actualTime;
 
CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error];
 
if (halfWayImage != NULL) {
 
    NSString *actualTimeString = (NSString *)CMTimeCopyDescription(NULL, actualTime);
    NSString *requestedTimeString = (NSString *)CMTimeCopyDescription(NULL, midpoint);
    NSLog(@"Got halfWayImage: Asked for %@, got %@", requestedTimeString, actualTimeString);
 
    // Do something interesting with the image.
    CGImageRelease(halfWayImage);
}

生成一些列图片

为了生成一系列图片,要向图片生成器发送 generateCGImagesAsynchronouslyForTimes:completionHandler: 消息。第一个参数是包含了 NSValue 对象的数组,每个包含一个 CMTime 结构体,指明你想生成图片的时间点。第二个参数是一个 block ,用作为每个生成的图像调用的回调函数。block 的参数提供了一个结果常量,告诉您图像是否已成功创建,或者操作是否已被取消,并且视情况而定:

  • 图片
  • 请求图片的时间以及图片生成的实际时间
  • 描述原因生成失败的错误对象

在你的 block 实现中,检查结果常量以确定图像是否被创建。此外,请确保您保留对图像生成器的强烈参考,直到完成图像创建。

AVAsset *myAsset = <#An asset#>];
// Assume: @property (strong) AVAssetImageGenerator *imageGenerator;
self.imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:myAsset];
 
Float64 durationSeconds = CMTimeGetSeconds([myAsset duration]);
CMTime firstThird = CMTimeMakeWithSeconds(durationSeconds/3.0, 600);
CMTime secondThird = CMTimeMakeWithSeconds(durationSeconds*2.0/3.0, 600);
CMTime end = CMTimeMakeWithSeconds(durationSeconds, 600);
NSArray *times = @[NSValue valueWithCMTime:kCMTimeZero],
                  [NSValue valueWithCMTime:firstThird], [NSValue valueWithCMTime:secondThird],
                  [NSValue valueWithCMTime:end]];
 
[imageGenerator generateCGImagesAsynchronouslyForTimes:times
                completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime,
                                    AVAssetImageGeneratorResult result, NSError *error) {
 
                NSString *requestedTimeString = (NSString *)
                    CFBridgingRelease(CMTimeCopyDescription(NULL, requestedTime));
                NSString *actualTimeString = (NSString *)
                    CFBridgingRelease(CMTimeCopyDescription(NULL, actualTime));
                NSLog(@"Requested: %@; actual %@", requestedTimeString, actualTimeString);
 
                if (result == AVAssetImageGeneratorSucceeded) {
                    // Do something interesting with the image.
                }
 
                if (result == AVAssetImageGeneratorFailed) {
                    NSLog(@"Failed with error: %@", [error localizedDescription]);
                }
                if (result == AVAssetImageGeneratorCancelled) {
                    NSLog(@"Canceled");
                }
  }];

您可以通过发送图像生成器 cancelAllCGImageGeneration 消息来取消生成图像序列。

修剪和转码电影

你可以使用 AVAssetExportSession 对象将视频转码为另一个格式,或者修剪视频。工作流程如图1-1所示。一个 export session 对象是管理异步导出 asset 的控制对象。 使用要导出的 asset 初始化 session,并指定要应用导出选项的导出预设的名称(请参阅allExportPresets)。然后配置导出 session 到一个指定的输出 URL 和文件类型,以及可选的其他设置(如元数据)以及输出是否应针对网络使用进行优化。

  • Figure 1-1 The export session workflow


    image.png

您可以检查是否可以使用 exportPresetsCompatibleWithAsset: 使用给定的预设导出给定资产:如下例所示:

AVAsset *anAsset = <#Get an asset#>;
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:anAsset];
if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality]) {
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
        initWithAsset:anAsset presetName:AVAssetExportPresetLowQuality];
    // Implementation continues.
}

通过提供输出URL(URL必须是文件URL)来完成会话的配置。AVAssetExportSession可以从URL的路径扩展中推断输出文件类型;通常使用 outputFileType 直接设置它。您还可以指定其他属性,例如时间范围,输出文件长度限制,导出的文件是否应针对网络使用进行优化以及视频合成。以下示例说明如何使用 timeRange 属性修剪影片:

    exportSession.outputURL = <#A file URL#>;
    exportSession.outputFileType = AVFileTypeQuickTimeMovie;
 
    CMTime start = CMTimeMakeWithSeconds(1.0, 600);
    CMTime duration = CMTimeMakeWithSeconds(3.0, 600);
    CMTimeRange range = CMTimeRangeMake(start, duration);
    exportSession.timeRange = range;

要创建新文件,请调用 exportAsynchronouslyWithCompletionHandler:。完成处理 block 会在导出操作完成后被调用。在你的实现中,你应该检查session’s的 status 的值以确定导出成功,失败还是被取消了。

[exportSession exportAsynchronouslyWithCompletionHandler:^{
 
        switch ([exportSession status]) {
            case AVAssetExportSessionStatusFailed:
                NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
                break;
            case AVAssetExportSessionStatusCancelled:
                NSLog(@"Export canceled");
                break;
            default:
                break;
        }
    }];

你也可以发送 cancelExport 消息取消导出。
如果试图重写已存在的文件或者往沙盒外边的文件里导出会导致失败。也可能会因为这些原因失败:

  • 有一个来电
  • 您的应用程序在后台,另一个应用程序开始播放
    在这些情况下,通常应该通知用户导出失败,然后允许用户重新启动导出
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,710评论 4 376
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,839评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,295评论 0 255
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,776评论 0 223
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,198评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,074评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,200评论 2 322
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,986评论 0 214
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,733评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,877评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,348评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,675评论 3 265
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,393评论 3 246
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,209评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,996评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,212评论 2 287
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 36,003评论 2 280

推荐阅读更多精彩内容