AVFoundation编程指南1-使用 Assets

创建assert对象 为了创建一个由URL标识的代表任何资源的assert对象,可以使用AVURLAssert,最简单的是从文件里创建一个assert对象:

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

·Asset 初始化的可选操作

AVURLAsset初始化方法的第二个参数使用一个dictionary,这个dictionary里的唯一一个key是 AVURLAssetPreferPreciseDurationAndTimingKey,它的value是一个boolean类型(用NSValue包装的对象),这个值表示asset是否提供一个精确的duration。 获取asset精确的duration需要很多处理时间,使用一个预估的duration效率比较高并且对播放来说足够。因此: 如果你想要播放asset,初始化方法传nil就行了,而不是一个dictionry,或者传一个以AVURLAssetPreferPreciseDurationAndTimingKeydictionary为key,值为NO的一个dictionary。 · 如果你想把asset加到一个composition中,你需要一个精确的访问权限,这时你可以传一个dictionary,这个dictionary的一组键值对为

AVURLAssetPreferPreciseDurationAndTimingKey和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 library 和相册里asset,你需要获取asset的URL。 · 为了访问ipod Library,需要创建MPMediaQuery对象来找到你想要对象,然后通过MPMediaItemPropertyAssetURL获取它的URL。要想获取更多关于Media Library,查看Multimedia Programming Guide. · 为了访问相册,可以使用ALAssetsLibrary. 下面的例子用来获取相册里面的第一个视频: 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(或者track)并不是表示asset里面所有的信息都是马上可用的。它需要一些时间去计算,即使是durtation(比如没有摘要信息的mp3文件),你应该使用AVAsynchronousKeyValueLoading协议获取这些值,通过- loadValuesAsynchronouslyForKeys:completionHandler:在handler里面获取你要的值。 你可以用statusOfValueForKey:error:测试一个属性的value是否成功获取,当一个assert第一次被加载,大多数属性的值是AVKeyValueStatusUnknown状态,为了获取一个或多个属性的值,你要调用loadValuesAsynchronouslyForKeys:completionHandler:,在comletiton handler里面,你可以根据属性的状态做任何合适的处理。你要处理加载没有成功的情况,可能因为一些原因比如网络不可连接,又或者loading被取消。

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属性。

·从video中获取静态图片

从asset中获取静态图片(比如说缩略图),你可以用AVAssetImageGenerator对象。可以用asset初始化一个AVAssetImageGenerator对象.即使asset在初始化的时候没有可见的track也能成功,所以你应该检测asset是否有track,使用tracksWithMediaCharacteristic:

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

你还可以设置imagegenerator的其他属性,比如,你可以指定生成图片的最大的分辨率,你可以生成指定时间的一张图片,或者一系列图片。你必须一直持有generator的引用,直到生成所有的图片。

·生成单个图片

你可以使用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提供一个result的参数告诉你图片是否成功生成或者图片生成操作是否取消。

在你的block实现中,需要检查result,判断image是否成功生成,另外,确保你持有image generator直到生成图片的操作结束。

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");
                }
  }];

你也可以取消生成图片的操作,通过想generator发送cancelAllCGImageGeneration的消息。

·裁剪视频和对视频转码

你可以对视频进行转码、裁剪,通过使用AVAssetExportSession对象。这个流程如下图所示,

image.png

一个export session是一个控制对象,可以异步的生成一个asset。可以用你需要生成的asset和presetName来初始化一个session,presetName指明你要生成的asset的属性。接下来你可以配置export session,比如可以指定输出的URL和文件类型,以及其他的设置,比如metadata等等。 你可以先检测设置的preset是否可用,通过使用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.
}

你可以配置session的输出的url(这个url必须是文件url),AVAssetExportSession可以推断通过url的扩展名出输出文件的类型。当然,你可以直接设置文件类型,使用outputFileType。你还可以指定其他属性,比如time range,输出文件的长度等等,下面是列子:

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;

生成一个新的asset,可以调用exportAsynchronouslyWithCompletionHandler:,当生成操作结束后会回调block,在这个block中你需要通过检查session的status来判断是否成功,如下:

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

你可以取消这个生成操作,通过给session发送 cancelExport 消息。 如果导出的文件存在,或者导出的url在沙盒之外,这个导出操作会失败。还有两种情况也可能导致失败: · 来了一个电话 · 你的程序在后台运行并且其他的应用开始播放。 这种情况下,你应该通知用户export失败,并且重新export。

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

推荐阅读更多精彩内容

  • 原文:AVFoundation Programming Guide 写在前面 简单翻译一下AVFoundation...
    朦胧1919阅读 964评论 0 1
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,099评论 18 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,293评论 18 399
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,014评论 11 349
  • 22年12月更新:个人网站关停,如果仍旧对旧教程有兴趣参考 Github 的markdown内容[https://...
    tangyefei阅读 35,127评论 22 257