AVFoundation – AVAsset 加载媒体
目錄
- 一.前言
- 1.AVAsset
- 2.AVAssetTrack
- 3.AVComposition / AVMutableComposition
- 4.AVMutableVideoComposition
- 5.AVMutableCompositionTrack
- 6.AVMutableVideoCompositionLayerInstruction
- 7.AVMutableVideoCompositionInstruction
- 8.AVAssetExportSession
- 二.AVAsset 簡介
- 三.創建 AVAsset
- 四.異步加載 AVAsset
- 五.AVAsset 常用屬性
- 六.猜你喜歡
零基礎 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 主要用于導出視頻;
二.AVAsset 簡介
- AVAsset 是 AVFoundation 框架中的核心的類,它提供了基于時間的音視頻數據.(如電影文件,視頻流),一個 asset 包含很多軌道的結合,如 audio , video , text , closed captions, subtitles …
- AVMetadataItem 提供了一個 asset 相關的所有資源信息.
- AVAssetTrack 一個軌道可以代表一個音頻軌道或視頻軌道
三.創建 AVAsset
Assets 可以來自一個文件或用戶的相冊,可以理解為多媒體資源,通過 URL 作為一個 asset 對象的標識. 這個 URL 可以是本地文件路徑或網絡流
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:@"網絡路徑"] [NSURL fileURLWithPath:@"本地路徑"] 如果讀取的是本地文件,那么請用第二個方法,第一個會出錯,讀取不到URL.示例:
/******************************************************************************************/ //@Author:猿說編程 //@Blog(個人博客地址): www.codersrc.com //@File:C語言教程 - AVAsset 加載媒體 //@Time:2021/07/25 07:30 //@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累! /******************************************************************************************///獲取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:主要用于獲取多媒體信息,是一個抽象類,不能直接使用。 AVURLAsset:AVAsset 的子類,可以根據一個 URL 路徑創建一個包含媒體信息的 AVURLAsset 對象;
四.異步加載 AVAsset
初始化 asset 并意味著你檢索的信息可以馬上使用. 它可能需要一定時間去計算視頻的信息.因此我們需要使用 block 異步接受處理的結果.使用 AVAsynchronousKeyValueLoading 協議.**示例代碼如下:
/******************************************************************************************/ //@Author:猿說編程 //@Blog(個人博客地址): www.codersrc.com //@File:C語言教程 - AVAsset 加載媒體 //@Time:2021/07/25 07:30 //@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累! /******************************************************************************************/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;/*播放的優選音量,一般為1;*/ @property (nonatomic, readonly) float preferredVolume;/*用于呈現或處理asset可視內容的首選轉換,一般為單位變換;*/ @property (nonatomic, readonly) CGAffineTransform preferredTransform;/*一個布爾值,指示資產是否提供精確的時間,NO為不提供,YSE提供。可以在使用URL初始化資產時,設置與時間相關的屬性所需的精確度;*/ @property (nonatomic, readonly) BOOL providesPreciseDurationAndTiming;/*獲取接受者使用的控制對外部媒體數據引用的限制;對于AVURLAsset來說,該屬性表示AVURLAssetReferenceRestrictionsKey鍵(如果存在)對應的值。*/ @property (nonatomic, readonly) AVAssetReferenceRestrictions referenceRestrictions;//確定asset某些功能的可用性 /*指示AVPlayer是否可以以滿足用戶期望的方式播放資產的內容(指這一asset或者它的URL是否能用來初始化一個AVPlayerItem的實例);*/ @property (nonatomic, readonly, getter=isPlayable) BOOL playable;/*指示asset是否具有受保護的內容。即使媒體資源的playable屬性值為YES。包含受保護內容的資產可能無法在未經授權的情況下播放。*/ @property (nonatomic, readonly) BOOL hasProtectedContent;/*指示asset是否可以使用AVAssetExportSession導出。*/ @property (nonatomic, readonly, getter=isExportable) BOOL exportable;/*指示是否可以使用AVAssetReader提取asset的媒體數據。*/ @property (nonatomic, readonly, getter=isReadable) BOOL readable;/*指示是否該asset可以在AVCompositionTrack對象的區段內使用,被用來創建一個AVMutableComposition對象。*/ @property (nonatomic, readonly, getter=isComposable) BOOL composable;/*指示是否可以將資源寫入“已保存的照片”相冊*/ @property (nonatomic, readonly, getter=isCompatibleWithSavedPhotosAlbum) BOOL compatibleWithSavedPhotosAlbum;/*指示資產是否與AirPlay Video兼容。如果用asset初始化的AVPlayerItem可以通過AirPlay Video由外部設備播放則為YES,反之為NO。*/ @property (nonatomic, readonly, getter=isCompatibleWithAirPlayVideo) BOOL compatibleWithAirPlayVideo;//訪問軌道(tracks)相關 /*asset包含的所有軌道(AVAssetTrack的實例)的集合;*/ @property (nonatomic, readonly) NSArray<AVAssetTrack *> *tracks;/*返回具有指定軌道ID的軌道,如果指定trackID的軌道不不存在,則返回nil;*/ - (nullable AVAssetTrack *)trackWithTrackID:(CMPersistentTrackID)trackID;/*返回呈現指定類型媒體的資產的資產軌道數組;*/ - (NSArray<AVAssetTrack *> *)tracksWithMediaType:(AVMediaType)mediaType;/*返回呈現具有指定特征的媒體的AVAssetTrack對象的數組;*/ - (NSArray<AVAssetTrack *> *)tracksWithMediaCharacteristic:(AVMediaCharacteristic)mediaCharacteristic;/*返回asset中所有軌道組(不同的軌道分組)的數組*/ @property (nonatomic, readonly) NSArray<AVAssetTrackGroup *> *trackGroups;//訪問元數據相關 /*獲取asset的創建日期,該屬性可能為nil,如果創建日期已被asset以可轉換為NSDate對象的形式存儲,則AVMetadataItem的dateValue屬性將提供一個NSDate的實例。否則創建日期只能使用其stringValue值作為字符串值。*/ @property (nonatomic, readonly, nullable) AVMetadataItem *creationDate;/*提供對適合當前語言環境的asset的文字歌詞的訪問;*/ @property (nonatomic, readonly, nullable) NSString *lyrics;/*屬性中包含著當前視頻公共密鑰空間中常見格式類型的元數據;*/ @property (nonatomic, readonly) NSArray<AVMetadataItem *> *commonMetadata;/*屬性中包含當前視頻所有格式類型的元數據;*/ @property (nonatomic, readonly) NSArray<AVMetadataItem *> *metadata/*一組字符串,每個字符串都代表資產可用的元數據格式;*/ @property (nonatomic, readonly) NSArray<AVMetadataFormat> *availableMetadataFormats;/*根據元數據格式返回AVMetadataItem對象數組。*/ - (NSArray<AVMetadataItem *> *)metadataForFormat:(AVMetadataFormat)format;六.猜你喜歡
- AVAsset 加載媒體
- AVAssetTrack 獲取視頻 音頻信息
- AVMetadataItem 獲取媒體屬性元數據
- AVAssetImageGenerator 截圖
- AVAssetImageGenerator 獲取多幀圖片
- AVAssetExportSession 裁剪/轉碼
- AVPlayer 播放視頻
- AVPlayerItem 管理資源對象
- AVPlayerLayer 顯示視頻
- AVQueuePlayer 播放多個媒體文件
- AVComposition AVMutableComposition 將多個媒體合并
- AVVideoComposition AVMutableVideoComposition 管理所有視頻軌道
未經允許不得轉載:猿說編程 ? AVFoundation – AVAsset 加載媒體
總結
以上是生活随笔為你收集整理的AVFoundation – AVAsset 加载媒体的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: qt生成无ui界面动态库,有ui界面的动
- 下一篇: 从数组随机抽取5个不重复_Power Q