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 music #415

Open wangxiaola opened 6 years ago

wangxiaola commented 6 years ago

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

dineshguptha commented 5 years ago

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

wangxiaola commented 5 years ago

@dineshguptha yes

hiteshpatel1992 commented 3 years ago

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];
                                        }];

}];

}