11

I know you could merge multiple clips and create a single video by appending one after other using AVFoundation classes- AVURLAsset, AVMutableComposition, AVMutableCompositionTrack etc.

There are apps like 'Video-Joiner' that do that.

What I want to do is to juxtaposition 2 videos.

My app idea - SelfInterviewer please don't steal :)

First I record video 1 using front facing camera standing left to the frame. Then video 2 standing to the right. In video 1 ask a question and in video 2 I answer.

When I merge, it should appear like I am being interviewed by myself.

I am almost sure its not feasible in iOS, just wanted to confirm.

Also, if it's a no go, I would be interested in any server-side solutions - Upload the two videos and accomplish the same. I think adobe premiere can do it. Not sure if they have any public API.

Appreciate any ideas.

Thanks.

RK-
  • 12,099
  • 23
  • 89
  • 155
Naga
  • 113
  • 1
  • 1
  • 4
  • did u find a solution for this ?? – Mr.G Jan 14 '14 at 08:40
  • HI @Krishnan, How are you... I need your help, seriously... I'm stuck with somehow same Question... I have to show the video side by side after merging... Both Videos are showing BUT not showing properly like side by side... – Ahtazaz Apr 12 '19 at 10:03

2 Answers2

11

Yes it is possible to merge 2 videos:
1. Add both assets to an AVMutableComposition at start time 0.
2. Set the preferred Transform to the tracks, in this example scale transform.

    - (void) mergeVideos{
    ///... after getting hold or your assets....firstAsset, secondAsset

    AVMutableComposition* mixComposition = [AVMutableComposition composition];

    AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo 
                                                                      preferredTracfirst:kCMPersistentTracfirst_Invalid];
    [firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration) 
                        ofTrack:[[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] 
                         atTime:kCMTimeZero error:nil];

    AVMutableCompositionTrack *secondTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo 
                                                                       preferredTracfirst:kCMPersistentTracfirst_Invalid];

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

    [secondTrack setPreferredTransform:CGAffineTransformMakeScale(0.25f,0.25f)]; 

    //... export video here...

}
Vaisakh
  • 2,919
  • 4
  • 26
  • 44
LiveMixBox
  • 126
  • 1
  • 3
  • thanks, I will try it out. Unfortunately, I cannot vote on the answer as I am new to SO. – Naga Apr 27 '11 at 14:09
  • But, addMutableTrackWithMediaType: preferredTracfirst: API does not exists. Only the API addMutableTrackWithMediaType:preferredTrackID: exists. I tried with the API addMutableTrackWithMediaType:preferredTrackID itself and I am getting only the first video in the exported video. – spd Dec 14 '11 at 08:24
  • Sorry for late reply. YOu should use one MutableComposition tract instead of two. – Splendid Dec 15 '11 at 16:08
  • [secondTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, secondAsset.duration) ofTrack:[[secondAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil]; I m getting warning for the this and this is the warning - "Instance method '-setPreferredTransform:' not found (return type defaults to 'id')" - Any Idea why this is happening. – Sameera Chathuranga Oct 23 '12 at 05:53
  • i'm getting only the first video in the exported video but the duration is right. my `MutableComposition` is allocated once as stated. please help. – Arnlee Vizcayno Feb 02 '13 at 15:21
  • @Mr.G I have no idea, I can't get it to work.. I only see one of the video-tracks. Depending on which track was added first. If I add the second one first, even with the transform, it still shows as full-screen, and the other track is now not to be seen.. Did you find a way? – Sti Oct 25 '14 at 15:35
  • yes , check my link below , i used that to make a video on video – Mr.G Oct 26 '14 at 07:59
2

i found this link when im trying to do the same thing , But for me its not side by side , its video top of another video, You can do the same thing by this link

Video Manipulation

Mr.G
  • 1,275
  • 1
  • 18
  • 48
  • @Torongo are u sure , it woks , just in case https://abdulazeem.wordpress.com/2012/04/02/video-manipulation-in-ios-resizingmerging-and-overlapping-videos-in-ios/ – Mr.G Jan 18 '19 at 05:36
  • Hi @Torongo ... I need your help in this Tutorial... Although the above link is a great tutorial... But how can I show video side by side, same like a Karaoke App Or you can say that same like Duet Video... I will really appreciate your Guidance... Thanks – Ahtazaz Apr 15 '19 at 07:11
  • Hi @Mr.G, How are you... I need your help in this Tutorial... Although the above link is a great tutorial... But how can I show video side by side, same like a Karaoke App Or you can say that same like Duet Video... I will really appreciate your Guidance... Thanks – Ahtazaz Apr 15 '19 at 07:12
  • @Mr.Ahtazaz I did this in 4 years back , but i dont think its hard to do , i think u need to adjust the video view frame – Mr.G May 03 '19 at 10:09
  • @Mr.G How i can repeat one video until large video play? – Dixit Akabari Dec 04 '19 at 11:43
  • @Mr.G how? can you explain please. – Dixit Akabari Dec 05 '19 at 05:47
  • what do u have to do is to keep the play continues bind two video samples together and play . what do u mean by until large video play ? – Mr.G Dec 06 '19 at 07:32