AVFoundation – AVAsset 加载媒体
目錄
- 一.前言 - 1.AVAsset
- 2.AVAssetTrack
- 3.AVComposition / AVMutableComposition
- 4.AVMutableVideoComposition
- 5.AVMutableCompositionTrack
- 6.AVMutableVideoCompositionLayerInstruction
- 7.AVMutableVideoCompositionInstruction
- 8.AVAssetExportSession
 
- 二.AVAsset 簡(jiǎn)介
- 三.創(chuàng)建 AVAsset
- 四.異步加載 AVAsset
- 五.AVAsset 常用屬性
- 六.猜你喜歡
零基礎(chǔ) Object-C 學(xué)習(xí)路線推薦 : Object-C 學(xué)習(xí)目錄 >> Object-C 基礎(chǔ)
零基礎(chǔ) Object-C 學(xué)習(xí)路線推薦 : Object-C 學(xué)習(xí)目錄 >> Object-C 線程
零基礎(chǔ) Object-C 學(xué)習(xí)路線推薦 : Object-C 學(xué)習(xí)目錄 >> OpenGL ES
零基礎(chǔ) Object-C 學(xué)習(xí)路線推薦 : Object-C 學(xué)習(xí)目錄 >> GPUImage
零基礎(chǔ) Object-C 學(xué)習(xí)路線推薦 : Object-C 學(xué)習(xí)目錄 >> AVFoundation
零基礎(chǔ) Object-C 學(xué)習(xí)路線推薦 : Object-C 學(xué)習(xí)目錄 >> CocoaPods
一.前言
1.AVAsset
Assets 可以來(lái)自一個(gè)文件或用戶的相冊(cè),可以理解為多媒體資源,通過(guò) URL 作為一個(gè) asset 對(duì)象的標(biāo)識(shí). 這個(gè) URL 可以是本地文件路徑或網(wǎng)絡(luò)流;
2.AVAssetTrack
AVAsset 包含很多軌道 **AVAssetTrack **的結(jié)合,如 audio, video, text, closed captions, subtitles…
3.AVComposition / AVMutableComposition
**使用 AVMutableComposition 類(lèi)可以增刪 AVAsset 來(lái)將單個(gè)或者多個(gè) AVAsset 集合到一起,用來(lái)合成新視頻。**除此之外,若想將集合到一起的視聽(tīng)資源以自定義的方式進(jìn)行播放,需要使用 AVMutableAudioMix 和 AVMutableVideoComposition 類(lèi)對(duì)其中的資源進(jìn)行協(xié)調(diào)管理;
4.AVMutableVideoComposition
AVFoundation 類(lèi) API 中最核心的類(lèi)是 AVVideoComposition / AVMutableVideoComposition 。
AVVideoComposition / AVMutableVideoComposition 對(duì)兩個(gè)或多個(gè)視頻軌道組合在一起的方法給出了一個(gè)總體描述。它由一組時(shí)間范圍和描述組合行為的介紹內(nèi)容組成。這些信息出現(xiàn)在組合資源內(nèi)的任意時(shí)間點(diǎn)。
AVVideoComposition / AVMutableVideoComposition 管理所有視頻軌道,可以決定最終視頻的尺寸,裁剪需要在這里進(jìn)行;
5.AVMutableCompositionTrack
將多個(gè) AVAsset 集合到一起合成新視頻中軌道信息,有音頻軌、視頻軌等,里面可以插入各種對(duì)應(yīng)的素材(畫(huà)中畫(huà),水印等);
6.AVMutableVideoCompositionLayerInstruction
AVMutableVideoCompositionLayerInstruction 主要用于對(duì)視頻軌道中的一個(gè)視頻處理縮放、模糊、裁剪、旋轉(zhuǎn)等;
7.AVMutableVideoCompositionInstruction
表示一個(gè)指令,決定一個(gè) timeRange 內(nèi)每個(gè)軌道的狀態(tài),每一個(gè)指令包含多個(gè) AVMutableVideoCompositionLayerInstruction ;而 AVVideoComposition 由多個(gè) AVVideoCompositionInstruction 構(gòu)成;
AVVideoCompositionInstruction 所提供的最關(guān)鍵的一段數(shù)據(jù)是組合對(duì)象時(shí)間軸內(nèi)的時(shí)間范圍信息。這一時(shí)間范圍是在某一組合形式出現(xiàn)時(shí)的時(shí)間范圍。要執(zhí)行的組全特質(zhì)是通過(guò)其 AVMutableVideoCompositionLayerInstruction 集合定義的。
8.AVAssetExportSession
AVAssetExportSession 主要用于導(dǎo)出視頻;
二.AVAsset 簡(jiǎn)介
- AVAsset 是 AVFoundation 框架中的核心的類(lèi),它提供了基于時(shí)間的音視頻數(shù)據(jù).(如電影文件,視頻流),一個(gè) asset 包含很多軌道的結(jié)合,如 audio , video , text , closed captions, subtitles …
- AVMetadataItem 提供了一個(gè) asset 相關(guān)的所有資源信息.
- AVAssetTrack 一個(gè)軌道可以代表一個(gè)音頻軌道或視頻軌道
三.創(chuàng)建 AVAsset
Assets 可以來(lái)自一個(gè)文件或用戶的相冊(cè),可以理解為多媒體資源,通過(guò) URL 作為一個(gè) asset 對(duì)象的標(biāo)識(shí). 這個(gè) URL 可以是本地文件路徑或網(wǎng)絡(luò)流
NSURL *url = <#A URL that identifies an audiovisual asset such as a movie file#>; AVURLAsset *anAsset = [[AVURLAsset alloc] initWithURL:url options:nil];注意 NSURL 的使用:
[NSURL URLWithString:@"網(wǎng)絡(luò)路徑"] [NSURL fileURLWithPath:@"本地路徑"] 如果讀取的是本地文件,那么請(qǐng)用第二個(gè)方法,第一個(gè)會(huì)出錯(cuò),讀取不到URL.示例:
/******************************************************************************************/ //@Author:猿說(shuō)編程 //@Blog(個(gè)人博客地址): www.codersrc.com //@File:C語(yǔ)言教程 - AVAsset 加載媒體 //@Time:2021/07/25 07:30 //@Motto:不積跬步無(wú)以至千里,不積小流無(wú)以成江海,程序人生的精彩需要堅(jiān)持不懈地積累! /******************************************************************************************///獲取url NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"123.mp4" ofType:nil]];//加載媒體方案一 AVURLAsset* asset = [[AVURLAsset alloc] initWithURL:url options:nil]; //加載媒體方案二 AVURLAsset *asset2 = [AVURLAsset assetWithURL:url];NSLog(@"asset:%@ time:%f",asset,CMTimeGetSeconds(asset.duration)); /* asset:<AVURLAsset: 0x600001f640c0, URL = file:///Users/xxx/Library/Developer/CoreSimulator/Devices/CF7390AF-D7D9-4CDA-8049-167662FFAEAD/data/Containers/Bundle/Application/536B4B5E-46A7-4D53-91B4-1C09A0A72764/LearnAVFoundation.app/123.mp4> time:4249.883000 */AVAsset:主要用于獲取多媒體信息,是一個(gè)抽象類(lèi),不能直接使用。 AVURLAsset:AVAsset 的子類(lèi),可以根據(jù)一個(gè) URL 路徑創(chuàng)建一個(gè)包含媒體信息的 AVURLAsset 對(duì)象;
四.異步加載 AVAsset
初始化 asset 并意味著你檢索的信息可以馬上使用. 它可能需要一定時(shí)間去計(jì)算視頻的信息.因此我們需要使用 block 異步接受處理的結(jié)果.使用 AVAsynchronousKeyValueLoading 協(xié)議.**示例代碼如下:
/******************************************************************************************/ //@Author:猿說(shuō)編程 //@Blog(個(gè)人博客地址): www.codersrc.com //@File:C語(yǔ)言教程 - AVAsset 加載媒體 //@Time:2021/07/25 07:30 //@Motto:不積跬步無(wú)以至千里,不積小流無(wú)以成江海,程序人生的精彩需要堅(jiān)持不懈地積累! /******************************************************************************************/NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"123.mp4" ofType:nil]];AVURLAsset* asset = [[AVURLAsset alloc] initWithURL:url options:nil];NSLog(@"time:%f",CMTimeGetSeconds(asset.duration)); NSArray *keys = @[@"duration"];//異步加載 [asset loadValuesAsynchronouslyForKeys:keys completionHandler:^() {NSError *error = nil;AVKeyValueStatus tracksStatus = [asset statusOfValueForKey:@"duration" error:&error];switch (tracksStatus) {case AVKeyValueStatusUnknown:NSLog(@"AVKeyValueStatusUnknown");break;case AVKeyValueStatusLoading: //正在加載NSLog(@"AVKeyValueStatusLoading");break;case AVKeyValueStatusLoaded: //加載完成NSLog(@"AVKeyValueStatusLoaded");break;case AVKeyValueStatusFailed: //加載失敗NSLog(@"AVKeyValueStatusFailed");break;case AVKeyValueStatusCancelled: //取消加載NSLog(@"AVKeyValueStatusCancelled");break; } }];五.AVAsset 常用屬性
/*播放速率,一般為1;*/ @property (nonatomic, readonly) float preferredRate;/*播放的優(yōu)選音量,一般為1;*/ @property (nonatomic, readonly) float preferredVolume;/*用于呈現(xiàn)或處理asset可視內(nèi)容的首選轉(zhuǎn)換,一般為單位變換;*/ @property (nonatomic, readonly) CGAffineTransform preferredTransform;/*一個(gè)布爾值,指示資產(chǎn)是否提供精確的時(shí)間,NO為不提供,YSE提供??梢栽谑褂肬RL初始化資產(chǎn)時(shí),設(shè)置與時(shí)間相關(guān)的屬性所需的精確度;*/ @property (nonatomic, readonly) BOOL providesPreciseDurationAndTiming;/*獲取接受者使用的控制對(duì)外部媒體數(shù)據(jù)引用的限制;對(duì)于AVURLAsset來(lái)說(shuō),該屬性表示AVURLAssetReferenceRestrictionsKey鍵(如果存在)對(duì)應(yīng)的值。*/ @property (nonatomic, readonly) AVAssetReferenceRestrictions referenceRestrictions;//確定asset某些功能的可用性 /*指示AVPlayer是否可以以滿足用戶期望的方式播放資產(chǎn)的內(nèi)容(指這一asset或者它的URL是否能用來(lái)初始化一個(gè)AVPlayerItem的實(shí)例);*/ @property (nonatomic, readonly, getter=isPlayable) BOOL playable;/*指示asset是否具有受保護(hù)的內(nèi)容。即使媒體資源的playable屬性值為YES。包含受保護(hù)內(nèi)容的資產(chǎn)可能無(wú)法在未經(jīng)授權(quán)的情況下播放。*/ @property (nonatomic, readonly) BOOL hasProtectedContent;/*指示asset是否可以使用AVAssetExportSession導(dǎo)出。*/ @property (nonatomic, readonly, getter=isExportable) BOOL exportable;/*指示是否可以使用AVAssetReader提取asset的媒體數(shù)據(jù)。*/ @property (nonatomic, readonly, getter=isReadable) BOOL readable;/*指示是否該asset可以在AVCompositionTrack對(duì)象的區(qū)段內(nèi)使用,被用來(lái)創(chuàng)建一個(gè)AVMutableComposition對(duì)象。*/ @property (nonatomic, readonly, getter=isComposable) BOOL composable;/*指示是否可以將資源寫(xiě)入“已保存的照片”相冊(cè)*/ @property (nonatomic, readonly, getter=isCompatibleWithSavedPhotosAlbum) BOOL compatibleWithSavedPhotosAlbum;/*指示資產(chǎn)是否與AirPlay Video兼容。如果用asset初始化的AVPlayerItem可以通過(guò)AirPlay Video由外部設(shè)備播放則為YES,反之為NO。*/ @property (nonatomic, readonly, getter=isCompatibleWithAirPlayVideo) BOOL compatibleWithAirPlayVideo;//訪問(wèn)軌道(tracks)相關(guān) /*asset包含的所有軌道(AVAssetTrack的實(shí)例)的集合;*/ @property (nonatomic, readonly) NSArray<AVAssetTrack *> *tracks;/*返回具有指定軌道ID的軌道,如果指定trackID的軌道不不存在,則返回nil;*/ - (nullable AVAssetTrack *)trackWithTrackID:(CMPersistentTrackID)trackID;/*返回呈現(xiàn)指定類(lèi)型媒體的資產(chǎn)的資產(chǎn)軌道數(shù)組;*/ - (NSArray<AVAssetTrack *> *)tracksWithMediaType:(AVMediaType)mediaType;/*返回呈現(xiàn)具有指定特征的媒體的AVAssetTrack對(duì)象的數(shù)組;*/ - (NSArray<AVAssetTrack *> *)tracksWithMediaCharacteristic:(AVMediaCharacteristic)mediaCharacteristic;/*返回asset中所有軌道組(不同的軌道分組)的數(shù)組*/ @property (nonatomic, readonly) NSArray<AVAssetTrackGroup *> *trackGroups;//訪問(wèn)元數(shù)據(jù)相關(guān) /*獲取asset的創(chuàng)建日期,該屬性可能為nil,如果創(chuàng)建日期已被asset以可轉(zhuǎn)換為NSDate對(duì)象的形式存儲(chǔ),則AVMetadataItem的dateValue屬性將提供一個(gè)NSDate的實(shí)例。否則創(chuàng)建日期只能使用其stringValue值作為字符串值。*/ @property (nonatomic, readonly, nullable) AVMetadataItem *creationDate;/*提供對(duì)適合當(dāng)前語(yǔ)言環(huán)境的asset的文字歌詞的訪問(wèn);*/ @property (nonatomic, readonly, nullable) NSString *lyrics;/*屬性中包含著當(dāng)前視頻公共密鑰空間中常見(jiàn)格式類(lèi)型的元數(shù)據(jù);*/ @property (nonatomic, readonly) NSArray<AVMetadataItem *> *commonMetadata;/*屬性中包含當(dāng)前視頻所有格式類(lèi)型的元數(shù)據(jù);*/ @property (nonatomic, readonly) NSArray<AVMetadataItem *> *metadata/*一組字符串,每個(gè)字符串都代表資產(chǎn)可用的元數(shù)據(jù)格式;*/ @property (nonatomic, readonly) NSArray<AVMetadataFormat> *availableMetadataFormats;/*根據(jù)元數(shù)據(jù)格式返回AVMetadataItem對(duì)象數(shù)組。*/ - (NSArray<AVMetadataItem *> *)metadataForFormat:(AVMetadataFormat)format;六.猜你喜歡
- AVAsset 加載媒體
- AVAssetTrack 獲取視頻 音頻信息
- AVMetadataItem 獲取媒體屬性元數(shù)據(jù)
- AVAssetImageGenerator 截圖
- AVAssetImageGenerator 獲取多幀圖片
- AVAssetExportSession 裁剪/轉(zhuǎn)碼
- AVPlayer 播放視頻
- AVPlayerItem 管理資源對(duì)象
- AVPlayerLayer 顯示視頻
- AVQueuePlayer 播放多個(gè)媒體文件
- AVComposition AVMutableComposition 將多個(gè)媒體合并
- AVVideoComposition AVMutableVideoComposition 管理所有視頻軌道
未經(jīng)允許不得轉(zhuǎn)載:猿說(shuō)編程 ? AVFoundation – AVAsset 加載媒體
總結(jié)
以上是生活随笔為你收集整理的AVFoundation – AVAsset 加载媒体的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: qt生成无ui界面动态库,有ui界面的动
- 下一篇: 从数组随机抽取5个不重复_Power Q
