SCRecorder
SCRecorder copied to clipboard
how to add background audio into SCAssetExportSession?
I'm trying to add a song into SCAssertExportSession, following code does not work.
AVMutableComposition *comosition = [AVMutableComposition composition];
NSURL *audioInputUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Sin" ofType:@"mp3"]];
AVURLAsset *audioAsset = [[AVURLAsset alloc] initWithURL:audioInputUrl options:nil];
CMTimeRange audioTimeRange = CMTimeRangeMake(kCMTimeZero, _recordSession.duration);
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];
AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
audioMix.inputParameters = @[parameters];
SCAssetExportSession *exportSession = [[SCAssetExportSession alloc] initWithAsset:self.recordSession.assetRepresentingSegments];
exportSession.videoConfiguration.preset = SCPresetHighestQuality;
exportSession.audioConfiguration.preset = SCPresetHighestQuality;
exportSession.videoConfiguration.maxFrameRate = 35;
exportSession.audioConfiguration.audioMix = audioMix;
exportSession.outputUrl = self.recordSession.outputUrl;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.delegate = self;
exportSession.contextType = SCContextTypeAuto;
self.exportSession = exportSession;
is it possible to add default audio into SCAssetExportSession?
@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.
- use SCAssetExportSession to export you video with filters (without audio), then you will get the output video and its url.
- use AVAssetExportSession to mix the output video and your audio.
Hello, can you tell me the video to add background music to solve?