gpt4 book ai didi

ios - 使用 AVMutableComposition 合并视频导致没有音频

转载 作者:可可西里 更新时间:2023-11-01 06:24:33 26 4
gpt4 key购买 nike

我有一个包含视频 NSURL 列表的 NSArray,我想将它们合并在一起以进行一个长的编译。问题是,当我使用下面的代码时,视频合并但没有音频。

- (IBAction)buildVideo {

// 1 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances.
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];

AVMutableCompositionTrack *track = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
preferredTrackID:kCMPersistentTrackID_Invalid];

int i = 0;

for (id object in movieArray) {

AVAsset *asset = [AVAsset assetWithURL:object];

if(i == 0){

[track insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];


}else {

[track insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:[mixComposition duration] error:nil];

}

i = i + 1;

}

// 4 - Get path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:@"mergeVideo-%d.mov",arc4random() % 1000]];
NSURL *url = [NSURL fileURLWithPath:myPathDocs];
// 5 - Create exporter
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition
presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL=url;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;
[exporter exportAsynchronouslyWithCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self exportDidFinish:exporter];
});
}];

最佳答案

您没有获得音频的原因是您没有添加音轨。您需要创建一个类型为 AVMediaTypeAudio 的附加 AVMutableCompositionTrack:

AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];

AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
preferredTrackID:kCMPersistentTrackID_Invalid];

AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
preferredTrackID:kCMPersistentTrackID_Invalid];

并将源音频和视频轨道的时间范围插入到两个合成轨道中:

CMTime insertTime = kCMTimeZero;

for (id object in movieArray) {

AVAsset *asset = [AVAsset assetWithURL:object];

CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);

[videoTrack insertTimeRange:timeRange
ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
atTime:insertTime
error:nil];

[audioTrack insertTimeRange:timeRange
ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
atTime:insertTime
error:nil];

insertTime = CMTimeAdd(insertTime,asset.duration);
}

关于ios - 使用 AVMutableComposition 合并视频导致没有音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24267514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com