如何使用Objective-C iOS以編程方式向對話視頻添加配音評論?我嘗試使用AVMutableComposition類將錄製音頻合併到對話視頻中,但似乎將它們混合在一起(視頻背景音合併後完全丟失),無法使用它進行配音。如何在ios中以編程方式將視頻添加到視頻中?
有人可以幫忙嗎?
這裏是我使用的代碼:
-(void)mergeAudio:(NSString *)audioPath toVideo:(NSString *)videoPath
{
AVURLAsset* videoAsset;
AVURLAsset* audioAsset;
AVMutableComposition* mixComposition = [AVMutableComposition composition];
NSURL *audio_url = [NSURL fileURLWithPath:audioPath];
audioAsset = [[AVURLAsset alloc]initWithURL:audio_url options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];
NSURL *video_url = [NSURL fileURLWithPath:videoPath];
videoAsset = [[AVURLAsset alloc]initWithURL:video_url options:nil];
CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,audioAsset.duration);
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *outputFilePath = [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"dub.mov"]];
NSURL *outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath]){
[[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];
}
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputURL = outputFileUrl;
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void) {
dispatch_async(dispatch_get_main_queue(), ^{
// Do export finish stuff
});
}];
}
「我嘗試使用AVMutableComposition類將錄製音頻合併到對話視頻中,但似乎將它們混合在一起(視頻背景聲音在合併後完全丟失)」然後你做錯了,因爲AVMutableVideoComposition正是你如何做到這一點的,如果您記得使用視頻背景音,它不會丟失視頻背景音。如果您需要特定幫助,請顯示您的代碼。 – matt
@matt我更新了代碼,我使用iphone攝像頭捕捉視頻,然後將該視頻與mp3歌曲合併,結果是一個帶有沒有原始視頻聲音的mp3歌曲的新視頻文件,您能告訴我哪裏出了錯? – UmbalaAZ
是的,我當然可以。 :) – matt