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

Open
wangxiaola opened this issue Jan 3, 2018 · 3 comments
Open

How to add background music #415

wangxiaola opened this issue Jan 3, 2018 · 3 comments

Comments

@wangxiaola
Copy link

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

@dineshguptha
Copy link

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

@wangxiaola
Copy link
Author

@dineshguptha yes

@hiteshpatel1992
Copy link

hiteshpatel1992 commented Jul 13, 2020

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

}

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

3 participants