SCRecorder icon indicating copy to clipboard operation
SCRecorder copied to clipboard

How to add background music

Open wangxiaola opened this issue 8 years ago • 3 comments

Sorry, this is my translation of English, please forgive me How do I add a background music to video

wangxiaola avatar Jan 03 '18 06:01 wangxiaola

Hello @wangxiaola , you got any solution for adding background music to the video. ?

dineshguptha avatar Dec 13 '18 14:12 dineshguptha

@dineshguptha yes

wangxiaola avatar Dec 15 '18 15:12 wangxiaola

If any wants to merger audio in the video Inside SCVideoPlayerViewController.m Inside Method - (void)saveToCameraRoll

Inside

[exportSession exportAsynchronouslyWithCompletionHandler:

//add Inside else if (error == nil) {

        NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"test"  ofType:@"mp3"];
        NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
        NSString* filename2 = [NSString stringWithFormat:@"FileName.mp4"];
        NSString* path2 = [NSTemporaryDirectory() stringByAppendingPathComponent:filename2];
        [self mergeAudio:soundFileURL withVideo:exportSession.outputUrl andSaveToPathUrl:path2];

//Method to Merger Audio And Video -(void) mergeAudio:(NSURL *) audioURL withVideo:(NSURL *) videoURL andSaveToPathUrl:(NSString *) savePath{

AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audioURL options:nil];
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoURL options:nil];

AVMutableComposition* mixComposition = [AVMutableComposition composition];




AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                                    preferredTrackID:kCMPersistentTrackID_Invalid];




[compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                                    ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                                     atTime:kCMTimeZero error:nil];

AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                               preferredTrackID:kCMPersistentTrackID_Invalid];


//[compositionVideoTrack setPreferredTransform:CGAffineTransformMakeRotation(M_PI/2)];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                               ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                                atTime:kCMTimeZero error:nil];

AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition
                                                                      presetName:AVAssetExportPresetPassthrough];

NSURL    *savetUrl = [NSURL fileURLWithPath:savePath];

if ([[NSFileManager defaultManager] fileExistsAtPath:savePath])
{
    [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil];
}

_assetExport.outputFileType = @"com.apple.quicktime-movie";

_assetExport.outputURL = savetUrl;
_assetExport.shouldOptimizeForNetworkUse = YES;

[_assetExport exportAsynchronouslyWithCompletionHandler:
 ^(void ) {
    NSLog(@"fileSaved !");
    
        
              ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
                                        [library writeVideoAtPathToSavedPhotosAlbum:savetUrl completionBlock:^(NSURL *assetURL, NSError *error){
                                           // NSLog(@"save completed");
                                           // [[NSFileManager defaultManager] removeItemAtPath:savetUrl error:nil];
                                        }];
   
}];

}

hiteshpatel1992 avatar Jul 13 '20 13:07 hiteshpatel1992