日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

AVFoundation – AVAssetTrack 获取视频 音频信息

發布時間:2024/9/27 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AVFoundation – AVAssetTrack 获取视频 音频信息 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

  • 一.前言
    • 1.AVAsset
    • 2.AVAssetTrack
    • 3.AVComposition / AVMutableComposition
    • 4.AVMutableVideoComposition
    • 5.AVMutableCompositionTrack
    • 6.AVMutableVideoCompositionLayerInstruction
    • 7.AVMutableVideoCompositionInstruction
    • 8.AVAssetExportSession
  • 二.AVAssetTrack 簡介
  • 三.AVAssetTrack 獲取
  • 四.AVAssetTrack 介紹
    • 1、檢索跟蹤信息
    • 2、檢索時間屬性
    • 3、檢索語言屬性
    • 4、檢索視覺特征
    • 5、檢索聲音特征
    • 6、檢索基于幀的特征
    • 7、尋找軌道段
    • 8、管理元數據
    • 9、使用關聯軌道
    • 10、創建示例游標
  • 五.AVAssetTrack 使用
  • 六.猜你喜歡

零基礎 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 主要用于導出視頻;

二.AVAssetTrack 簡介

AVAsset 是 AVFoundation 框架中的核心的類,它提供了基于時間的音視頻數據.(如電影文件,視頻流),一個 AVAsset 包含很多軌道 AVAssetTrack 的結合,如 audio, video, text, closed captions, subtitles…

AVF_EXPORT AVMediaType const AVMediaTypeVideo NS_AVAILABLE(10_7, 4_0); AVF_EXPORT AVMediaType const AVMediaTypeAudio NS_AVAILABLE(10_7, 4_0); AVF_EXPORT AVMediaType const AVMediaTypeText NS_AVAILABLE(10_7, 4_0); AVF_EXPORT AVMediaType const AVMediaTypeClosedCaption NS_AVAILABLE(10_7, 4_0); AVF_EXPORT AVMediaType const AVMediaTypeSubtitle NS_AVAILABLE(10_7, 4_0); AVF_EXPORT AVMediaType const AVMediaTypeTimecode NS_AVAILABLE(10_7, 4_0); AVF_EXPORT AVMediaType const AVMediaTypeMetadata NS_AVAILABLE(10_8, 6_0); AVF_EXPORT AVMediaType const AVMediaTypeMuxed NS_AVAILABLE(10_7, 4_0);

三.AVAssetTrack 獲取

AVAssetTrack 可以 通過 AVAsset 獲取,如下

- (nullable AVAssetTrack *)trackWithTrackID:(CMPersistentTrackID)trackID; - (NSArray<AVAssetTrack *> *)tracksWithMediaType:(AVMediaType)mediaType; - (NSArray<AVAssetTrack *> *)tracksWithMediaCharacteristic:(AVMediaCharacteristic)mediaCharacteristic;@property (nonatomic, readonly) NSArray<AVAssetTrack *> *tracks;

其中 track id 是視頻文件中的唯一標識碼。

四.AVAssetTrack 介紹

1、檢索跟蹤信息

/* 軌道所屬的 AVAsset? */ @property (nonatomic, readonly, weak) AVAsset *asset;/* 該軌道的唯一標識符? */ @property (nonatomic, readonly) CMPersistentTrackID trackID;/* 軌道的媒體類型;例如 AVMediaTypeVideo,AVMediaTypeAudio或AVMediaTypeSubtitle */ @property (nonatomic, readonly) AVMediaType mediaType;/* 軌道引用的媒體樣本的格式描述 */ @property (nonatomic, readonly) NSArray *formatDescriptions;/* 指示軌道是否可在當前環境中播放;如果為YES,則可以啟用使用軌道資源初始化的 AVPlayerItem的 AVPlayerItemTrack進行回放 */ @property (nonatomic, readonly, getter=isPlayable) BOOL playable API_AVAILABLE(macos(10.8), ios(5.0), tvos(9.0), watchos(1.0));/* Indicates whether the receiver is decodable in the current environment; if YES, the track can be decoded even though decoding may be too slow for real time playback.? */ @property (nonatomic, readonly, getter=isDecodable) BOOL decodable API_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0));/* 指示是否根據存儲在其容器中的狀態啟用軌道。對于基于文件的媒體,可以使用 AVPlayerItemTrack更改其啟用的演示文稿狀態 */ @property (nonatomic, readonly, getter=isEnabled) BOOL enabled;/* 指示軌道是否引用僅包含在其存儲容器中的樣本數據;如果軌道僅在其存儲容器內引用樣本數據,則值為YES,否則為NO。 */ @property (nonatomic, readonly, getter=isSelfContained) BOOL selfContained;/* 軌道所需的樣本數據的總字節數;如果無法確定總樣本數據長度,則該值可以是0。 */ @property (nonatomic, readonly) long long totalSampleDataLength;/*指示軌道是否引用具有指定媒體特征的媒體 */ - (BOOL)hasMediaCharacteristic:(AVMediaCharacteristic)mediaCharacteristic;

2、檢索時間屬性

/* AVAsset總時間軸內的跟蹤時間范圍。 */ @property (nonatomic, readonly) CMTimeRange timeRange;/* 時間刻度,其中軌道的時間值可以在沒有無關數字轉換的情況下操作。*/ @property (nonatomic, readonly) CMTimeScale naturalTimeScale;/* 由軌道引用的媒體數據的預估數據速率比特每秒。如果未指示數據速率,則該值可以是0.0,并且不能使用存儲在媒體資源中的其他信息來計算該值。 */ @property (nonatomic, readonly) float estimatedDataRate;

3、檢索語言屬性

/* 與軌道相關的語言,作為? ISO 639-2/T?語言代碼。如果沒有指明語言,則該值可以是nil。 */ @property (nonatomic, readonly, nullable) NSString *languageCode;/* 與軌道關聯的語言標記,作為?BCP 47?語言標記。如果未指示語言標記,則該值可以是nil。 */ @property (nonatomic, readonly, nullable) NSString *extendedLanguageTag;

4、檢索視覺特征

/* 軌道引用的媒體數據的原生尺寸。對于視頻軌道,例如視頻或字幕軌道,這將返回媒體的 naturalSize。對于非視頻軌道,例如音頻或章節軌道,這將返回 CGSizeZero的值。*/ @property (nonatomic, readonly) CGSize naturalSize;/* 在軌道的存儲容器中指定的轉換作為可視媒體數據的首選轉換以用于顯示目的。此屬性的值通常(但不總是) CGAffineTransformIdentity。 */ @property (nonatomic, readonly) CGAffineTransform preferredTransform;

5、檢索聲音特征

/*軌道存儲容器中指定的音量作為可聽媒體數據的首選音量;通常(但不總是)為1.0;對于不可聽的音軌,該值將為0.0。 */ @property (nonatomic, readonly) float preferredVolume;

6、檢索基于幀的特征

/*軌道的幀速率,以每秒幀數為單位。標稱幀速率表示每個媒體樣本攜帶全幀的軌道的每秒幀數。對于基于場的(隔行掃描)視頻軌道,此屬性的值指示場速率,而不是幀速率。 */ @property (nonatomic, readonly) float nominalFrameRate;/*軌道幀的最小持續時間。軌道的最小幀持續時間是其最大幀速率的倒數。例如,具有每秒30幀的最大幀速率的視頻軌道將具有1/30(或0.033)的最小幀持續時間。如果軌道的最小幀持續時間未知或無法計算,則此屬性的值為 kCMTimeInvalid。 */ @property (nonatomic, readonly) CMTime minFrameDuration API_AVAILABLE(macos(10.10), ios(7.0), tvos(9.0), watchos(1.0));/*指示軌道中的樣本是否可能具有不同的顯示值和解碼時間戳。 */ @property (nonatomic, readonly) BOOL requiresFrameReordering API_AVAILABLE(macos(10.10), ios(8.0), tvos(9.0), watchos(1.0));

7、尋找軌道段

//從軌道的媒體樣本到其時間線的時間映射 @property (nonatomic, copy, readonly) NSArray<AVAssetTrackSegment *> *segments;//來自segments數組的軌道段對應或最接近trackTime。 - (nullable AVAssetTrackSegment *)segmentForTrackTime:(CMTime)trackTime;//通過適當的時間映射映射指定的跟蹤時間,并返回生成的樣本顯示時間。與trackTime對應的樣本呈現時間; 如果trackTime超出范圍,則該值將無效。 - (CMTime)samplePresentationTimeForTrackTime:(CMTime)trackTime;

8、管理元數據

/* 所有元數據標識符的元數據項數組,其中有一個值可用。* 可以使用-metadataItemsFromArray:filteredAndSortedAccordingToPreferredLanguages:方法根據語言過濾AVMetadataItem實例數組。* 可以使用- metadataItemsFromArray:filteredByIdentifier:方法通過標識符過濾它們。*/ @property (nonatomic, readonly) NSArray<AVMetadataItem *> *metadata;//每個公共元數據鍵的AVMetadataItem對象數組,其值可用。 @property (nonatomic, readonly) NSArray<AVMetadataItem *> *commonMetadata;//該數組包含NSString對象,每個對象用于軌道可用的每種元數據格式 @property (nonatomic, readonly) NSArray<AVMetadataFormat> *availableMetadataFormats;//創建一個元數據對象數組,每個元數據對象對應于指定格式的容器中的每個元數據項。 - (NSArray<AVMetadataItem *> *)metadataForFormat:(AVMetadataFormat)format;

9、使用關聯軌道

//該數組包含NSString對象,每個類型的關聯對應一個對象。 @property (nonatomic, readonly) NSArray<AVTrackAssociationType> *availableTrackAssociationTypes;//創建一個數組,其中包含與使用指定關聯類型的軌道關聯的其他軌道。 - (NSArray<AVAssetTrack *> *)associatedTracksOfType:(AVTrackAssociationType)trackAssociationType;

10、創建示例游標

//指示AVAssetTrack是否可以提供AVSampleCursor的實例來遍歷其媒體樣本并發現信息。 @property (nonatomic, readonly) BOOL canProvideSampleCursors;/* 創建AVSampleCursor的實例,并將其放置在指定的時間戳附近。 * 如果AVAssetTrack的asset的屬性receiverPreciseDurationAndTiming的值為YES,則示例光標準確定位在接收者的最后一個媒體示例上,其顯示時間戳小于或等于所需的時間戳,如果沒有這樣的示例,則為表示順序中的第一個示例。 * 如果providePreciseDurationAndTiming為NO,并且將精確樣本定位在期望的時間戳上非常昂貴,則可以近似定位樣本光標。 */ - (nullable AVSampleCursor *)makeSampleCursorWithPresentationTimeStamp:(CMTime)presentationTimeStamp;//創建AVSampleCursor的實例,并將其按解碼順序放置在接收器的第一個媒體樣本上。 - (nullable AVSampleCursor *)makeSampleCursorAtFirstSampleInDecodeOrder;//創建AVSampleCursor的實例,并將其按解碼順序放置在接收器的最后一個媒體樣本上。 - (nullable AVSampleCursor *)makeSampleCursorAtLastSampleInDecodeOrder;

五.AVAssetTrack 使用

根據 AVAsset 獲取 AVAssetTrack 檢查視頻是否自帶旋轉角度(比如相機拍攝的視頻有可能帶有旋轉角度)

/******************************************************************************************/ //@Author:猿說編程 //@Blog(個人博客地址): www.codersrc.com //@File:C語言教程 - AVAssetTrack 獲取視頻 音頻信息 //@Time:2021/07/26 07:30 //@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累! /******************************************************************************************/- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor redColor];//加載視頻文件,檢查是否有旋轉角度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));NSLog(@"angle : %d",[self degressFromVideoFileWithURL:url]);//加載音頻文件,檢查是否有視頻NSURL* url2 = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"456.mp3" ofType:nil]];AVURLAsset* asset2 = [[AVURLAsset alloc] initWithURL:url options:nil];NSArray *videoTracks = [asset2 tracksWithMediaType:AVMediaTypeVideo]; // 檢查是否有視頻軌道NSArray *audioTracks = [asset2 tracksWithMediaType:AVMediaTypeAudio]; // 檢查是否有音頻軌道if([videoTracks count] > 0)NSLog(@"存在視頻");if([audioTracks count] > 0)NSLog(@"存在音頻"); }-(NSUInteger)degressFromVideoFileWithURL:(NSURL *)url {NSUInteger degress = 0;AVAsset *asset = [AVAsset assetWithURL:url];NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo]; // 檢查是否有視頻軌道if([tracks count] > 0) {AVAssetTrack *videoTrack = [tracks objectAtIndex:0];//獲取視頻軌道相關信息CGAffineTransform t = videoTrack.preferredTransform;if(t.a == 0 && t.b == 1.0 && t.c == -1.0 && t.d == 0){// Portraitdegress = 90;}else if(t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0){// PortraitUpsideDowndegress = 270;}else if(t.a == 1.0 && t.b == 0 && t.c == 0 && t.d == 1.0){// LandscapeRightdegress = 0;}else if(t.a == -1.0 && t.b == 0 && t.c == 0 && t.d == -1.0){// LandscapeLeftdegress = 180;}}return degress; } /* time:4249.883000 angle : 90 存在音頻 */

六.猜你喜歡

  • AVAsset 加載媒體
  • AVAssetTrack 獲取視頻 音頻信息
  • AVMetadataItem 獲取媒體屬性元數據
  • AVAssetImageGenerator 截圖
  • AVAssetImageGenerator 獲取多幀圖片
  • AVAssetExportSession 裁剪/轉碼
  • AVPlayer 播放視頻
  • AVPlayerItem 管理資源對象
  • AVPlayerLayer 顯示視頻
  • AVQueuePlayer 播放多個媒體文件
  • AVComposition AVMutableComposition 將多個媒體合并
  • AVVideoComposition AVMutableVideoComposition 管理所有視頻軌道

未經允許不得轉載:猿說編程 ? AVFoundation – AVAssetTrack 獲取視頻 音頻信息

總結

以上是生活随笔為你收集整理的AVFoundation – AVAssetTrack 获取视频 音频信息的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。