Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to add background audio into SCAssetExportSession? #403

Open
Dada08 opened this issue Jun 30, 2017 · 5 comments
Open

how to add background audio into SCAssetExportSession? #403

Dada08 opened this issue Jun 30, 2017 · 5 comments

Comments

@Dada08
Copy link

Dada08 commented Jun 30, 2017

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
Copy link

@Dada08
Copy link
Author

Dada08 commented Aug 15, 2017

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

@amofbnow
Copy link

amofbnow commented Dec 7, 2017

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
Copy link
Author

Dada08 commented Dec 8, 2017

@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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants