0

Merging Code-

AVAsset *firstAsset=[AVAsset assetWithURL:urlIntroVideo]; AVAsset *secondAsset=[AVAsset assetWithURL:recordedVideoUrl];

if (firstAsset !=nil && secondAsset!=nil) { //[[AppDelegate Getdelegate] showIndicator]; // 1 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances. AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init]; // 2 - Video track

/********************************************************************************

 --------------->>      VIDEO MERGING TRACK              <<--------------------

 ********************************************************************************/



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

NSArray *videoTracks = [NSArray arrayWithArray: [firstAsset tracksWithMediaType:AVMediaTypeVideo]];
NSLog(@"Video Tracks count  1st Assest=> %ld",[videoTracks count]);


NSArray *audioTracks = [NSArray arrayWithArray: [firstAsset tracksWithMediaType:AVMediaTypeAudio]];
NSLog(@"Auido Tracks count => %ld",[audioTracks count]);



[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration)
                    ofTrack:[[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]

atTime:kCMTimeZero error:nil];

[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, secondAsset.duration)
                    ofTrack:[[secondAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]

atTime:firstAsset.duration error:nil];

NSString *bundleDirectory = [[NSBundle mainBundle] bundlePath];

NSString *potrait_intro = [bundleDirectory stringByAppendingPathComponent:@"silent08s.mp3"];

NSURL    *potrait_intro_url = [NSURL fileURLWithPath:potrait_intro];


AVAsset *audioAssest=[AVAsset assetWithURL:potrait_intro_url];


AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio

preferredTrackID:kCMPersistentTrackID_Invalid];

NSArray *audioTracksSilent = [NSArray arrayWithArray: [audioAssest tracksWithMediaType:AVMediaTypeAudio]];


[audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAssest.duration)
                    ofTrack:([audioTracksSilent count]>0)?[audioTracksSilent objectAtIndex:0]:nil
                     atTime:kCMTimeZero
                      error:nil];



NSArray *audioTracks2 = [NSArray arrayWithArray: [secondAsset tracksWithMediaType:AVMediaTypeAudio]];
NSLog(@"Auido Tracks count => %ld",[audioTracks2 count]);

[audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, secondAsset.duration)
                    ofTrack:[[secondAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                     atTime:audioAssest.duration
                      error:nil];

//



//  NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSHomeDirectory()
                                stringByAppendingPathComponent:@"Documents"];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:[NSString

stringWithFormat:@"final_merged_video-%d.mov",arc4random() % 1000]];

NSURL *url = [NSURL fileURLWithPath:myPathDocs];
// 5 - Create exporter
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition
                                                                  presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL=url;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;
[exporter exportAsynchronouslyWithCompletionHandler:^{
    dispatch_async(dispatch_get_main_queue(), ^{

        [[AppDelegate Getdelegate] hideIndicator];

        [self exportDidFinish:exporter];
    });
}]; }

Please help me out on this

Raman Srivastava
  • 396
  • 1
  • 14

1 Answers1

0

You probably loose the rotation info on your AVAsset when you merged both videos and/or your server is unable to process it. What you need to do is getting the rotation info (you can use this for example : https://gist.github.com/lukabernardi/5020724 ), and then rotate the video accordingly BEFORE sending it to the server.

thibaut noah
  • 1,474
  • 2
  • 11
  • 39
  • thanx for your answer it helps me getting the orientation but now how to rotate that AVasset by 90 degree? – Raman Srivastava Feb 28 '17 at 10:46
  • You can follow this answer to help fix your issue : http://stackoverflow.com/questions/12136841/avmutablevideocomposition-rotated-video-captured-in-portrait-mode – thibaut noah Feb 28 '17 at 10:50
  • It doesnt woked!! After merging two videos the recorded one is rotated by 90 degree and other one's orientation is fine – Raman Srivastava Feb 28 '17 at 11:16
  • You need to be more specific about your issue – thibaut noah Feb 28 '17 at 13:29
  • I am merging two videos in potrait mode.The first video is a normal mp4 video in potrait video and other one is the video which i am recording fron my camera in potrait from.Now when i merge both video the first video orientation is fine but the second video is rotated by 90 degree – Raman Srivastava Feb 28 '17 at 19:13
  • You are not specific. At which point did you transform the video? Why one did you transform? Is this your original issue or what is happening after your latest tries? – thibaut noah Mar 01 '17 at 10:07