gpt4 book ai didi

ios - AVMutableComposition 去除视频音轨

转载 作者:行者123 更新时间:2023-11-29 13:17:29 24 4
gpt4 key购买 nike

我一直在尝试使用 AVMutableComposition 将音频(录制的)与视频(.mp4)混合,这意味着我需要 2 个文件在混合后并行播放,这是我使用的代码:temporaryRecFile 为录制文件url路径audiosURL 为视频文件url路径

NSURL *audiosURL =[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"dam3.mp4"] ofType:nil]];

NSLog(@"SOMEDATA IS THERE ");
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:temporaryRecFile options:nil];
AVURLAsset* audio2Asset = [[AVURLAsset alloc]initWithURL:audiosURL options:nil];

AVMutableComposition* mixComposition = [AVMutableComposition composition];

AVMutableCompositionTrack *compositionCommentary2Track = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionCommentary2Track insertTimeRange:CMTimeRangeMake(kCMTimeZero, audio2Asset.duration) ofTrack:[[audio2Asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];

NSLog(@"audio =%@",audioAsset);
AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];


AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetPassthrough];

NSString* videoName = @"export.mov";

NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];
NSURL *exportUrl = [NSURL fileURLWithPath:exportPath];

if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath])
{
[[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
}

_assetExport.outputFileType = AVFileTypeQuickTimeMovie;
//@"com.apple.quicktime-movie";
NSLog(@"file type %@",_assetExport.outputFileType);
_assetExport.outputURL = exportUrl;
_assetExport.shouldOptimizeForNetworkUse = YES;



[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void )
{
switch (_assetExport.status)
{
case AVAssetExportSessionStatusCompleted:
// export complete

NSLog(@"Export Complete");
// From Here I want play movie using MPMoviePlayerController.<<<---------
NSString *fileNamePath = @"sound_record.mov";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *oldappSettingsPath = [documentsDirectory stringByAppendingPathComponent:fileNamePath];


if ([[NSFileManager defaultManager] fileExistsAtPath:oldappSettingsPath]) {

NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath: oldappSettingsPath error:NULL];

}
NSURL *documentDirectoryURL = [NSURL fileURLWithPath:oldappSettingsPath];
[[NSFileManager defaultManager] copyItemAtURL:exportUrl toURL:documentDirectoryURL error:nil];
[audioAsset release];
[audio2Asset release];
[_assetExport release];
[self performSelectorOnMainThread:@selector(playVideo:) withObject:documentDirectoryURL waitUntilDone:NO];

break;
case AVAssetExportSessionStatusFailed:
NSLog(@"Export Failed");
NSLog(@"ExportSessionError: %@", [_assetExport.error localizedDescription]);

// export error (see exportSession.error)
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export Failed");
NSLog(@"ExportSessionError: %@", [_assetExport.error localizedDescription]);

// export cancelled
break;

}
}
];

因此,导出的视频成功播放录制的音频和视频,但是视频的音轨被删除了,所以导出的.mov只播放录制的音频文件和视频的图像,没有它的声音,为什么会这样?

最佳答案

您必须将视频中的音频添加为单独的轨道。因此,您将拥有一个视频轨道和两个音频轨道。在您的代码中,您没有添加视频中的音频。

关于ios - AVMutableComposition 去除视频音轨,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15338824/

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