大佬教程收集整理的这篇文章主要介绍了我正在尝试合并两个视频,其中一个是预先选择的视频,另一个是来自前置摄像头的视频,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我可以合并两个视频并导出它们,但是我要解决的问题是第二个视频(前置摄像头)轨道的方向/位置取决于第一个。无论第一个视频(用户内容)如何,我都希望能够获得一致的输出
let reactionAsset = AVURLAsset(url: reactionURL!)
let vIDeoAsset = AVURLAsset(url: vIDeoURL!)
let composition = AVMutableComposition()
guard
let firstTrack = composition.addMutableTrack(withMediaType: .vIDeo,preferredTrackID: kCMPersistentTrackID_InvalID)
else{
print("Something is wrong with the asset.")
onComplete(.failure(dIDNotFinish.dIDNotCompletE))
return
}
print("loaded first track")
do{
let timerange = CMTimeRange(start: .zero,duration: vIDeoAsset.duration)
try firstTrack.insertTimeRange(timerange,of: vIDeoAsset.tracks(withMediaType: .vIDeo)[0],at: .zero)
print("inserted first timerange")
}catch{
print("Failed to load first track")
onComplete(.failure(dIDNotFinish.dIDNotCompletE))
return
}
guard
let firstAudTrack = composition.addMutableTrack(withMediaType: .au@R_301_6901@,preferredTrackID: kCMPersistentTrackID_InvalID)
else{
print("Something is wrong with the au@R_301_6901@ asset.")
onComplete(.failure(dIDNotFinish.dIDNotCompletE))
return
}
print("loaded first au@R_301_6901@ track")
do{
let timerange = CMTimeRange(start: .zero,duration: vIDeoAsset.duration)
try firstAudTrack.insertTimeRange(timerange,of: vIDeoAsset.tracks(withMediaType: .au@R_301_6901@)[0],at: .zero)
print("inserted first au@R_301_6901@ timerange")
}catch{
onComplete(.failure(dIDNotFinish.dIDNotCompletE))
print("Failed to load first au@R_301_6901@ track")
return
}
guard
let secondTrack = composition.addMutableTrack(withMediaType: .vIDeo,preferredTrackID: kCMPersistentTrackID_InvalID)
else{
print("something is wrong with second asset")
onComplete(.failure(dIDNotFinish.dIDNotCompletE))
return
}
print("loaded second track")
do {
let second_timerange = CMTimeRange(start: .zero,duration: vIDeoAsset.duration)
print(reactionAsset.tracks)
try secondTrack.insertTimeRange(second_timerange,of: reactionAsset.tracks(withMediaType: .vIDeo)[0],at: .zero)
print("inserted second timerange")
}
catch{
print("Failed to load second track")
onComplete(.failure(dIDNotFinish.dIDNotCompletE))
return
}
guard
let secondAudTrack = composition.addMutableTrack(withMediaType: .au@R_301_6901@,preferredTrackID: kCMPersistentTrackID_InvalID)
else{
print("something is wrong with second asset")
onComplete(.failure(dIDNotFinish.dIDNotCompletE))
return
}
print("inserted second aud track")
do {
let second_timerange = CMTimeRange(start: .zero,duration: vIDeoAsset.duration)
try secondAudTrack.insertTimeRange(second_timerange,of: reactionAsset.tracks(withMediaType: .au@R_301_6901@)[0],at: .zero)
print("added second au@R_301_6901@ time range")
}
catch{
print("Failed to load second track")
onComplete(.failure(dIDNotFinish.dIDNotCompletE))
return
}
let maininstruction = AVMutableVIDeoCompositioninstruction()
maininstruction.timeRange = CMTimeRange(start: .zero,duration: vIDeoAsset.duration)
let firstinstruction = AVMutableVIDeoCompositionLayerinstruction(assetTrack: firstTrack)
let secondinstruction = AVMutableVIDeoCompositionLayerinstruction(assetTrack: secondTrack)
let scaletransform = CGAffinetransform(scaleX: 0.1,y: 0.1)
let tranlate = scaletransform.translatedBy(x: 2400,y: 300)
let rotatetransform = tranlate.rotated(by: .pi/2)
secondinstruction.settransform(rotatetransform,at: .zero)
//secondinstruction.settransform(secondTrack.preferredtransform,at: .zero)
//secondinstruction.setCropRectangle(CGRect(origin: CGPoint(x: 50,y: 50),size: CGSize(wIDth:70,height: 120)),at: .zero)
//secondinstruction.setCropRectangle(CGRect(x: 200,y: 200,wIDth: 130,height: 180),at: .zero)
maininstruction.layerinstructions = [secondinstruction,firstinstruction]
maininstruction.BACkgroundcolor = UIcolor.clear.cgcolor
let mainComposition = AVMutableVIDeoComposition()
mainComposition.instructions = [maininstruction]
mainComposition.frameDuration = CMTimeMake(value: 1,timescale: 30)
mainComposition.renderSize = CGSize(wIDth: firstTrack.naturalSize.wIDth,height: firstTrack.naturalSize.height)
let tempurl = tempuRLWithmp4Extension()
guard let export = AVAssetExportSession(asset: composition,presetname: AVAssetExportPresetHighestQuality)
else{
onComplete(.failure(dIDNotFinish.dIDNotCompletE))
return
}
我想要的结果看起来像这样
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的我正在尝试合并两个视频,其中一个是预先选择的视频,另一个是来自前置摄像头的视频全部内容,希望文章能够帮你解决我正在尝试合并两个视频,其中一个是预先选择的视频,另一个是来自前置摄像头的视频所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。