零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> Object-C 基础
零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> Object-C 线程
零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> OpenGL ES
零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> GPUImage
零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> AVFoundation
零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> CocoaPods
一.前言
1.AVAsset
Assets 可以来自一个文件或用户的相册,可以理解为多媒体资源,通过 URL 作为一个 asset 对象的标识. 这个 URL 可以是本地文件路径或网络流;
2.AVAssetTrack
AVAsset 包含很多轨道 AVAssetTrack 的结合,如 audio, video, text, closed captions, subtitles…
3.AVComposition / AVMutableComposition
使用 AVMutableComposition 类可以增删 AVAsset 来将单个或者多个 AVAsset 集合到一起,用来合成新视频。除此之外,若想将集合到一起的视听资源以自定义的方式进行播放,需要使用 AVMutableAudioMix 和 AVMutableVideoComposition 类对其中的资源进行协调管理;
4.AVMutableVideoComposition
AVFoundation 类 API 中最核心的类是 AVVideoComposition / AVMutableVideoComposition 。
AVVideoComposition / AVMutableVideoComposition 对两个或多个视频轨道组合在一起的方法给出了一个总体描述。它由一组时间范围和描述组合行为的介绍内容组成。这些信息出现在组合资源内的任意时间点。
AVVideoComposition / AVMutableVideoComposition 管理所有视频轨道,可以决定最终视频的尺寸,裁剪需要在这里进行;
5.AVMutableCompositionTrack
将多个 AVAsset 集合到一起合成新视频中轨道信息,有音频轨、视频轨等,里面可以插入各种对应的素材(画中画,水印等);
6.AVMutableVideoCompositionLayerInstruction
AVMutableVideoCompositionLayerInstruction 主要用于对视频轨道中的一个视频处理缩放、模糊、裁剪、旋转等;
7.AVMutableVideoCompositionInstruction
表示一个指令,决定一个 timeRange 内每个轨道的状态,每一个指令包含多个 AVMutableVideoCompositionLayerInstruction ;而 AVVideoComposition 由多个 AVVideoCompositionInstruction 构成;
AVVideoCompositionInstruction 所提供的最关键的一段数据是组合对象时间轴内的时间范围信息。这一时间范围是在某一组合形式出现时的时间范围。要执行的组全特质是通过其 AVMutableVideoCompositionLayerInstruction 集合定义的。
8.AVAssetExportSession
AVAssetExportSession 主要用于导出视频;
二.AVMetadataItem简介
AVAsset 和 AVAssetTrack 都可以实现查询相关元数据的功能.一般使用 AVAsset 提供的元数据当涉及获取曲目一级元数据等情况时会使用 AVAssetTrack ,使用 AVAsset 获取元数据相关如下:
/* 包含着当前视频常见格式类型的元数据 */
@property (nonatomic, readonly) NSArray<AVMetadataItem *> *commonMetadata;
// 包含当前视频所有格式类型的元数据.
@property (nonatomic, readonly) NSArray<AVMetadataItem *> *metadata API_AVAILABLE(macos(10.10), ios(8.0), tvos(9.0), watchos(1.0));
// 包含当前视频所有可用元数据的格式类型元数据的格式类型在AVMetadataFormat中定义了很多种,常见的有title、creator、subject、publisher等.
@property (nonatomic, readonly) NSArray<AVMetadataFormat> *availableMetadataFormats;
AVMetadataFormat 如下:
// Metadata common keys
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyTitle ;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyCreator;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeySubject;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyDescription;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyPublisher;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyContributor;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyCreationDate;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyLastModifiedDate;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyType;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyFormat;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyIdentifier;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeySource;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyLanguage;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyRelation;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyLocation;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyCopyrights;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyAlbumName;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyAuthor;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyArtist;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyArtwork;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyMake;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyModel ;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeySoftware ;
AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyAccessibilityDescription;
三.使用availableMetadataFormats 获取元数据
/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - AVMetadataItem 获取媒体属性元数据
//@Time:2021/07/29 07:30
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/
NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"456.mp3" ofType:nil]];
AVURLAsset* asset = [[AVURLAsset alloc] initWithURL:url options:nil];
for (AVMetadataFormat item in [asset availableMetadataFormats]) {
NSArray *medata = [asset metadataForFormat:item];
for (AVMetadataItem *mitem in medata) {
NSLog(@"%@:%@",mitem.key,mitem.value);
}
}
/*
2021-07-24 23:22:28.863338+0800 LearnAVFoundation[7405:497502] TXXX:3923013_396752
2021-07-24 23:22:28.863393+0800 LearnAVFoundation[7405:497502] TCON:3923013_396752
2021-07-24 23:22:28.863429+0800 LearnAVFoundation[7405:497502] TSSE:Lavf56.4.101
2021-07-24 23:22:28.863464+0800 LearnAVFoundation[7405:497502] TPOS:1
2021-07-24 23:22:28.863499+0800 LearnAVFoundation[7405:497502] TRCK:12
2021-07-24 23:22:28.863529+0800 LearnAVFoundation[7405:497502] TPE1:Thia Megia
2021-07-24 23:22:28.863574+0800 LearnAVFoundation[7405:497502] APIC:{length = 665140, bytes = 0x89504e47 0d0a1a0a 0000000d 49484452 ... 49454e44 ae426082 }
2021-07-24 23:22:28.863606+0800 LearnAVFoundation[7405:497502] COMM:163 key(Don't modify):L64FU3W4YxX3ZFTmbZ+8/cmlcJixcHCmHiX1THVPt/hhwCr8IK4+AdU6ba2JAq6yLF2az6pftnczEio2AkbRn47o8pfzLEbtKa3vNxCKaBpQujCz48iUDx0+FASY522oYNHmaeewFYBc+xLPSfsyaUgHaQaa/ojLxzzWUVv50Mg/I2IoOKyxPTUquetcGKXGgEofzvAcSlem6yEo7nkM9Nkmbhv7/ne7NlU5GvpG8nOvoBYZiG5otuvxV8pc7ti7ATYNINAs3qHiA2LTuJ9l6CxYtCboFlRr29BVyJSH1eMjIA9m04AEobEgVsuiyG+VXTNeh8imOV8WG268fh5dRxD0EEYDscUALQfptmS3TWSoyM7LF6ASZYn7aBZNkheSYNjgijkkEAt31UpVbNbLjW+HyJwvJyJ/Tls9A9h0PFfmIbeSTrjenxpMz22eRkw/WM3snECea8IhyNFe71F0VXmiZFi5/A6aiBv2EJrHFDKQWD1ktANXSTiTcjmLIEIb7b8cQdI8quSJAZ02VcS4zA==
2021-07-24 23:22:28.863647+0800 LearnAVFoundation[7405:497502] TIT2:Colors Of The Wind
2021-07-24 23:22:28.863676+0800 LearnAVFoundation[7405:497502] TALB:American Idol Top 12 Season 10
*/
四.使用metadata获取元数据
/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - AVMetadataItem 获取媒体属性元数据
//@Time:2021/07/29 07:30
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/
NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"456.mp3" ofType:nil]];
AVURLAsset* asset = [[AVURLAsset alloc] initWithURL:url options:nil];
[asset loadValuesAsynchronouslyForKeys:@[@"metadata"] completionHandler:^{
AVKeyValueStatus commonStatus = [asset statusOfValueForKey:@"metadata" error:nil];
if (commonStatus == AVKeyValueStatusLoaded) {
// 歌曲名称
AVMetadataItem *titleItem = [AVMetadataItem metadataItemsFromArray:asset.metadata filteredByIdentifier:AVMetadataCommonIdentifierTitle].firstObject;
// 演唱者
AVMetadataItem *artistItem = [AVMetadataItem metadataItemsFromArray:asset.metadata filteredByIdentifier:AVMetadataCommonIdentifierArtist].firstObject;
// 专辑名称
AVMetadataItem *albumItem = [AVMetadataItem metadataItemsFromArray:asset.metadata filteredByIdentifier:AVMetadataCommonIdentifierAlbumName].firstObject;
NSLog(@"%@ : %@", titleItem.key, titleItem.value);
NSLog(@"%@ : %@", artistItem.key, artistItem.value);
NSLog(@"%@ : %@", albumItem.key, albumItem.value);
}
}];
/*
2021-07-24 23:25:12.442039+0800 LearnAVFoundation[7441:500002] TIT2 : Colors Of The Wind
2021-07-24 23:25:12.442086+0800 LearnAVFoundation[7441:500002] TPE1 : Thia Megia
2021-07-24 23:25:12.442113+0800 LearnAVFoundation[7441:500002] TALB : American Idol Top 12 Season 10
*/
五.编辑元数据
AVAsset 是一个不可变类,如果要保存元数据的修改,使用 AVAssetExportSession 导出一个新的资源副本以及元数据改动。
AVAssetExportSession 用于将 AVAsset 内容根据导出预设条件进行转码,并将导出资源写到磁盘。它提供了多个功能来实现将一种格式转换为另一种格式、修订资源的内容、修改资源的音频和视频行为、写入新的元数据。
创建一个 AVAssetExportSession 实例需要提供资源和导出预设。导出预设用于确定导出内容的质量、大小等属性。创建导出会话后,还要指定导出内容地址 outputURL ,最后调用 exportAsynchronouslyWithCompletionHandler 开始导出。
/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - AVMetadataItem 获取媒体属性元数据
//@Time:2021/07/29 07:30
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/
//加载媒体
NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"456.mp3" ofType:nil]];
AVURLAsset* asset = [[AVURLAsset alloc] initWithURL:url options:nil];
//导出
NSString *presetName = AVAssetExportPresetPassthrough;
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:presetName];
//NSURL *outputUrl = @"导出地址" ;
exportSession.outputURL = outputUrl;
exportSession.outputFileType = AVFileTypeMPEGLayer3; //mp3
exportSession.metadata = [asset availableMetadataFormats]
//异步导出
[exportSession exportAsynchronouslyWithCompletionHandler:^{
AVAssetExportSessionStatus status = exportSession.status;
BOOL success = (status == AVAssetExportSessionStatusCompleted);
if (success) {
// todo
}
}];
注意:AVAssetExportPresetPassthrough 可以修改存在的元数据信息,不过它不能添加新的元数据,添加元数据唯一方法是使用转码预设值。
六.猜你喜欢
- AVAsset 加载媒体
- AVAssetTrack 获取视频 音频信息
- AVMetadataItem 获取媒体属性元数据
- AVAssetImageGenerator 截图
- AVAssetImageGenerator 获取多帧图片
- AVAssetExportSession 裁剪/转码
- AVPlayer 播放视频
- AVPlayerItem 管理资源对象
- AVPlayerLayer 显示视频
- AVQueuePlayer 播放多个媒体文件
- AVComposition AVMutableComposition 将多个媒体合并
- AVVideoComposition AVMutableVideoComposition 管理所有视频轨道
ChatGPT 3.5 国内中文镜像站免费使用啦
暂无评论内容