AVComposition使用
一男窟、理解
對象AVComposition
AVComposition繼承自AVAsset,通過其可變對象將來自多個媒體資源數(shù)據(jù)組合在一起顯示牺六。
AVComposition是有資源軌道(AVMutableCompositionTrack)信息組成,如視頻資源中包含了音頻軌道和視頻軌道盗胀;每個軌道是有軌道片段(AVCompositionTrackSegment)組成宅荤。
AVMutableComposition
//生成一個空的可變組合對象
+ (instancetype)composition;
//生成一個空的可變組合對象,并且可以設(shè)置一些信息如:AVURLAssetPreferPreciseDurationAndTimingKey 精確的時間設(shè)置
+ (instancetype)compositionWithURLAssetInitializationOptions:(nullable NSDictionary<NSString *, id> *)URLAssetInitializationOptions;
//獲取軌道資源信息
@property (nonatomic, readonly) NSArray<AVMutableCompositionTrack *> *tracks;
//組合資源輸出的尺寸
@property (nonatomic) CGSize naturalSize;
//將一段指定的Asset資源(只能是 AVURLAsset 和 AVComposition)插入到軌道指定時間點
- (BOOL)insertTimeRange:(CMTimeRange)timeRange ofAsset:(AVAsset *)asset atTime:(CMTime)startTime error:(NSError * _Nullable * _Nullable)outError;
//在合成軌道上添加一段空timeRange
- (void)insertEmptyTimeRange:(CMTimeRange)timeRange;
//合成軌道上刪除指定timeRange
- (void)removeTimeRange:(CMTimeRange)timeRange;
// 添加空的視頻或音頻軌道
// mediaType:添加軌道的媒體類型
// preferredTrackID:指定新軌道的首選軌道ID惫确。如果你不提供一個明確的就設(shè)置成kCMPersistentTrackID_Invalid昧诱,會自動生成一個唯一的TrackID,否則你就需要提供一個未被使用過的一個TrackID
- (nullable AVMutableCompositionTrack *)addMutableTrackWithMediaType:(AVMediaType)mediaType preferredTrackID:(CMPersistentTrackID)preferredTrackID;
image
二、使用
簡單的使用方法;
1. 創(chuàng)建可變組合對象
AVMutableComposition *composition = [AVMutableComposition composition];
2. 創(chuàng)建視頻和音頻軌道
AVMutableCompositionTrack *videoCompositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *audioCompositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
3. 插入指定的視頻軌道和音頻軌道
AVAssetTrack *videoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] firstObject];
[videoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:videoTrack atTime:kCMTimeZero error:nil];
AVAssetTrack *audioAssetTrack = [[audioAsset tracksWithMediaType:AVMediaTypeAudio] firstObject];
[audioCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAssetTrack.duration) ofTrack:audioAssetTrack atTime:kCMTimeZero error:nil];
4. 導(dǎo)出組合后的媒體資源
NSURL *videoLocalPath = [NSURL fileURLWithPath:outputPath];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:composition presetName:AVAssetExportPresetMediumQuality];
exportSession.outputFileType = AVFileTypeQuickTimeMovie; // 導(dǎo)出資源類型
exportSession.outputURL = videoLocalPath; // 導(dǎo)出資源路徑
exportSession.shouldOptimizeForNetworkUse = YES; // 導(dǎo)出資源針對網(wǎng)絡(luò)加載優(yōu)化
[exportSession exportAsynchronouslyWithCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
//導(dǎo)出完成
if (exportSession.status == AVAssetExportSessionStatusCompleted ) {
if (successBlock) {
successBlock(exportSession.outputURL);
}
} else {
NSLog(@"視頻合成失敗--%@",exportSession.error);
if (successBlock) {
successBlock(exportSession.error);
}
}
});
}];
三、高級媒體組合
媒體資源組合時也可以對各個媒體資源自定義處理。
AVMutableVideoComposition
使用 AVMutableVideoComposition 可以直接處理視頻的軌道崖技,通過它輸出視頻時還可以控制輸出的尺寸、縮放比例装诡、幀率;
image
// 生成一個videoComposition
+ (AVMutableVideoComposition *)videoComposition;
// 幀率 必須設(shè)置否則crash
@property (nonatomic) CMTime frameDuration;
// 渲染尺寸 必須設(shè)置否則crash
@property (nonatomic) CGSize renderSize;
// 設(shè)置渲染縮放比
@property (nonatomic) float renderScale
// 媒體處理指令集
@property (nonatomic, copy) NSArray<id <AVVideoCompositionInstruction>> *instructions;
// 通過使用Core Animation對視頻做一些處理肄程,如添加水印等
@property (nonatomic, retain, nullable) AVVideoCompositionCoreAnimationTool *animationTool;
AVMutableVideoCompositionInstruction
視頻指令集
// 生成視頻指令集
+ (instancetype)videoCompositionInstruction;
// 設(shè)置指令集作用的timeRange
@property (nonatomic, assign) CMTimeRange timeRange;
// 設(shè)置媒體背景顏色
@property (nonatomic, retain, nullable) __attribute__((NSObject)) CGColorRef backgroundColor;
// 媒體layer層指令集
@property (nonatomic, copy) NSArray<AVVideoCompositionLayerInstruction *> *layerInstructions;
AVMutableVideoCompositionLayerInstruction
// 根據(jù)媒體軌道信息生成layer 指令對象
+ (instancetype)videoCompositionLayerInstructionWithAssetTrack:(AVAssetTrack *)track;
// 旋轉(zhuǎn)媒體資源
- (void)setTransform:(CGAffineTransform)transform atTime:(CMTime)time;
- - (void)setTransformRampFromStartTransform:(CGAffineTransform)startTransform toEndTransform:(CGAffineTransform)endTransform timeRange:(CMTimeRange)timeRange;
// 設(shè)置透明度
- (void)setOpacityRampFromStartOpacity:(float)startOpacity toEndOpacity:(float)endOpacity timeRange:(CMTimeRange)timeRange;
- (void)setOpacity:(float)opacity atTime:(CMTime)time;
-
AVMutableAudioMix
image
// 生成一個空的音頻管理對象
+ (instancetype)audioMix;
// 設(shè)置混合音頻處理集
@property (nonatomic, copy) NSArray<AVAudioMixInputParameters *> *inputParameters;
AVMutableAudioMixInputParameters
// 根據(jù)音頻軌道生成一個混音輸入?yún)?shù)對象
+ (instancetype)audioMixInputParametersWithTrack:(nullable AVAssetTrack *)track;
// 設(shè)置音頻的質(zhì)量
/*
AVAudioTimePitchAlgorithmLowQualityZeroLatency 低質(zhì)量
AVAudioTimePitchAlgorithmTimeDomain 標準質(zhì)量
AVAudioTimePitchAlgorithmSpectral 高音質(zhì)
AVAudioTimePitchAlgorithmVarispeed 無損音質(zhì)
*/
@property (nonatomic, copy, nullable) AVAudioTimePitchAlgorithm audioTimePitchAlgorithm API_AVAILABLE(macos(10.10), ios(7.0), tvos(9.0), watchos(1.0));
// 設(shè)置混音中音量寺惫、音量漸變
- (void)setVolumeRampFromStartVolume:(float)startVolume toEndVolume:(float)endVolume timeRange:(CMTimeRange)timeRange;
- (void)setVolume:(float)volume atTime:(CMTime)time;
二、使用
簡單的使用方法;
1. 創(chuàng)建可變組合對象
AVMutableComposition *composition = [AVMutableComposition composition];
2. 創(chuàng)建視頻和音頻軌道
AVMutableCompositionTrack *videoCompositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *audioCompositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
3. 插入指定的視頻軌道和音頻軌道
AVAssetTrack *videoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] firstObject];
[videoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:videoTrack atTime:kCMTimeZero error:nil];
AVAssetTrack *audioAssetTrack = [[audioAsset tracksWithMediaType:AVMediaTypeAudio] firstObject];
[audioCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAssetTrack.duration) ofTrack:audioAssetTrack atTime:kCMTimeZero error:nil];
4. 導(dǎo)出組合后的媒體資源
NSURL *videoLocalPath = [NSURL fileURLWithPath:outputPath];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:composition presetName:AVAssetExportPresetMediumQuality];
exportSession.outputFileType = AVFileTypeQuickTimeMovie; // 導(dǎo)出資源類型
exportSession.outputURL = videoLocalPath; // 導(dǎo)出資源路徑
exportSession.shouldOptimizeForNetworkUse = YES; // 導(dǎo)出資源針對網(wǎng)絡(luò)加載優(yōu)化
[exportSession exportAsynchronouslyWithCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
//導(dǎo)出完成
if (exportSession.status == AVAssetExportSessionStatusCompleted ) {
if (successBlock) {
successBlock(exportSession.outputURL);
}
} else {
NSLog(@"視頻合成失敗--%@",exportSession.error);
if (successBlock) {
successBlock(exportSession.error);
}
}
});
}];
三锰镀、高級媒體組合
媒體資源組合時也可以對各個媒體資源自定義處理憾筏。
AVMutableVideoComposition
使用 AVMutableVideoComposition 可以直接處理視頻的軌道刨肃,通過它輸出視頻時還可以控制輸出的尺寸、縮放比例桅打、幀率站绪;
image
// 生成一個videoComposition
+ (AVMutableVideoComposition *)videoComposition;
// 幀率 必須設(shè)置否則crash
@property (nonatomic) CMTime frameDuration;
// 渲染尺寸 必須設(shè)置否則crash
@property (nonatomic) CGSize renderSize;
// 設(shè)置渲染縮放比
@property (nonatomic) float renderScale
// 媒體處理指令集
@property (nonatomic, copy) NSArray<id <AVVideoCompositionInstruction>> *instructions;
// 通過使用Core Animation對視頻做一些處理锰蓬,如添加水印等
@property (nonatomic, retain, nullable) AVVideoCompositionCoreAnimationTool *animationTool;
AVMutableVideoCompositionInstruction
視頻指令集
// 生成視頻指令集
+ (instancetype)videoCompositionInstruction;
// 設(shè)置指令集作用的timeRange
@property (nonatomic, assign) CMTimeRange timeRange;
// 設(shè)置媒體背景顏色
@property (nonatomic, retain, nullable) __attribute__((NSObject)) CGColorRef backgroundColor;
// 媒體layer層指令集
@property (nonatomic, copy) NSArray<AVVideoCompositionLayerInstruction *> *layerInstructions;
AVMutableVideoCompositionLayerInstruction
// 根據(jù)媒體軌道信息生成layer 指令對象
+ (instancetype)videoCompositionLayerInstructionWithAssetTrack:(AVAssetTrack *)track;
// 旋轉(zhuǎn)媒體資源
- (void)setTransform:(CGAffineTransform)transform atTime:(CMTime)time;
- - (void)setTransformRampFromStartTransform:(CGAffineTransform)startTransform toEndTransform:(CGAffineTransform)endTransform timeRange:(CMTimeRange)timeRange;
// 設(shè)置透明度
- (void)setOpacityRampFromStartOpacity:(float)startOpacity toEndOpacity:(float)endOpacity timeRange:(CMTimeRange)timeRange;
- (void)setOpacity:(float)opacity atTime:(CMTime)time;
-
AVMutableAudioMix
image
// 生成一個空的音頻管理對象
+ (instancetype)audioMix;
// 設(shè)置混合音頻處理集
@property (nonatomic, copy) NSArray<AVAudioMixInputParameters *> *inputParameters;
AVMutableAudioMixInputParameters
// 根據(jù)音頻軌道生成一個混音輸入?yún)?shù)對象
+ (instancetype)audioMixInputParametersWithTrack:(nullable AVAssetTrack *)track;
// 設(shè)置音頻的質(zhì)量
/*
AVAudioTimePitchAlgorithmLowQualityZeroLatency 低質(zhì)量
AVAudioTimePitchAlgorithmTimeDomain 標準質(zhì)量
AVAudioTimePitchAlgorithmSpectral 高音質(zhì)
AVAudioTimePitchAlgorithmVarispeed 無損音質(zhì)
*/
@property (nonatomic, copy, nullable) AVAudioTimePitchAlgorithm audioTimePitchAlgorithm API_AVAILABLE(macos(10.10), ios(7.0), tvos(9.0), watchos(1.0));
// 設(shè)置混音中音量、音量漸變
- (void)setVolumeRampFromStartVolume:(float)startVolume toEndVolume:(float)endVolume timeRange:(CMTimeRange)timeRange;
- (void)setVolume:(float)volume atTime:(CMTime)time;