SCRecorder icon indicating copy to clipboard operation
SCRecorder copied to clipboard

how to add background audio into SCAssetExportSession?

Open Dada08 opened this issue 7 years ago • 5 comments

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 avatar Jun 30 '17 02:06 Dada08

@Dada08 you might want to look into this https://stackoverflow.com/questions/40404634/merging-audio-and-video-swift

lmick002 avatar Jul 28 '17 15:07 lmick002

@lmick002 thank you. I've done it with 'AVAssetExportSession'

Dada08 avatar Aug 15 '17 07:08 Dada08

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 avatar Dec 07 '17 18:12 amofbnow

@amofbnow you can try this method by two steps.

  1. use SCAssetExportSession to export you video with filters (without audio), then you will get the output video and its url.
  2. use AVAssetExportSession to mix the output video and your audio.

Dada08 avatar Dec 08 '17 08:12 Dada08

Hello, can you tell me the video to add background music to solve?

wangxiaola avatar Jan 03 '18 06:01 wangxiaola