Please help me to merge 2 separate mp4 videos side by side as like duet video feature of musically (TikTok) app with swift for iOS app like below screenshot.
Asked
Active
Viewed 2,194 times
5
-
https://www.raywenderlich.com/188034/how-to-play-record-and-merge-videos-in-ios-and-swift – Prashant Tukadiya Jul 19 '18 at 12:49
-
Hi @Touhid, i really need you help regarding this Question, Have you found this answer? How you accomplish this GOAL? Right now i'm playing 2 videos separately side by side... BUT how can i play 2 videos at ONCE, SIDE BY SIDE, NOT SEPARATELY? – Ahtazaz Apr 11 '19 at 06:27
-
Hello @Mr.Ahtazaz I solved this problem in iOS and Android both. – Touhid Apr 16 '19 at 16:19
-
Hi @Touhid, it sounds very very very GREAT for me... BUT how you are able to do this??? I have also done using AVFoundation But the problem is Both Duet videos are not showing Properly... 1 Video is showing as a OVERLAY to 2nd video... Looking for your BIG help... Thanks mate... – Ahtazaz Apr 16 '19 at 16:22
-
Hello @Mr.Ahtazaz, In android I found ffmpeg library which worked perfectly. In iOS I send both video to server and used ffmpeg on linux server environment. After recording user is watching in separate player and after uploading to server, merge both videos by ffmpeg and save to server. I also found ffmpeg for iOS (search on cocoapods) but did not found perfect one for my requirement. It successfully merged video but did not get good quality video. You can search on cocoapods for ffmpeg framework, You will get lots of option, find which one is perfect for you by testing one by one. – Touhid Apr 16 '19 at 16:30
-
Can you come to Skype just for a short Time... I'm stuck in from past 3 days almost?? – Ahtazaz Apr 16 '19 at 16:38
-
Yeah, add 1010 after my name and search – Touhid Apr 17 '19 at 18:07
-
@Mr. Ahtazaz Sir you found any solution for it ?? – Davender Verma Oct 30 '19 at 12:46
-
@DavenderVermaSoni No Sir. – Ahtazaz Oct 31 '19 at 06:13
-
@Mr.Ahtazaz. I am working on it and update you if found any solution . Thanks – Davender Verma Oct 31 '19 at 07:58
-
@DavenderVermaSoni sure, i will wait... – Ahtazaz Oct 31 '19 at 09:32
-
@Touhid. Sir may you please provide the code for merging two video using FFmpeg iOS – Davender Verma Nov 20 '19 at 07:53
-
1@DavenderVermaSoni You can try this code to merge 2 video: "-i firstVideoPath.mp4 -i secondVideoPath.mp4 -filter_complex hstack -c:v h264 outputVideoPath.mp4" . Remember that both video must have to be same dimension and same format. You can try this framework also: https://github.com/tanersener/mobile-ffmpeg – Touhid Nov 21 '19 at 10:46
-
@Touhid. Sir I have taken 2 mov. format video but nothing happened – Davender Verma Nov 23 '19 at 05:27
-
let filePath1 = Bundle.main.url(forResource: "movie1", withExtension: "mov") let filePath2 = Bundle.main.url(forResource: "movie2", withExtension: "mov") let command = "-i \(String(describing: filePath1!)) -i \(String(describing: filePath2!)) -filter_complex hstack -c:v h264 outputVideoPath.mov" MobileFFmpeg.execute(command) let resultCode = MobileFFmpeg.getLastReturnCode() print("\(resultCode)") – Davender Verma Nov 23 '19 at 09:48
-
@DavenderVermaSoni Hi, I found a solution for iOS. Here is an example project what you are searching for, you have to re initialize pod to run the project because I did not upload Pod directory as there are some big files, Git link is: https://github.com/TouhidApps/iOS-Projects/tree/master/FFMpeg-Example-iOS – Touhid Nov 24 '19 at 18:56
-
@Touhid Sir What is the command which convert an mp4 video in same format extension. I want to convert mp4 video to mov – Davender Verma Nov 25 '19 at 07:32
-
@Touhid Sir . I am facing dimension issue . Input 1 height 960 does not match input 0 height 480. in ios swift . How to use command for this – Davender Verma Nov 25 '19 at 12:49
-
@Touhid. MobileFFmpeg.execute("-i \(self.videoFilePath1)) -i \(String(describing: filePath2!)) -filter_complex hstack -c:v h264 \(destination)") – Davender Verma Nov 26 '19 at 11:40
-
in the above the first one is the path created by video recorded and second one is the video already exist coming from server url . – Davender Verma Nov 26 '19 at 11:42
-
@DavenderVermaSoni You have to download server video in local storage before merge. Both video need to exist in local storage and video path will be local path. – Touhid Nov 26 '19 at 11:51
-
@Touhid Sir the library code is not working when I merge path generated by video and already exist video – Davender Verma Nov 26 '19 at 11:52
-
@Touhid Sir video exist in by project bundle – Davender Verma Nov 26 '19 at 11:53
-
@Touhid. Sir may you please suggest the solution for above issue ? – Davender Verma Nov 27 '19 at 04:32
-
@DavenderVermaSoni without watching your code I can't understand what is the problem in your code. Tips: See error log in Xcode. FFMpeg is a popular library and you can google it for the commend. – Touhid Nov 27 '19 at 08:57
-
@Touhid. Sir let filePath2 = Bundle.main.url(forResource: "hate-me-_arishfakhan_", withExtension: "mp4") this is my first video. and self.videoFilePath1 is the path as your example. Not able to merge these two videos – Davender Verma Nov 27 '19 at 09:36
-
@Touhid. Sir the below command is not working MobileFFmpeg.execute("-i \(self.videoFilePath1) -i \(String(describing: filePath2!)) -filter_complex hstack \(destination)") – Davender Verma Nov 27 '19 at 09:38
-
@DavenderVermaSoni did you debug it? Did you see log? what is inside self.videoFilePath1, filePath2 & destination? – Touhid Nov 27 '19 at 09:41
-
@Touhid. Sir in self.videoFilePath1 consist of local path for created video and filepathvideo is video from url and destination coati the output for command – Davender Verma Nov 27 '19 at 10:16
-
https://trac.ffmpeg.org/wiki/Create%20a%20mosaic%20out%20of%20several%20input%20videos – Davender Verma Nov 27 '19 at 10:40
-
@Touhid sir may you update your repository with mosaic out of several input videos one from Camera and other from url – Davender Verma Nov 27 '19 at 12:35
-
@Mr.Ahtazaz. use the code the post below – Davender Verma Jan 07 '20 at 09:19
2 Answers
-1
you can achieve this by using two UIVIEW,and assign each one to swift class like this.
import UIKit
import AVKit
import AVFoundation
class videoplaying: UIView {
override static var layerClass: AnyClass {
return AVPlayerLayer.self;
}
var playerlayer: AVPlayerLayer{
return layer as! AVPlayerLayer;
}
var player: AVPlayer?{
get{
return playerlayer.player
}
set {
playerlayer.player = newValue
}
}
var playetitem: AVPlayerItem?
}

Abd
- 497
- 4
- 14
-
I actually want to merge 2 videos because I also needs share feature. Separate video playing at once will not fulfill my requirement. – Touhid Jul 19 '18 at 12:32
-
Hello @Touhid, I am facing same issue have you got any solution for merging it as a two parallel video. Please guide if you got the solution. – Ashwini Salunkhe Aug 18 '20 at 08:00
-1
let commandValue = "-y -i \(self.videoFilePath1) -i \(self.videoFilePath2) -filter_complex nullsrc=size=\(value4*2)*\(value3)[base];[\(0):v]setpts=PTS-STARTPTS,scale=\(value4)*\(value3)[upperleft];[\(1):v]setpts=PTS-STARTPTS,scale=\(value4)*\(value3)[upperright];[base][upperleft]overlay=shortest=\(1):x=\(0):y=\(0)[tmp1];[tmp1][upperright]overlay=shortest=\(1):x=\(value4):y=\(0) -c:a copy -c:v libx264 -crf 0 -strict experimental -preset ultrafast -t \(self.sendDuration) \(destination)"
MobileFFmpeg.execute(commandValue)

Davender Verma
- 503
- 2
- 12