注意: - 并排合并视频而不会丢失视频质量
我认为这是一个非常非常重要的问题,经过大量的搜索和谷歌搜索,没有找到任何与本课题相关的有用材料。
我正在开发一个项目,我需要在一个文件中并排合并视频。
我使用AVFoundation完成了合并视频但问题是FIRST视频显示为覆盖到第二个视频(不像SMULE应用程序/卡拉OK应用程序或Tiktok应用程序那样正确合并)。
func mergeVideosFilesWithUrl(savedVideoUrl: URL, newVideoUrl: URL, audioUrl:URL)
{
let savePathUrl : NSURL = NSURL(fileURLWithPath: NSHomeDirectory() + "/Documents/camRecordedVideo.mp4")
do { // delete old video
try FileManager.default.removeItem(at: savePathUrl as URL)
} catch { print(error.localizedDescription) }
var mutableVideoComposition : AVMutableVideoComposition = AVMutableVideoComposition()
var mixComposition : AVMutableComposition = AVMutableComposition()
let aNewVideoAsset : AVAsset = AVAsset(url: newVideoUrl)
let asavedVideoAsset : AVAsset = AVAsset(url: savedVideoUrl)
let aNewVideoTrack : AVAssetTrack = aNewVideoAsset.tracks(withMediaType: AVMediaType.video)[0]
let aSavedVideoTrack : AVAssetTrack = asavedVideoAsset.tracks(withMediaType: AVMediaType.video)[0]
let mutableCompositionNewVideoTrack : AVMutableCompositionTrack = mixComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)!
do{
try mutableCompositionNewVideoTrack.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: aNewVideoAsset.duration), of: aNewVideoTrack, at: CMTime.zero)
}catch { print("Mutable Error") }
let mutableCompositionSavedVideoTrack : AVMutableCompositionTrack = mixComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)!
do{
try mutableCompositionSavedVideoTrack.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: asavedVideoAsset.duration), of: aSavedVideoTrack , at: CMTime.zero)
}catch{ print("Mutable Error") }
let mainInstruction = AVMutableVideoCompositionInstruction()
mainInstruction.timeRange = CMTimeRangeMake(start: CMTime.zero, duration: CMTimeMaximum(aNewVideoAsset.duration, asavedVideoAsset.duration) )
let newVideoLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: mutableCompositionNewVideoTrack)
let newScale : CGAffineTransform = CGAffineTransform.init(scaleX: 0.7, y: 0.7)
let newMove : CGAffineTransform = CGAffineTransform.init(translationX: 230, y: 230)
newVideoLayerInstruction.setTransform(newScale.concatenating(newMove), at: CMTime.zero)
let savedVideoLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: mutableCompositionSavedVideoTrack)
let savedScale : CGAffineTransform = CGAffineTransform.init(scaleX: 1.2, y: 1.5)
let savedMove : CGAffineTransform = CGAffineTransform.init(translationX: 0, y: 0)
savedVideoLayerInstruction.setTransform(savedScale.concatenating(savedMove), at: CMTime.zero)
mainInstruction.layerInstructions = [newVideoLayerInstruction, savedVideoLayerInstruction]
mutableVideoComposition.instructions = [mainInstruction]
mutableVideoComposition.frameDuration = CMTimeMake(value: 1, timescale: 30)
mutableVideoComposition.renderSize = CGSize(width: 1240 , height: self.camPreview.frame.height)
finalPath = savePathUrl.absoluteString
let assetExport: AVAssetExportSession = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)!
assetExport.videoComposition = mutableVideoComposition
assetExport.outputFileType = AVFileType.mov
assetExport.outputURL = savePathUrl as URL
assetExport.shouldOptimizeForNetworkUse = true
assetExport.exportAsynchronously { () -> Void in
switch assetExport.status {
case AVAssetExportSession.Status.completed:
print("success")
case AVAssetExportSession.Status.failed:
print("failed \(assetExport.error)")
case AVAssetExportSession.Status.cancelled:
print("cancelled \(assetExport.error)")
default:
print("complete")
}
}
}
而我想要的是什么
因为我不知道制作SIDE BY SIDE VIDEO / DUET VIDEO的最佳方法是什么......至于现在,我使用过AVFoundation。我没有使用任何第三方框架或任何POD。
我想问一下,实施这个的最佳方法是什么?视频应该在服务器端还是应用程序上合并?我应该使用哪种方法?
任何帮助都将受到高度赞赏。谢谢
为了实现这一点,我将创建一个包含2个轨道的新AVMutableComposition
对象,并在每个轨道上设置变换以将它们并排放置:
let composition = AVMutableComposition(urlAssetInitializationOptions: <your options>)
let videoTrackA = composition.addMutableTrack(withMediaType:.video, preferredTrackID:kCMPersistentTrackID_Invalid);
let videoTrackB = composition.addMutableTrack(withMediaType:.video, preferredTrackID:kCMPersistentTrackID_Invalid);
videoTrackA.preferredTransform = CGAffineTransform(translationX: <yourX_for_A>, y:0.0)
videoTrackB.preferredTransform = CGAffineTransform(translationX: <yourX_for_B>, y:0.0)
然后。保存使用:
let exporter = AVAssetExportSession(asset:<yourAsset>, presetName:<yourPresetName>)
exporter.exportAsynchronously(completionHandler: <yourCompletionHandler>)
(未测试Swift代码)。
事实上,AVAssetExportSession
是出于简单的需求,而且对你的情况来说太简单了。
你必须使用AVAssetWriter。
你将AVAssetWriterInput添加到你的AVAssetWriter
。
您可以使用qazxsw poi属性配置qazxsw poi的变换。
然后,使用AVAssetWriterInput
调用向CMSampleBuffer(每个图像缓冲区)提供transform
。
有关详细示例,请参阅完整的Apple文档:AVAssetWriterInput