Open Dada08 opened 7 years ago
@Dada08 you might want to look into this https://stackoverflow.com/questions/40404634/merging-audio-and-video-swift
@lmick002 thank you. I've done it with 'AVAssetExportSession'
I have the same problem, but i can't switch to classic AVAssetExportSession because i need to use filters. This is my code, what i want is to record a video without audio (already done) and then add an audio soundtrack to the recorded video.
SCAssetExportSession *exportSession = [[SCAssetExportSession alloc] initWithAsset:self.recordSession.assetRepresentingSegments]; exportSession.videoConfiguration.filter = currentFilter; exportSession.videoConfiguration.preset = SCPresetHighestQuality; exportSession.audioConfiguration.preset = SCPresetHighestQuality; exportSession.videoConfiguration.maxFrameRate = 35; exportSession.outputUrl = self.recordSession.outputUrl; exportSession.outputFileType = AVFileTypeMPEG4; exportSession.delegate = self; exportSession.contextType = SCContextTypeAuto;
NSString *tempFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"tmp.m4a"];
NSFileManager *manager = [NSFileManager defaultManager];
[manager createFileAtPath:tempFilePath contents:self.audio_data attributes:nil];
AVURLAsset *audioAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:tempFilePath] options:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[manager removeItemAtPath:tempFilePath error:nil];
});
CMTimeRange audioTimeRange = CMTimeRangeMake(kCMTimeZero, self.recordSession.duration);
AVMutableComposition *comosition = [AVMutableComposition composition];
AVMutableCompositionTrack *audioTrack = [comosition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *audioAssetTrack = [[audioAsset tracksWithMediaType:AVMediaTypeAudio] firstObject];
[audioTrack insertTimeRange:audioTimeRange ofTrack:audioAssetTrack atTime:kCMTimeZero error:nil];
AVMutableAudioMixInputParameters *parameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:audioAssetTrack];
[parameters setVolume:1.0 atTime:kCMTimeZero];
AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
audioMix.inputParameters = @[parameters];
exportSession.audioConfiguration.enabled=YES;
exportSession.audioConfiguration.audioMix=audioMix;
NSLog(@"seconds = %f", CMTimeGetSeconds(audioAsset.duration));
@amofbnow you can try this method by two steps.
Hello, can you tell me the video to add background music to solve?
I'm trying to add a song into SCAssertExportSession, following code does not work.
is it possible to add default audio into SCAssetExportSession?