rFlex / SCRecorder

iOS camera engine with Vine-like tap to record, animatable filters, slow motion, segments editing
Apache License 2.0
3.06k stars 583 forks source link

how to add background audio into SCAssetExportSession? #403

Open Dada08 opened 7 years ago

Dada08 commented 7 years ago

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?

lmick002 commented 6 years ago

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

Dada08 commented 6 years ago

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

amofbnow commented 6 years ago

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));
Dada08 commented 6 years ago

@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.
wangxiaola commented 6 years ago

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