PQStuckPointEditerController.swift 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  1. //
  2. // PQStuckPointEditerController.swift
  3. // PQSpeed
  4. //
  5. // Created by ak on 2021/4/26.
  6. // Copyright © 2021 BytesFlow. All rights reserved.
  7. // 功能:卡点音乐编辑界面
  8. // 创建不同玩法的类型 (1:跳跃卡点,2:快慢速,3:仅配乐)
  9. public enum createStickersModel: Int{
  10. case createStickersModelPoint = 1 // 跳跃卡点
  11. case createStickersModelSpeed = 2 // 快慢速
  12. case createStickersModelOnlyMusic = 3// 仅配乐
  13. }
  14. import Foundation
  15. import ObjectMapper
  16. import RealmSwift
  17. import UIKit
  18. class PQStuckPointEditerController: PQBaseViewController {
  19. // 是否导出视频成功
  20. var isExportVideosSuccess: Bool = false
  21. // 是否请求卡点数据成功
  22. var isStuckPointDataSuccess: Bool = false
  23. // 是否同步音乐成功
  24. var isSynchroMusicInfoSuccess: Bool = false
  25. /// 当前所有的filter
  26. var filters: Array = Array<ImageProcessingOperation>.init()
  27. // 选中所有素材的的总时长 已经不包括图片的时长
  28. var selectedTotalDuration: Float64 = 0
  29. // 选择的总数
  30. var selectedDataCount: Int = 0
  31. // 选择的图片总数
  32. var selectedImageDataCount: Int = 0
  33. // 选中的素材数据
  34. var selectedPhotoData: [PQEditVisionTrackMaterialsModel]?
  35. // 选中的音乐数据
  36. var stuckPointMusicData: PQVoiceModel?
  37. // 保存所有段的所有贴纸,音频信息,用于播放器的渲染使用
  38. var projectModel: PQEditProjectModel = PQEditProjectModel()
  39. // 从草稿箱进入的项目数据
  40. var draftProjectModel: PQEditProjectModel?
  41. var mStickers: [PQEditVisionTrackMaterialsModel]?
  42. // 播放器的开始和结束时间,1,刚进界面使用推荐的开始结束时间,2,用户修改起结点时修改
  43. var playeTimeRange: CMTimeRange = CMTimeRange()
  44. // add by ak 是否是再创作模式
  45. var isReCreate: Bool = false
  46. // 最后一个选择的模式 BTN 用于还原选中状态
  47. var lastEditModelBtn: UIButton?
  48. // add by ak 最大、最小速度 有固定值和自定义,当快慢速下两个值都有效,当跳跃卡点只有maxSpeed有效
  49. var maxSpeed: Float = 1
  50. var minSpeed: Float = 1
  51. //快慢速最后一次选择的速度位置
  52. var lastSpeedSelectIndex:Int = 0
  53. //跳跃卡点最后一次选择的速度位置
  54. var lastJumpSpeedSelectIndex:Int = 0
  55. //循环次数设置最后一次选择的位置
  56. var lastCyclesSelectIndex:Int = 0
  57. //当前选择的玩法模式
  58. var currentCreateStickersModel:createStickersModel = .createStickersModelSpeed
  59. //最终使用的卡点数据
  60. var finallyStuckPoints: Array = Array<Float>.init()
  61. //最终使用的音频时长
  62. var finallyUserAudioTime:Float = 0.0
  63. // 下一步
  64. lazy var nextBtn: UIButton = {
  65. let nextBtn = UIButton(type: .custom)
  66. nextBtn.frame = CGRect(x: cScreenWidth - 16 - cDefaultMargin * 6, y: cDevice_iPhoneStatusBarHei + (cDevice_iPhoneNavBarHei - cDefaultMargin * 3) / 2, width: cDefaultMargin * 6, height: cDefaultMargin * 3)
  67. nextBtn.setTitle("合成", for: .normal)
  68. nextBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: .medium)
  69. nextBtn.addTarget(self, action: #selector(nextBtnClick(sender:)), for: .touchUpInside)
  70. nextBtn.backgroundColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  71. nextBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#FFFFFF"), for: .normal)
  72. nextBtn.uxy_acceptEventInterval = 0.5
  73. nextBtn.addCorner(corner: 3)
  74. return nextBtn
  75. }()
  76. // 播放器显示 view
  77. lazy var playerView: PQGPUImagePlayerView = {
  78. let playerHeight = cScreenWidth
  79. let playerView = PQGPUImagePlayerView(frame: CGRect(x: 0, y: navHeadImageView?.frame.maxY ?? 0, width: playerHeight, height: playerHeight))
  80. playerView.backgroundColor = PQBFConfig.shared.styleBackGroundColor
  81. playerView.isShowLine = false
  82. playerView.showGaussianBlur = true
  83. playerView.renderViewOnClickHandle = {[weak self] in
  84. self?.musicEditBGView.pausePlayer()
  85. }
  86. playerView.playerEmptyView.isHidden = true
  87. return playerView
  88. }()
  89. /// 节奏选择视图
  90. lazy var sustomSwitchView: PQCustomSwitchView = {
  91. let sustomSwitchView = PQCustomSwitchView(frame: CGRect(x: 16, y: 0, width: 180, height: 30), titles: ["快节奏", "适中", "慢节奏"], defaultIndex: stuckPointMusicData?.speed ?? 2)
  92. sustomSwitchView.switchChangeHandle = { [weak self] sender in
  93. // 改变速率
  94. if(self?.currentCreateStickersModel == .createStickersModelSpeed){
  95. self?.stuckPointMusicData?.speed = sender.tag + 1
  96. }else{
  97. self?.stuckPointMusicData?.speed = sender.tag
  98. }
  99. self?.projectModel.sData?.getBGMSession()?.sectionTimeline?.audioTrack?.audioTrackMaterials.first?.bgmInfo?.rhythmMusicSpeed = sender.tag
  100. // 播放前先暂停
  101. // self?.playerView.stop()
  102. // 开始播放
  103. self?.settingPlayerView()
  104. // 点击上报:选择节奏
  105. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_click_selectRhythm, pageSource: .sp_stuck_previewSyncedUp, extParams: ["rhythmNumber": sender.tag], remindmsg: "点击上报:选择节奏")
  106. }
  107. return sustomSwitchView
  108. }()
  109. /// 裁剪视图
  110. lazy var stuckPointCuttingView: PQStuckPointCuttingView = {
  111. let stuckPointCuttingView = PQStuckPointCuttingView(frame: CGRect(x: 0, y: optionlineView.frame.minY - 85 - 28, width: view.frame.width, height: 80), duration: CGFloat(Float(stuckPointMusicData?.duration ?? "0") ?? 0), startTime: CGFloat(stuckPointMusicData?.startTime ?? 0), endTime: CGFloat(stuckPointMusicData?.endTime ?? 0))
  112. /// 裁剪进度回调
  113. stuckPointCuttingView.videoRangeDidChanged = { [weak self] startTime, endTime in
  114. BFLog(message: "裁剪返回--startTime = \(startTime),endTime = \(endTime)")
  115. }
  116. /// 播放进度回调
  117. stuckPointCuttingView.videoProgressDidChanged = { [weak self] progress in
  118. BFLog(message: "进度更新返回--progress = \(progress) \(String(describing: self?.playerView.mPlayeTimeRange))")
  119. }
  120. /// 拖缀结束的回调 type - 1-拖动左边裁剪结束 2--拖动右边裁剪结束 3-进度条拖动结束 4-滑动结束
  121. stuckPointCuttingView.videoDidEndDragging = { [weak self] type, startTime, endTime, progress in
  122. BFLog(message: "拖拽结束返回--type = \(type),startTime = \(startTime),endTime = \(endTime),progress = \(progress)")
  123. self?.playerView.pause()
  124. // 修改最新值
  125. self?.stuckPointMusicData?.startTime = Float64(startTime)
  126. self?.stuckPointMusicData?.endTime = Float64(endTime)
  127. // 红的指针完成
  128. if type == 3 {
  129. if CMTimeGetSeconds(self?.playerView.mPlayeTimeRange?.end ?? .zero) == 0 {
  130. BFLog(message: "mPlayeTimeRange is error")
  131. return
  132. }
  133. let newBeginSconds = (Double(startTime) + (Double(endTime) - Double(startTime)) * Double(progress)) * 600
  134. BFLog(message: " newBeginSconds is \(newBeginSconds)")
  135. let seekTimeRange: CMTimeRange = CMTimeRange(start: CMTime(value: CMTimeValue(Int64(newBeginSconds)), timescale: 600), end:
  136. CMTime(value: CMTimeValue(Int64(endTime * 600)), timescale: 600))
  137. BFLog(message: "修改的开始 \(CMTimeGetSeconds(seekTimeRange.start)) 结束 \(CMTimeGetSeconds(seekTimeRange.end))")
  138. // 重新设置有效缓存
  139. self?.playerView.configCache(beginTime: CMTimeGetSeconds(seekTimeRange.start))
  140. self?.playerView.play(pauseFirstFrame: false, playeTimeRange: seekTimeRange)
  141. } else {
  142. // 更改素材开始时间及结束时间
  143. self?.projectModel.sData?.getBGMSession()?.sectionTimeline?.audioTrack?.audioTrackMaterials.first?.out = Float64(endTime)
  144. self?.projectModel.sData?.getBGMSession()?.sectionTimeline?.audioTrack?.audioTrackMaterials.first?.model_in = Float64(startTime)
  145. self?.projectModel.sData?.getBGMSession()?.sectionTimeline?.audioTrack?.audioTrackMaterials.first?.timelineIn = Float64(startTime)
  146. self?.projectModel.sData?.getBGMSession()?.sectionTimeline?.audioTrack?.audioTrackMaterials.first?.timelineOut = Float64(endTime)
  147. BFLog(message: "调整后总时长: \(endTime - startTime) startTime:\(startTime) endTime:\(endTime)")
  148. // 初始化音频的开始和结束时间
  149. self?.playeTimeRange = CMTimeRange(start: CMTimeMakeWithSeconds(Float64(startTime), preferredTimescale: BASE_FILTER_TIMESCALE), end: CMTimeMakeWithSeconds(Float64(endTime), preferredTimescale: BASE_FILTER_TIMESCALE))
  150. // DispatchQueue.global().async { // 并行、异步
  151. let beginTime: TimeInterval = Date().timeIntervalSince1970
  152. self?.mStickers = self?.createStickers(sections: self?.projectModel.sData?.sections ?? List(), inputSize: CGSize(width: CGFloat(self?.projectModel.sData?.videoMetaData?.videoWidth ?? 0), height: CGFloat(self?.projectModel.sData?.videoMetaData?.videoHeight ?? 0)))
  153. self?.playerView.mStickers = self?.mStickers
  154. DispatchQueue.main.async { // 串行、异步
  155. var endTime: TimeInterval = Date().timeIntervalSince1970
  156. BFLog(message: "endTime is endTimeendTime \(endTime = beginTime)")
  157. self?.playerView.play(pauseFirstFrame: false, playeTimeRange: self!.playeTimeRange)
  158. }
  159. // }
  160. }
  161. }
  162. return stuckPointCuttingView
  163. }()
  164. /// 卡点模式标题
  165. lazy var pointEditRemindLab: UILabel = {
  166. let pointEditRemindLab = UILabel()
  167. pointEditRemindLab.backgroundColor = .clear
  168. pointEditRemindLab.textAlignment = .left
  169. pointEditRemindLab.font = UIFont.boldSystemFont(ofSize: 14)
  170. pointEditRemindLab.textColor = .black
  171. pointEditRemindLab.text = "卡点模式"
  172. return pointEditRemindLab
  173. }()
  174. /// 卡点模式标题
  175. lazy var speedTitleLab: UILabel = {
  176. let speedTitleLab = UILabel()
  177. speedTitleLab.backgroundColor = .clear
  178. speedTitleLab.textAlignment = .left
  179. speedTitleLab.font = UIFont.boldSystemFont(ofSize: 14)
  180. speedTitleLab.textColor = .black
  181. speedTitleLab.text = "节奏变化"
  182. return speedTitleLab
  183. }()
  184. /// 卡点模式下方操作区背景
  185. lazy var pointEditBGView: UIView = {
  186. let pointEditBGView = UIView()
  187. pointEditBGView.backgroundColor = .clear
  188. return pointEditBGView
  189. }()
  190. /// 下方音乐编辑操作区背景
  191. lazy var musicEditBGView: PQSelecteMusicView = {
  192. let musicEditBGView = PQSelecteMusicView.init()
  193. musicEditBGView.backgroundColor = .clear
  194. musicEditBGView.isUserInteractionEnabled = true
  195. musicEditBGView.isHidden = true
  196. musicEditBGView.musicSearchBtn.addTarget(self, action: #selector(musicSearchBtnClick(sender:)), for: .touchUpInside)
  197. musicEditBGView.didSelectItemHandle = { [weak self] isplaying in
  198. self?.playerView.pause()
  199. }
  200. musicEditBGView.btnClickHandle = { [weak self] _, bgmData in
  201. // 使用音乐
  202. self?.userstuckPointMusic(musicData: bgmData as? PQVoiceModel)
  203. }
  204. return musicEditBGView
  205. }()
  206. // 卡点编辑 btn
  207. lazy var pointEditerBtn: UIButton = {
  208. let pointEdterBtn = UIButton(type: .custom)
  209. pointEdterBtn.setImage(UIImage().BF_Image(named: "pointEditerBtn_n"), for: .normal)
  210. pointEdterBtn.setImage(UIImage().BF_Image(named: "pointEditerBtn_h"), for: .selected)
  211. pointEdterBtn.addTarget(self, action: #selector(pointEditerBtnClick(sender:)), for: .touchUpInside)
  212. pointEdterBtn.isSelected = true
  213. pointEdterBtn.adjustsImageWhenHighlighted = false
  214. return pointEdterBtn
  215. }()
  216. // 音乐编辑 btn
  217. lazy var musicEditerBtn: UIButton = {
  218. let musicEditerBtn = UIButton(type: .custom)
  219. musicEditerBtn.setImage(UIImage().BF_Image(named: "musicEditerBtn_n"), for: .normal)
  220. musicEditerBtn.setImage(UIImage().BF_Image(named: "musicEditerBtn_h"), for: .selected)
  221. musicEditerBtn.addTarget(self, action: #selector(musicEditerBtnClick(sender:)), for: .touchUpInside)
  222. musicEditerBtn.adjustsImageWhenHighlighted = false
  223. return musicEditerBtn
  224. }()
  225. // 快慢速卡点模式 btn
  226. lazy var speedStuckBtn: UIButton = {
  227. let speedStuckBtn = UIButton(type: .custom)
  228. speedStuckBtn.addTarget(self, action: #selector(editModelClick(sender:)), for: .touchUpInside)
  229. speedStuckBtn.setBackgroundImage(UIImage().BF_Image(named: "speedstuck_n"), for: .normal)
  230. speedStuckBtn.setBackgroundImage(UIImage().BF_Image(named: "speedstuck_h"), for: .selected)
  231. speedStuckBtn.tag = 1
  232. speedStuckBtn.adjustsImageWhenHighlighted = false
  233. return speedStuckBtn
  234. }()
  235. //
  236. lazy var speedStuckBtnGif: UIImageView = {
  237. let speedStuckBtnGif = UIImageView.init()
  238. speedStuckBtnGif.kf.setImage(with: URL(fileURLWithPath: Bundle.init().BF_mainbundle().path(forResource: "speedstuck_h", ofType: ".gif")!))
  239. return speedStuckBtnGif
  240. }()
  241. // 跳转卡点模式 btn
  242. lazy var jumpPointBtn: UIButton = {
  243. let jumpPointBtn = UIButton(type: .custom)
  244. jumpPointBtn.setImage(UIImage().BF_Image(named: "jumpPoint_n"), for: .normal)
  245. jumpPointBtn.setImage(UIImage().BF_Image(named: "jumpPoint_h"), for: .selected)
  246. jumpPointBtn.tag = 2
  247. jumpPointBtn.addTarget(self, action: #selector(editModelClick(sender:)), for: .touchUpInside)
  248. jumpPointBtn.adjustsImageWhenHighlighted = false
  249. return jumpPointBtn
  250. }()
  251. lazy var jumpPointBtnGif: UIImageView = {
  252. let jumpPointBtnGif = UIImageView.init()
  253. jumpPointBtnGif.kf.setImage(with: URL(fileURLWithPath: Bundle.init().BF_mainbundle().path(forResource: "jumpPoint_n", ofType: ".gif")!))
  254. return jumpPointBtnGif
  255. }()
  256. // 仅配乐模式 btn
  257. lazy var onlyMusicBtn: UIButton = {
  258. let onlyMusicBtn = UIButton(type: .custom)
  259. onlyMusicBtn.setImage(UIImage().BF_Image(named: "onlyMusic_n"), for: .normal)
  260. onlyMusicBtn.setImage(UIImage().BF_Image(named: "onlyMusic_h"), for: .selected)
  261. onlyMusicBtn.tag = 3
  262. onlyMusicBtn.addTarget(self, action: #selector(editModelClick(sender:)), for: .touchUpInside)
  263. onlyMusicBtn.adjustsImageWhenHighlighted = false
  264. return onlyMusicBtn
  265. }()
  266. // 编辑模式的指示线
  267. lazy var editModelLineView: UIView = {
  268. let editModelLineView = UIView()
  269. editModelLineView.backgroundColor = UIColor.hexColor(hexadecimal: "#EFEFEF")
  270. return editModelLineView
  271. }()
  272. // 操作面板上的分割线
  273. lazy var optionlineView: UIView = {
  274. let optionlineView = UIView()
  275. optionlineView.backgroundColor = UIColor.hexColor(hexadecimal: "#EFEFEF")
  276. return optionlineView
  277. }()
  278. // 固定速度 UI
  279. lazy var speedSettingView: PQSpeedSettingView = {
  280. let speedSettingView = PQSpeedSettingView()
  281. speedSettingView.backgroundColor = .clear
  282. speedSettingView.viewType = 1
  283. speedSettingView.selectSpeedCallBack = { [weak self] maxSpeed, minSpeed,selectIndex in
  284. BFLog(message: "固定maxSpeed is\(maxSpeed) minSpeed \(minSpeed)")
  285. //更新最后一次选择的位置恢复时使用
  286. if(speedSettingView.viewType == 1){
  287. self?.lastSpeedSelectIndex = selectIndex
  288. }else if(speedSettingView.viewType == 2){
  289. self?.lastJumpSpeedSelectIndex = selectIndex
  290. }else{
  291. self?.lastCyclesSelectIndex = selectIndex
  292. }
  293. self?.maxSpeed = maxSpeed
  294. self?.minSpeed = minSpeed
  295. if maxSpeed == -1.0 && minSpeed == -1.0 {
  296. self?.customSpeedSettingView.isHidden = true
  297. self?.customSpeedSettingView.isJumpSpeedModel = speedSettingView.viewType == 2
  298. }
  299. self?.settingPlayerView()
  300. }
  301. return speedSettingView
  302. }()
  303. // 自定义速度
  304. lazy var customSpeedSettingView: PQCustomSpeedSettingView = {
  305. let customSpeedSettingView = PQCustomSpeedSettingView(frame: CGRect(x: 0, y: cScreenHeigth - 354, width: cScreenWidth, height: 354))
  306. customSpeedSettingView.isHidden = true
  307. customSpeedSettingView.isJumpSpeedModel = false
  308. customSpeedSettingView.selectSpeedCallBack = { [weak self] maxSpeed, minSpeed ,isJumpSpeedModel in
  309. self?.maxSpeed = maxSpeed
  310. self?.minSpeed = minSpeed
  311. BFLog(message: "自定义速度maxSpeed is\(maxSpeed) minSpeed \(minSpeed) \(isJumpSpeedModel)")
  312. self?.settingPlayerView()
  313. }
  314. return customSpeedSettingView
  315. }()
  316. /// 音乐标题
  317. lazy var musicNameView: UIView = {
  318. let musicNameView = UIView()
  319. musicNameView.addSubview(musicNameLab)
  320. let nameWidth: CGFloat = musicNameLab.frame.width + (25 + cDefaultMargin * 3)
  321. musicNameView.frame = CGRect(x: (view.frame.width - nameWidth) / 2, y: cDevice_iPhoneStatusBarHei + (cDevice_iPhoneNavBarHei - cDefaultMargin * 3) / 2, width: nameWidth, height: cDefaultMargin * 3)
  322. // musicNameView.backgroundColor = UIColor.hexColor(hexadecimal: "#333333")
  323. musicNameView.addCorner(corner: musicNameView.frame.height / 2)
  324. let musicImageView = UIImageView()
  325. musicImageView.tintColor = PQBFConfig.shared.styleTitleColor
  326. musicImageView.image = UIImage().BF_Image(named: "stuckPoint_reCreate_music").withRenderingMode(.alwaysTemplate)
  327. musicImageView.frame = CGRect(x: musicNameView.frame.height / 2 - 5, y: (musicNameView.frame.height - 22) / 2, width: 22, height: 22)
  328. musicNameView.addSubview(musicImageView)
  329. musicNameLab.frame.origin.x = musicImageView.frame.maxX + 5
  330. return musicNameView
  331. }()
  332. /// 音乐歌曲名称
  333. lazy var musicNameLab: LMJHorizontalScrollText = {
  334. let nameWidth: CGFloat = sizeWithText(text: "\(stuckPointMusicData?.musicName ?? "")", font: UIFont.systemFont(ofSize: 13), size: CGSize(width: view.frame.width - ((cDefaultMargin * 6 + 16 * 2) * 2) - (25 + cDefaultMargin * 3), height: cDefaultMargin * 3)).width
  335. let musicNameLab = LMJHorizontalScrollText(frame: CGRect(x: 0, y: 0, width: nameWidth < cDefaultMargin * 4 ? cDefaultMargin * 4 : nameWidth, height: cDefaultMargin * 3))
  336. musicNameLab.textColor = PQBFConfig.shared.styleTitleColor
  337. musicNameLab.textFont = UIFont.systemFont(ofSize: 13)
  338. musicNameLab.speed = 0.03
  339. musicNameLab.moveDirection = LMJTextScrollMoveLeft
  340. musicNameLab.moveMode = LMJTextScrollContinuous
  341. if nameWidth < cDefaultMargin * 4 {
  342. musicNameLab.text = " \(stuckPointMusicData?.musicName ?? "") "
  343. } else {
  344. musicNameLab.text = " \(stuckPointMusicData?.musicName ?? "") "
  345. }
  346. return musicNameLab
  347. }()
  348. /// 同步进度显示
  349. lazy var synchroMarskView: PQStuckPointLoadingView = {
  350. var synchroMarskView = PQStuckPointLoadingView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
  351. synchroMarskView.cancelHandle = { [weak self] _ in
  352. self?.navigationController?.popViewController(animated: true)
  353. }
  354. return synchroMarskView
  355. }()
  356. override func viewWillAppear(_ animated: Bool) {
  357. super.viewDidAppear(animated)
  358. lineView?.isHidden = true
  359. UIApplication.shared.isIdleTimerDisabled = true
  360. musicNameLab.move()
  361. PQNotification.addObserver(self, selector: #selector(enterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
  362. }
  363. @objc func enterBackground() {
  364. BFLog(message: "进入到后台")
  365. // 取消导出
  366. playerView.pause()
  367. }
  368. override func backBtnClick() {
  369. super.backBtnClick()
  370. playerView.pause()
  371. // 点击上报:返回按钮
  372. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_click_back, pageSource: .sp_stuck_previewSyncedUp, extParams: nil, remindmsg: "卡点视频数据上报-(点击上报:返回按钮)")
  373. }
  374. //使用选择音乐 调用情况:1,操作面板直接选择 ,2 搜索界面点击使用
  375. func userstuckPointMusic(musicData: PQVoiceModel?) {
  376. //1,音乐面板点击了使用
  377. stuckPointMusicData = musicData
  378. //2,同步最新音乐数据
  379. synchroMusicInfoData()
  380. //3,更新音乐标题UI
  381. let nameWidth: CGFloat = sizeWithText(text: "\(stuckPointMusicData?.musicName ?? "")", font: UIFont.systemFont(ofSize: 13), size: CGSize(width: (view.frame.width ) - ((cDefaultMargin * 6 + 16 * 2) * 2) - (25 + cDefaultMargin * 3), height: cDefaultMargin * 3)).width
  382. if nameWidth < cDefaultMargin * 4 {
  383. musicNameLab.text = " \(stuckPointMusicData?.musicName ?? "") "
  384. } else {
  385. musicNameLab.text = " \(stuckPointMusicData?.musicName ?? "") "
  386. }
  387. }
  388. //点击搜索音乐
  389. @objc func musicSearchBtnClick(sender: UIButton) {
  390. let searchVC = PQEditMusicSearchController.init()
  391. searchVC.btnClickHandle = { [weak self] _, bgmData in
  392. // 使用音乐
  393. BFLog(message: "搜索音乐点击了使用")
  394. // self?.musicEditBGView.insertSearchMusic(model: bgmData as! PQVoiceModel)
  395. self?.userstuckPointMusic(musicData: bgmData as? PQVoiceModel)
  396. }
  397. let navigationController: UINavigationController = UINavigationController(rootViewController: searchVC)
  398. navigationController.modalPresentationStyle = .fullScreen
  399. present(navigationController, animated: true, completion: nil)
  400. }
  401. // 卡点编辑
  402. @objc func pointEditerBtnClick(sender: UIButton) {
  403. if sender.isSelected { return }
  404. sender.isSelected = !sender.isSelected
  405. musicEditerBtn.isSelected = false
  406. pointEditBGView.isHidden = false
  407. musicEditBGView.isHidden = true
  408. }
  409. // 音乐编辑
  410. @objc func musicEditerBtnClick(sender: UIButton) {
  411. if sender.isSelected { return }
  412. sender.isSelected = !sender.isSelected
  413. pointEditerBtn.isSelected = false
  414. pointEditBGView.isHidden = true
  415. musicEditBGView.isHidden = false
  416. }
  417. // 三种模式修改
  418. @objc func editModelClick(sender: UIButton) {
  419. //1,根据条件判断哪些功能不可用直接出提示
  420. // if(selectedDataCount == selectedImageDataCount && (sender == speedStuckBtn || sender == onlyMusicBtn )){
  421. // BFLog(message: "全是图片 \(selectedDataCount) \(selectedImageDataCount)")
  422. // cShowHUB(superView: self.view, msg: "1111")
  423. // return
  424. // }
  425. // if sender.isSelected {
  426. // BFLog(message: "已经是选中状态")
  427. // return
  428. // }
  429. lastEditModelBtn?.isSelected = false
  430. sender.isSelected = !sender.isSelected
  431. lastEditModelBtn = sender
  432. BFLog(message: "sender tag is \(sender.tag)")
  433. // 1 ui 调整
  434. if sender.tag == 1 || sender.tag == 2 {
  435. speedSettingView.viewType = sender.tag
  436. customSpeedSettingView.isJumpSpeedModel = speedSettingView.viewType == 2
  437. speedSettingView.snp.remakeConstraints { make in
  438. make.left.equalToSuperview().offset(16)
  439. make.right.equalToSuperview()
  440. make.top.equalTo(editModelLineView.snp_bottom).offset(8)
  441. make.height.equalTo(sender.tag == 1 ? 44 : 30)
  442. }
  443. speedSettingView.isHidden = false
  444. speedTitleLab.isHidden = false
  445. sustomSwitchView.isHidden = false
  446. editModelLineView.isHidden = false
  447. } else {
  448. speedTitleLab.isHidden = true
  449. speedSettingView.isHidden = true
  450. editModelLineView.isHidden = true
  451. sustomSwitchView.isHidden = true
  452. }
  453. //素材全是图片的时候三个模式都显示循环设置 UI
  454. if(selectedDataCount == selectedImageDataCount){
  455. speedSettingView.viewType = 3
  456. customSpeedSettingView.isJumpSpeedModel = false
  457. speedSettingView.snp.remakeConstraints { make in
  458. make.left.equalToSuperview().offset(16)
  459. make.right.equalToSuperview()
  460. make.top.equalTo(editModelLineView.snp_bottom).offset(8)
  461. make.height.equalTo(30)
  462. }
  463. speedSettingView.isHidden = !(selectedDataCount == selectedImageDataCount)
  464. editModelLineView.isHidden = !(selectedDataCount == selectedImageDataCount)
  465. speedSettingView.setSelectItem(index: lastCyclesSelectIndex)
  466. }else{
  467. if sender.tag == 1 { // 快慢速
  468. speedSettingView.setSelectItem(index: lastSpeedSelectIndex)
  469. } else if sender.tag == 2 { // 跳跃卡点
  470. speedSettingView.setSelectItem(index: lastJumpSpeedSelectIndex)
  471. } else if sender.tag == 3 { // 仅音乐
  472. customSpeedSettingView.isJumpSpeedModel = false
  473. speedSettingView.viewType = 2
  474. }
  475. }
  476. // 2 设置 btn 不同显示状态
  477. var speedStuckBtnGifName = ""
  478. var jumpPointBtnGifName = ""
  479. if sender.tag == 1 { // 快慢速
  480. speedStuckBtnGifName = "speedstuck_h"
  481. jumpPointBtnGifName = "jumpPoint_n"
  482. currentCreateStickersModel = .createStickersModelSpeed
  483. } else if sender.tag == 2 { // 跳跃卡点
  484. speedStuckBtnGifName = "speedstuck_n"
  485. jumpPointBtnGifName = "jumpPoint_h"
  486. currentCreateStickersModel = .createStickersModelPoint
  487. } else if sender.tag == 3 { // 仅音乐
  488. speedStuckBtnGifName = "speedstuck_n"
  489. jumpPointBtnGifName = "jumpPoint_n"
  490. currentCreateStickersModel = .createStickersModelOnlyMusic
  491. }
  492. settingPlayerView()
  493. speedStuckBtnGif.kf.setImage(with: URL(fileURLWithPath: Bundle.init().BF_mainbundle().path(forResource:speedStuckBtnGifName , ofType: ".gif")!))
  494. jumpPointBtnGif.kf.setImage(with: URL(fileURLWithPath: Bundle.init().BF_mainbundle().path(forResource: jumpPointBtnGifName , ofType: ".gif")!))
  495. }
  496. override func viewWillDisappear(_ animated: Bool) {
  497. super.viewWillDisappear(animated)
  498. UIApplication.shared.isIdleTimerDisabled = false
  499. musicNameLab.stop()
  500. playerView.pause()
  501. musicEditBGView.pausePlayer()
  502. }
  503. override func viewDidLoad() {
  504. super.viewDidLoad()
  505. leftButton(image: "icon_detail_back", tintColor: PQBFConfig.shared.styleTitleColor)
  506. navHeadImageView?.addSubview(nextBtn)
  507. navHeadImageView?.addSubview(musicNameView)
  508. // 添加子视图
  509. addSubViews()
  510. // 导出相册视频
  511. exportPhotoData()
  512. // 同步音乐数据
  513. synchroMusicInfoData()
  514. // 曝光上报:预览页面曝光上报
  515. PQEventTrackViewModel.baseReportUpload(businessType: .bt_windowView, objectType: .ot_view_previewSyncedUp, pageSource: .sp_stuck_previewSyncedUp, extParams: nil, remindmsg: "卡点视频数据上报-(曝光上报:预览页面曝光上报)")
  516. }
  517. /// 添加子视图
  518. /// - Returns: <#description#>
  519. func addSubViews() {
  520. if (stuckPointMusicData?.rhythmSdata.count ?? 0) <= 0 {
  521. return
  522. }
  523. view.addSubview(playerView)
  524. view.addSubview(pointEditBGView)
  525. view.addSubview(musicEditBGView)
  526. view.addSubview(optionlineView)
  527. view.addSubview(pointEditerBtn)
  528. view.addSubview(musicEditerBtn)
  529. view.addSubview(customSpeedSettingView)
  530. //卡点
  531. pointEditBGView.addSubview(pointEditRemindLab)
  532. pointEditBGView.addSubview(speedTitleLab)
  533. pointEditBGView.addSubview(speedStuckBtn)
  534. speedStuckBtn.addSubview(speedStuckBtnGif)
  535. pointEditBGView.addSubview(jumpPointBtn)
  536. jumpPointBtn.addSubview(jumpPointBtnGif)
  537. pointEditBGView.addSubview(onlyMusicBtn)
  538. pointEditBGView.addSubview(editModelLineView)
  539. pointEditBGView.addSubview(speedSettingView)
  540. pointEditBGView.addSubview(sustomSwitchView)
  541. //音乐
  542. musicEditBGView.addSubview(stuckPointCuttingView)
  543. pointEditerBtn.snp.makeConstraints { make in
  544. make.left.equalToSuperview().offset(100)
  545. make.bottom.equalToSuperview().offset(-(14 + cAKSafeAreaHeight))
  546. make.height.equalTo(24)
  547. make.width.equalTo(24)
  548. }
  549. musicEditerBtn.snp.makeConstraints { make in
  550. make.right.equalToSuperview().offset(-100)
  551. make.top.equalTo(pointEditerBtn.snp_top)
  552. make.height.equalTo(24)
  553. make.width.equalTo(24)
  554. }
  555. pointEditBGView.snp.makeConstraints { make in
  556. make.left.right.equalTo(view)
  557. make.bottom.equalTo(pointEditerBtn.snp_top).offset(-7)
  558. make.height.equalTo(290)
  559. }
  560. musicEditBGView.snp.makeConstraints { make in
  561. make.left.right.equalTo(view)
  562. make.bottom.equalTo(pointEditerBtn.snp_top).offset(-7)
  563. make.height.equalTo(290)
  564. }
  565. stuckPointCuttingView.snp.makeConstraints { make in
  566. make.left.right.equalTo(view)
  567. make.bottom.equalTo(pointEditerBtn.snp_top).offset(-7)
  568. make.height.equalTo(290)
  569. }
  570. optionlineView.snp.makeConstraints { make in
  571. make.left.right.equalToSuperview()
  572. make.bottom.equalTo(pointEditBGView.snp_bottom).offset(-1)
  573. make.height.equalTo(1)
  574. }
  575. stuckPointCuttingView.snp.makeConstraints { make in
  576. make.left.right.equalToSuperview()
  577. make.bottom.equalTo(musicEditBGView.snp_bottom).offset(-1)
  578. make.height.equalTo(85)
  579. }
  580. pointEditRemindLab.snp.makeConstraints { make in
  581. make.left.equalToSuperview().offset(16)
  582. make.top.equalToSuperview().offset(16)
  583. make.height.equalTo(20)
  584. make.width.equalTo(80)
  585. }
  586. speedStuckBtn.snp.makeConstraints { make in
  587. make.left.equalToSuperview().offset(16)
  588. make.top.equalTo(pointEditRemindLab.snp_bottom).offset(8)
  589. make.height.equalTo(80)
  590. make.width.equalTo(80)
  591. }
  592. speedStuckBtnGif.snp.makeConstraints { make in
  593. make.height.width.equalTo(46)
  594. make.top.equalToSuperview().offset(8)
  595. make.centerX.equalToSuperview()
  596. }
  597. jumpPointBtn.snp.makeConstraints { make in
  598. make.left.equalTo(speedStuckBtn.snp_right).offset(10)
  599. make.top.equalTo(speedStuckBtn.snp_top)
  600. make.height.equalTo(80)
  601. make.width.equalTo(80)
  602. }
  603. jumpPointBtnGif.snp.makeConstraints { make in
  604. make.height.width.equalTo(46)
  605. make.top.equalToSuperview().offset(8)
  606. make.centerX.equalToSuperview()
  607. }
  608. onlyMusicBtn.snp.makeConstraints { make in
  609. make.left.equalTo(jumpPointBtn.snp_right).offset(10)
  610. make.top.equalTo(speedStuckBtn.snp_top)
  611. make.height.equalTo(80)
  612. make.width.equalTo(64)
  613. }
  614. editModelLineView.snp.makeConstraints { make in
  615. make.left.equalToSuperview().offset(16)
  616. make.right.equalToSuperview()
  617. make.bottom.equalTo(onlyMusicBtn.snp_bottom).offset(17)
  618. make.height.equalTo(1)
  619. }
  620. speedSettingView.snp.makeConstraints { make in
  621. make.left.equalToSuperview().offset(16)
  622. make.right.equalToSuperview()
  623. make.top.equalTo(editModelLineView.snp_bottom).offset(8)
  624. make.height.equalTo(44)
  625. }
  626. speedTitleLab.snp.makeConstraints { make in
  627. make.left.equalToSuperview().offset(16)
  628. make.top.equalToSuperview().offset(213)
  629. make.height.equalTo(20)
  630. make.width.equalTo(80)
  631. }
  632. sustomSwitchView.snp.makeConstraints { make in
  633. make.left.equalToSuperview().offset(16)
  634. make.top.equalTo(speedTitleLab.snp_bottom).offset(8)
  635. make.height.equalTo(30)
  636. make.width.equalTo(180)
  637. }
  638. }
  639. @objc func nextBtnClick(sender _: UIButton) {
  640. BFLog(message: "去发布")
  641. playerView.pause()
  642. let videoExporter = PQStuckPointPublicController()
  643. videoExporter.rhythmMode = currentCreateStickersModel
  644. videoExporter.isReCreate = isReCreate
  645. videoExporter.selectedTotalDuration = selectedTotalDuration
  646. videoExporter.selectedDataCount = selectedDataCount
  647. videoExporter.selectedImageDataCount = selectedImageDataCount
  648. videoExporter.finallyUserAudioTime = finallyUserAudioTime
  649. videoExporter.clipAudioRange = getClipAudioRange()
  650. videoExporter.playeTimeRange = playeTimeRange
  651. // 使用深 copy
  652. let json = projectModel.toJSONString(prettyPrint: false)
  653. if json == nil {
  654. BFLog(message: "数据转换有问题 跳转")
  655. return
  656. }
  657. let tempModel: PQEditProjectModel? = Mapper<PQEditProjectModel>().map(JSONString: json!)
  658. videoExporter.mStickers = mStickers
  659. videoExporter.audioMixModel = stuckPointMusicData
  660. videoExporter.editProjectModel = tempModel
  661. navigationController?.pushViewController(videoExporter, animated: true)
  662. // 点击上报:去合成
  663. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_click_commit, pageSource: .sp_stuck_previewSyncedUp, extParams: ["musicName": stuckPointMusicData?.musicName ?? "", "musicId": stuckPointMusicData?.musicId ?? "", "rhythmNumber": stuckPointMusicData?.speed ?? 2, "duration": ((stuckPointMusicData?.endTime ?? 0) - (stuckPointMusicData?.startTime ?? 0)) * 1000], remindmsg: "点击上报:去合成")
  664. }
  665. // MARK: - 播放器相关操作
  666. /// seek 播放器
  667. /// - Parameter playeTimeRange: 开始和结束时间
  668. func seekPlayer(playeTimeRange: CMTimeRange) {
  669. playerView.setEnableSeek(isSeek: true)
  670. playerView.play(pauseFirstFrame: false, playeTimeRange: playeTimeRange)
  671. }
  672. /// 通过传入的 selectedPhotoData 、 stuckPointMusicData 创建 projectModel 模型 后面都使用 projectModel 参数
  673. func createPorjectData() {
  674. // 1,添加选择的视觉素材
  675. let section: PQEditSectionModel = PQEditSectionModel()
  676. selectedPhotoData?.forEach { model in
  677. let json = model.toJSONString(prettyPrint: false)
  678. if json == nil {
  679. BFLog(message: "数据转换有问题 跳转")
  680. return
  681. }
  682. let tempModel: PQEditVisionTrackMaterialsModel = Mapper<PQEditVisionTrackMaterialsModel>().map(JSONString: json!)!
  683. section.sectionTimeline?.visionTrack?.visionTrackMaterials.append(tempModel)
  684. }
  685. projectModel.sData?.sections.append(section)
  686. // 2,添加背景音乐
  687. projectModel.sData?.addBGM(audioMix: stuckPointMusicData!)
  688. }
  689. //计算拼接音乐的开始和结束点
  690. func getClipAudioRange() -> CMTimeRange {
  691. //设置音乐的拼接范围,开始:推荐的卡点 结束:点到的倒数第二位
  692. let lastSecondPoint = Float((stuckPointMusicData!.rhythmSdata[0].pointTimes[stuckPointMusicData!.rhythmSdata[0].pointTimes.count - 2] )) / Float(BASE_FILTER_TIMESCALE)
  693. let clipAudioRange =
  694. CMTimeRange(start: CMTime(value: CMTimeValue( Float(stuckPointMusicData?.startTime ?? 0) * Float(BASE_FILTER_TIMESCALE)), timescale: BASE_FILTER_TIMESCALE), end: CMTime(value: CMTimeValue((Float(lastSecondPoint)) * Float(BASE_FILTER_TIMESCALE)), timescale: BASE_FILTER_TIMESCALE))
  695. return clipAudioRange
  696. }
  697. // 设置播放器
  698. func settingPlayerView() {
  699. // 1,设置播放器的显示区域 和画布大小
  700. // - 按第一个素材尺寸自适应
  701. let playerShowHeight = pointEditBGView.frame.minY - (navHeadImageView?.frame.maxY ?? 0)
  702. var showRect: CGRect = PQPlayerViewModel.getShowCanvasRect(editProjectModel: projectModel, showType: 1, playerViewHeight: playerShowHeight)
  703. if showRect.size.width == showRect.size.height {
  704. if cScreenWidth > playerShowHeight {
  705. showRect.origin.x = (cScreenWidth - playerShowHeight) / 2
  706. showRect.size.width = playerShowHeight
  707. showRect.size.height = playerShowHeight
  708. } else {
  709. showRect.origin.x = 0
  710. showRect.size.width = cScreenWidth
  711. showRect.size.height = cScreenWidth
  712. }
  713. }
  714. showRect.origin.y = (playerShowHeight - showRect.size.height) / 2.0 + (navHeadImageView?.frame.maxY ?? 0)
  715. if showRect.size.width != 0, showRect.size.height != 0 {
  716. playerView.resetCanvasFrame(frame: showRect)
  717. }
  718. var firstModel: PQEditVisionTrackMaterialsModel?
  719. for part in projectModel.sData!.sections {
  720. if part.sectionTimeline?.visionTrack?.getEnableVisionTrackMaterials().count ?? 0 > 0 {
  721. firstModel = part.sectionTimeline?.visionTrack?.getEnableVisionTrackMaterials().first
  722. break
  723. }
  724. }
  725. var videoSize: CGSize = CGSize(width: Int(firstModel?.width ?? 0), height: Int(firstModel?.height ?? 0))
  726. var minSlider = min(videoSize.width, videoSize.height)
  727. var maxSlider = max(videoSize.width, videoSize.height)
  728. let ration = 1080 / minSlider
  729. minSlider = minSlider * ration
  730. maxSlider = maxSlider * ration
  731. if videoSize.width > videoSize.height { // 宽屏
  732. videoSize = CGSize(width: maxSlider, height: minSlider)
  733. } else {
  734. videoSize = CGSize(width: minSlider, height: maxSlider)
  735. }
  736. let maxValue = max(videoSize.width, videoSize.height )
  737. if maxValue > 1920 {
  738. let maxRation = 1920 / maxValue
  739. videoSize = CGSize(width: videoSize.width * CGFloat(maxRation), height: videoSize.height * CGFloat(maxRation))
  740. BFLog(message: "最长边已经超过 1920 要等比缩小 缩放后\(videoSize)")
  741. }
  742. if (Int(videoSize.width) % 2) != 0 {
  743. videoSize.width = videoSize.width - 1
  744. }
  745. if (Int(videoSize.height) % 2) != 0 {
  746. videoSize.height = videoSize.height - 1
  747. }
  748. projectModel.sData?.videoMetaData?.videoWidth = Int(videoSize.width)
  749. projectModel.sData?.videoMetaData?.videoHeight = Int(videoSize.height)
  750. // 2,创建滤镜
  751. let beginTime: TimeInterval = Date().timeIntervalSince1970
  752. mStickers = createStickers(sections: projectModel.sData?.sections ?? List(), inputSize: CGSize(width: CGFloat(projectModel.sData?.videoMetaData?.videoWidth ?? 0), height: CGFloat(projectModel.sData?.videoMetaData?.videoHeight ?? 0)), model: currentCreateStickersModel)
  753. playerView.mStickers = mStickers
  754. let end: TimeInterval = Date().timeIntervalSince1970
  755. BFLog(message: "createStickers tiskskskskme \(end - beginTime)")
  756. // 3,设置音频
  757. let audioPath = stuckPointMusicData?.localPath ?? ""
  758. BFLog(message: "初始化音频播放器的音频地址为:\(audioPath)")
  759. playerView.stop()
  760. // 这里的测试这个音乐播放有问题
  761. // self.playerView.updateAsset(URL(fileURLWithPath: "63930549652d74e477141e3b79c8d29a9ef8af81625053214516.mp3", relativeTo:Bundle.main.resourceURL!), videoComposition: nil, audioMixModel: nil)
  762. playerView.updateAsset(URL(fileURLWithPath: documensDirectory + audioPath), videoComposition: nil, audioMixModel: nil,originMusicDuration:finallyUserAudioTime,clipAudioRange: getClipAudioRange())
  763. let end2: TimeInterval = Date().timeIntervalSince1970
  764. BFLog(message: "updateAsset tiskskskskme \(end2 - end)")
  765. // 4, 设置播放器的输出画布大小
  766. playerView.movie?.mShowVidoSize = CGSize(width: CGFloat(projectModel.sData?.videoMetaData?.videoWidth ?? 0), height: CGFloat(projectModel.sData?.videoMetaData?.videoHeight ?? 0))
  767. // 5,开始播放
  768. playerView.isLoop = false
  769. playerView.showProgressLab = true
  770. // 初始化音频的开始和结束时间
  771. BFLog(message: "播放的器 开始\(String(describing: CMTimeGetSeconds(playeTimeRange.start))) 结束 \(String(describing: CMTimeGetSeconds(playeTimeRange.end)))")
  772. let end3: TimeInterval = Date().timeIntervalSince1970
  773. playerView.play(pauseFirstFrame: false, playeTimeRange: CMTimeRange(start: playeTimeRange.start, end: playeTimeRange.end))
  774. let end4: TimeInterval = Date().timeIntervalSince1970
  775. BFLog(message: " playerView.play tiskskskskme \(end4 - end3)")
  776. // 6,进度回调
  777. playerView.progress = { [weak self] currentTime, tatolTime, _ in
  778. // 更新进度
  779. let progress = (currentTime - CMTimeGetSeconds(self?.playeTimeRange.start ?? .zero)) / CMTimeGetSeconds(self?.playeTimeRange.duration ?? .zero)
  780. BFLog(message: "\(currentTime) \(tatolTime) 显示播放器进度为: \(progress)")
  781. self?.stuckPointCuttingView.updateProgress(progress: CGFloat(progress))
  782. if self?.synchroMarskView.superview != nil {
  783. self?.synchroMarskView.removeMarskView()
  784. }
  785. }
  786. }
  787. deinit {
  788. BFLog(message: "卡点视频预览界面销毁")
  789. musicNameLab.stop()
  790. playerView.pause()
  791. // 取消所有的导出
  792. PQSingletoMemoryUtil.shared.allExportSession.forEach { _, exportSession in
  793. exportSession.cancelExport()
  794. }
  795. }
  796. }
  797. // MARK: - 视频渲染相关逻辑方法
  798. extension PQStuckPointEditerController {
  799. /// 分割视频 这里只设置视频类型的 in 和 out 并不设置显示的开始和结束时间 mp4 ,png ,png ,mp4
  800. /// - Parameter section: 当前段
  801. /// - Parameter stuckPoints: 用户选择的,或推荐的卡点数
  802. /// - Returns: 返回分割后的所有 stickers 和卡点数是一致的
  803. func clipVideoMerage(section: PQEditSectionModel, stuckPoints: [Float]) -> [PQEditVisionTrackMaterialsModel] {
  804. var stickers: Array = Array<PQEditVisionTrackMaterialsModel>.init()
  805. // 第二种情况:有视频要进行分割
  806. /*
  807. 1, 确定每个视频素材需要切的段数p
  808. 2, 将所有视频时长相加,得到总视频素材时长L = l1 + l2 + ... + ln
  809. 3, 视频素材a1需要切分的个数clipNum = max (round (kongduan * a1 / L) , 1)
  810. */
  811. // 要补的空位数
  812. let kongduan: Int = Int(stuckPoints.count) - Int(section.sectionTimeline!.visionTrack?.getEnableVisionTrackMaterials().count ?? 0)
  813. // 所有视频总时长
  814. var videoTotalDuration: Float64 = 0.0
  815. for video in section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials(type: "video") {
  816. let asset: AVURLAsset = AVURLAsset(url: URL(fileURLWithPath: documensDirectory + video.locationPath), options: nil)
  817. videoTotalDuration = videoTotalDuration + Float64(CMTimeGetSeconds(asset.duration))
  818. }
  819. if videoTotalDuration == 0 {
  820. BFLog(message: "视频总时长出现错误!!!!这里应该有视频素材的")
  821. return stickers
  822. }
  823. for sticker in section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials() {
  824. if sticker.type == StickerType.VIDEO.rawValue {
  825. let asset: AVURLAsset = AVURLAsset(url: URL(fileURLWithPath: documensDirectory + sticker.locationPath), options: nil)
  826. // 要分割的段落
  827. let clipNum = Int(max(round(Double(kongduan) * CMTimeGetSeconds(asset.duration) / videoTotalDuration), 1))
  828. sticker.duration = CMTimeGetSeconds(asset.duration)
  829. BFLog(message: "单个视频\(sticker.locationPath)时长::\(CMTimeGetSeconds(asset.duration)) ,clipNum is:\(clipNum)")
  830. for clipindex in 0 ... clipNum - 1 {
  831. // deep copy sticker model 防止只有一个对象
  832. let stickerjson = sticker.toJSONString(prettyPrint: false)
  833. let deepCopySticker = Mapper<PQEditVisionTrackMaterialsModel>().map(JSONString: stickerjson!)
  834. // 设置循环模式和适配模式
  835. deepCopySticker?.generateDefaultValues()
  836. deepCopySticker?.model_in = clipindex == 0 ? 0 : CMTimeGetSeconds(asset.duration) / Double(clipNum) * Double(clipindex)
  837. deepCopySticker?.out = (deepCopySticker?.model_in ?? 0) + CMTimeGetSeconds(asset.duration) / Double(clipNum)
  838. if (deepCopySticker?.model_in ?? 0) >= CMTimeGetSeconds(asset.duration) || (deepCopySticker?.out ?? 0) >= CMTimeGetSeconds(asset.duration) {
  839. deepCopySticker?.model_in = CMTimeGetSeconds(asset.duration) - CMTimeGetSeconds(asset.duration) / Double(clipNum)
  840. deepCopySticker?.out = CMTimeGetSeconds(asset.duration)
  841. }
  842. BFLog(message: " crilp is in \(deepCopySticker?.model_in ?? 0) out \(deepCopySticker?.out ?? 0) 总时长\(CMTimeGetSeconds(asset.duration))")
  843. if deepCopySticker != nil {
  844. stickers.append(deepCopySticker!)
  845. }
  846. }
  847. } else if sticker.type == StickerType.IMAGE.rawValue {
  848. sticker.generateDefaultValues()
  849. stickers.append(sticker)
  850. }
  851. }
  852. return stickers
  853. }
  854. /// 创建sticker
  855. /// - Parameters:
  856. /// - sections: 项目所有段落数据信息
  857. /// - inputSize: 画布大小
  858. /// - Returns: filters 数据 播放器可直接使用
  859. func createStickers(sections: List<PQEditSectionModel>, inputSize _: CGSize = .zero, model: createStickersModel = .createStickersModelPoint) -> [PQEditVisionTrackMaterialsModel] {
  860. // 推荐卡点数
  861. dealParameter(model: model)
  862. // 保存滤镜对象数据
  863. var stickers: Array = Array<PQEditVisionTrackMaterialsModel>.init()
  864. if model == .createStickersModelPoint { //跳跃卡点
  865. for section in sections {
  866. if section.sectionType == "normal" {
  867. // 当用户上传视觉素材个数大于等于音乐选择区域节拍分割个数时,无需进行视频分割,只显示卡点数-1 个素材
  868. if section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials().count >= stuckPointMusicData!.rhythmSdata[0].pointTimes.count {
  869. for (index, sticker) in section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials().enumerated() {
  870. if index == stuckPointMusicData!.rhythmSdata[0].pointTimes.count {
  871. BFLog(message: "到达卡点数量")
  872. break
  873. }
  874. BFLog(message: "创建 filter start :\(sticker.timelineIn) end :\(sticker.timelineOut) type is \(sticker.type) \(sticker.locationPath)")
  875. sticker.timelineIn = Float64("\(finallyStuckPoints[index])") ?? 0.0
  876. sticker.timelineOut = Float64("\(finallyStuckPoints[index + 1])") ?? 0.0
  877. BFLog(message: "卡点 间隔 \(sticker.timelineIn - sticker.timelineOut)")
  878. sticker.generateDefaultValues()
  879. stickers.append(sticker)
  880. }
  881. } else {
  882. // 卡点数 > 选择素材数
  883. // 第一种情况:全是图片,图片回环播放
  884. if section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials(type: "video").count == 0, section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials(type: "image").count > 0 {
  885. for (index, point) in finallyStuckPoints.enumerated() {
  886. let sticker: PQEditVisionTrackMaterialsModel = section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials()[index % section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials().count]
  887. BFLog(message: "stickerlocationPath sticker : \(sticker.locationPath)")
  888. let stickerjson = sticker.toJSONString(prettyPrint: false)
  889. let deepCopySticker = Mapper<PQEditVisionTrackMaterialsModel>().map(JSONString: stickerjson!)
  890. if deepCopySticker!.type == StickerType.IMAGE.rawValue {
  891. if index + 1 < finallyStuckPoints.count {
  892. deepCopySticker!.timelineIn = Float64("\(finallyStuckPoints[index])") ?? 0.0
  893. deepCopySticker!.timelineOut = Float64("\(finallyStuckPoints[index + 1])") ?? 0.0
  894. if deepCopySticker != nil {
  895. deepCopySticker?.generateDefaultValues()
  896. stickers.append(deepCopySticker!)
  897. }
  898. }
  899. }
  900. }
  901. } else {
  902. // 第二种情况:有视频要进行分割
  903. let clipFilters = clipVideoMerage(section: section, stuckPoints: finallyStuckPoints)
  904. for (index, point) in finallyStuckPoints.enumerated() {
  905. BFLog(message: "aaaaaindexindeindexxindexindexindex \(index) \(point)")
  906. if index + 1 < finallyStuckPoints.count, index < clipFilters.count {
  907. BFLog(message: "bbbbbindexindeindexxindexindexindex \(index) \(point)")
  908. let sticker: PQEditVisionTrackMaterialsModel = clipFilters[index]
  909. sticker.timelineIn = Float64("\(finallyStuckPoints[index])") ?? 0.0
  910. sticker.timelineOut = Float64("\(finallyStuckPoints[index + 1])") ?? 0.0
  911. // 卡点的时间 > in out 值 这里就会出现鬼畜效果
  912. let timelineInterval = sticker.timelineOut - sticker.timelineIn
  913. let inOutInterval = sticker.out - sticker.model_in
  914. if timelineInterval > inOutInterval {
  915. BFLog(message: "实际要显示卡点时长\(timelineInterval) 素材裁剪时长:\(inOutInterval)")
  916. sticker.out = sticker.model_in + timelineInterval
  917. // 下面只是 LOG 方便查问题
  918. let stickerInOut = sticker.out - sticker.model_in
  919. let stickerTimelineInOut = sticker.timelineOut - sticker.timelineIn
  920. if stickerInOut != stickerTimelineInOut {
  921. BFLog(message: "sticker.timelineIn \(sticker.timelineIn) stickerTimelineInOut is\(stickerTimelineInOut) stickerInOut is\(stickerInOut) 相差\(stickerTimelineInOut - stickerInOut)")
  922. }
  923. }
  924. // out > 素材的总时长in out 进行前移操作
  925. let offsetAssetDuration = sticker.out - sticker.duration
  926. if offsetAssetDuration > 0 {
  927. sticker.model_in = sticker.model_in - offsetAssetDuration
  928. sticker.out = sticker.out - offsetAssetDuration
  929. }
  930. BFLog(message: "index is \(index)分割后 创建 filter timelineIn :\(sticker.timelineIn) timelineOut :\(sticker.timelineOut) in :\(sticker.model_in) out:\(sticker.out) type is \(sticker.type) 显示总时长为:\(sticker.timelineOut - sticker.timelineIn) 裁剪总时长\(sticker.out - sticker.model_in)")
  931. stickers.append(sticker)
  932. }
  933. }
  934. }
  935. }
  936. }
  937. }
  938. }else if (model == .createStickersModelOnlyMusic || model == .createStickersModelSpeed){ //仅音乐 和 跳跃卡点
  939. for section in sections {
  940. if section.sectionType == "normal" {
  941. BFLog(message: "stuckPoints count is \(finallyStuckPoints.count)")
  942. // 第一种情况:全是图片,图片回环播放
  943. if section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials(type: "video").count == 0, section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials(type: "image").count > 0 {
  944. for (index, point) in finallyStuckPoints.enumerated() {
  945. let sticker: PQEditVisionTrackMaterialsModel = section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials()[index % section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials().count]
  946. BFLog(message: "stickerlocationPath sticker : \(sticker.locationPath)")
  947. let stickerjson = sticker.toJSONString(prettyPrint: false)
  948. let deepCopySticker = Mapper<PQEditVisionTrackMaterialsModel>().map(JSONString: stickerjson!)
  949. if deepCopySticker!.type == StickerType.IMAGE.rawValue {
  950. if index + 1 < finallyStuckPoints.count {
  951. deepCopySticker!.timelineIn = Float64("\(finallyStuckPoints[index])") ?? 0.0
  952. deepCopySticker!.timelineOut = Float64("\(finallyStuckPoints[index + 1])") ?? 0.0
  953. if deepCopySticker != nil {
  954. deepCopySticker?.generateDefaultValues()
  955. stickers.append(deepCopySticker!)
  956. }
  957. }
  958. }
  959. }
  960. }else{
  961. //一共裁剪的段数
  962. var totalClipNum:Int = 0
  963. for sticker in section.sectionTimeline!.visionTrack!.getEnableVisionTrackMaterials() {
  964. if sticker.type == StickerType.VIDEO.rawValue {
  965. let asset: AVURLAsset = AVURLAsset(url: URL(fileURLWithPath: documensDirectory + sticker.locationPath), options: nil)
  966. var i:Int = 0
  967. //一个视频切的的段落
  968. var oneVideoClipNum:Int = 0
  969. var realUsedMusicDuration = 0.0
  970. while realUsedMusicDuration <= CMTimeGetSeconds(asset.duration) {
  971. //快速段
  972. let LA = maxSpeed * (finallyStuckPoints[i+1] - finallyStuckPoints[i])
  973. realUsedMusicDuration = realUsedMusicDuration + Float64(LA)
  974. if(realUsedMusicDuration > CMTimeGetSeconds(asset.duration)){ break}
  975. oneVideoClipNum = oneVideoClipNum + 1
  976. //慢速段
  977. if(i + 2 >= finallyStuckPoints.count){
  978. break
  979. }
  980. let LB = minSpeed * (finallyStuckPoints[i+2] - finallyStuckPoints[i+1])
  981. realUsedMusicDuration = realUsedMusicDuration + Float64(LB)
  982. oneVideoClipNum = oneVideoClipNum + 1
  983. i = i + 1
  984. }
  985. BFLog(message: "单个视频\(sticker.locationPath)时长::\(CMTimeGetSeconds(asset.duration)) ,clipNum is:\(oneVideoClipNum)")
  986. var lastOutTime:Float64 = 0.0
  987. for clipindex in 0 ... oneVideoClipNum - 1 {
  988. // deep copy sticker model 防止只有一个对象
  989. let stickerjson = sticker.toJSONString(prettyPrint: false)
  990. let deepCopySticker = Mapper<PQEditVisionTrackMaterialsModel>().map(JSONString: stickerjson!)
  991. // 设置循环模式和适配模式
  992. deepCopySticker?.generateDefaultValues()
  993. //当前分段的速度
  994. var tempSpeed:Float = 1.0
  995. if model == .createStickersModelSpeed{
  996. tempSpeed = (totalClipNum + clipindex) % 2 == 0 ? maxSpeed : minSpeed
  997. }
  998. if(totalClipNum + 1 + clipindex < finallyStuckPoints.count){
  999. deepCopySticker?.speedRate = tempSpeed
  1000. //定义临时使用的变量
  1001. let tempModel_In = lastOutTime
  1002. let tempOut = lastOutTime + Float64(tempSpeed) * Float64(finallyStuckPoints[totalClipNum + 1 + clipindex] - finallyStuckPoints[totalClipNum + clipindex])
  1003. let tempTimelineIn = Float64(finallyStuckPoints[totalClipNum + clipindex])
  1004. let timelineOut = Float64(finallyStuckPoints[totalClipNum + 1 + clipindex])
  1005. //处理最后一点视频素材不够卡点时长 e.g. 0.3 卡点时长0.5
  1006. if(tempOut > realUsedMusicDuration){
  1007. BFLog(message: "最后一点视频素材不够卡点时长 ")
  1008. //最后一点素材时长
  1009. let lastAssetDuration = realUsedMusicDuration - tempOut
  1010. let pointDuration = timelineOut - tempTimelineIn
  1011. let needSpeed = lastAssetDuration / pointDuration
  1012. //当前卡点段为快速
  1013. if(tempSpeed >= 1){
  1014. if(needSpeed > 0.4 * Double(tempSpeed)){
  1015. deepCopySticker?.speedRate = tempSpeed
  1016. } else{
  1017. continue
  1018. }
  1019. }else{//当前卡点段为慢速
  1020. if(needSpeed > 0.4 * Double(tempSpeed) && needSpeed > 0.2){
  1021. deepCopySticker?.speedRate = tempSpeed
  1022. } else{
  1023. continue
  1024. }
  1025. }
  1026. }
  1027. deepCopySticker?.model_in = tempModel_In
  1028. deepCopySticker?.out = tempOut
  1029. deepCopySticker?.timelineIn = tempTimelineIn
  1030. deepCopySticker?.timelineOut = timelineOut
  1031. lastOutTime = deepCopySticker?.out ?? 0
  1032. }
  1033. BFLog(message: " 创建 sticker crilp is in \(String(format: "%.6f", deepCopySticker?.model_in ?? 0)) out \(String(format: "%.6f", deepCopySticker?.out ?? 0)) ,分段素材时长:\(String(format: "%.6f", (deepCopySticker?.out ?? 0) - (deepCopySticker?.model_in ?? 0))) ,分段显示时长:\(String(format: "%.6f", (deepCopySticker?.timelineOut ?? 0) - (deepCopySticker?.timelineIn ?? 0))), 总时长\(CMTimeGetSeconds(asset.duration)) timelineIN: \(String(format: "%.6f", deepCopySticker?.timelineIn ?? 0)) timelineOUT:\(String(format: "%.6f", deepCopySticker?.timelineOut ?? 0)) speedRate:\(deepCopySticker?.speedRate ?? 0.0)")
  1034. if deepCopySticker != nil {
  1035. stickers.append(deepCopySticker!)
  1036. }
  1037. }
  1038. totalClipNum = totalClipNum + oneVideoClipNum - 1
  1039. } else if sticker.type == StickerType.IMAGE.rawValue {
  1040. if(totalClipNum + 1 >= finallyStuckPoints.count){
  1041. BFLog(message: "数据出现错误!!!查正")
  1042. break
  1043. }
  1044. sticker.generateDefaultValues()
  1045. sticker.timelineIn = Float64(finallyStuckPoints[totalClipNum])
  1046. sticker.timelineOut = Float64(finallyStuckPoints[totalClipNum + 1])
  1047. totalClipNum = totalClipNum + 1
  1048. stickers.append(sticker)
  1049. }
  1050. }
  1051. }
  1052. }
  1053. }
  1054. }
  1055. return stickers
  1056. }
  1057. /// 根据档位取最后使用的卡点数据
  1058. /// - Parameter seed: 档位速度
  1059. /// - Returns: 最后使用的卡点
  1060. func getUsedStuckPoint(seed:Int) -> Array<Float> {
  1061. // 推荐卡点数
  1062. var stuckPoints: Array = Array<Float>.init()
  1063. var stuckPointsTemp = Array<Float>.init()
  1064. for (index, dunshu) in stuckPointMusicData!.rhythmSdata[0].pointTimes.enumerated() {
  1065. BFLog(message: "原所有卡点数:\(index) \(Float(dunshu) / Float(BASE_FILTER_TIMESCALE))")
  1066. if Float64(dunshu) / Float64(BASE_FILTER_TIMESCALE) > (stuckPointMusicData?.startTime ?? 0) {
  1067. stuckPointsTemp.append(Float(dunshu) / Float(BASE_FILTER_TIMESCALE))
  1068. }
  1069. }
  1070. /*
  1071. 一,快慢速模式下取卡点 1 3 5
  1072. 快节奏为选中区域的所有点位,即0,1,2,3,4 5 6 7 8 9 10 ……
  1073. 适中为每两个点位取一个,即0,3,6,9 12
  1074. 慢节奏为每三个点位取一个,即0 5 10 15
  1075. 二,跳跃卡点模式下根据不同速度 取卡点 1,2,3
  1076. 快节奏为选中区域的所有点位,即0,1,2,3,4 5 6 7 8 9 10 ……
  1077. 适中为每两个点位取一个,即0,2,4,6……
  1078. 慢节奏为每三个点位取一个,即0,3,6,9……
  1079. */
  1080. BFLog(message: "stuckPointMusicData?.speed is \(String(describing: stuckPointMusicData?.speed))")
  1081. //不丢
  1082. if(seed == 1){
  1083. stuckPoints = stuckPointsTemp
  1084. }else{
  1085. //根据档位要丢
  1086. for (index, point) in stuckPointsTemp.enumerated() {
  1087. if index % seed == 0 {
  1088. stuckPoints.append(point)
  1089. }
  1090. }
  1091. }
  1092. for point in stuckPoints {
  1093. BFLog(message: "没有 start end 计算后的卡点数\(point)")
  1094. }
  1095. //若音乐起点至第一个卡点点位之间时长t0<0.3时,此段时长与下一个点位时长合并,故第一段卡点部分时长为t0+d
  1096. if(Float(stuckPointMusicData?.startTime ?? 0) - (stuckPoints.first ?? 0.0) < 0.3){
  1097. if stuckPoints.first != nil {
  1098. stuckPoints.removeFirst()
  1099. }
  1100. }
  1101. stuckPoints.insert(Float(stuckPointMusicData?.startTime ?? 0), at: 0)
  1102. // if stuckPoints.last != nil {
  1103. // stuckPoints.removeLast()
  1104. // }
  1105. // 开始时间是服务器返回, 结果时间根据策略计算的
  1106. // stuckPoints.insert(Float(stuckPointMusicData?.startTime ?? 0), at: 0)
  1107. // stuckPoints.insert(Float(stuckPointMusicData?.endTime ?? 0), at: stuckPoints.count)
  1108. for point in stuckPoints {
  1109. BFLog(message: "有 start end 计算后的卡点数\(point)")
  1110. }
  1111. BFLog(message: "stuckPoints count is \(stuckPoints.count)")
  1112. return stuckPoints
  1113. }
  1114. /// 根据不同模式model, maxSpeed ,minSpeed, self?.stuckPointMusicData?.speed 档位,生成音乐时长和最终使用的卡点信息
  1115. func dealParameter(model:createStickersModel){
  1116. //清空上一次使用的卡点数据
  1117. finallyStuckPoints.removeAll()
  1118. //注意推荐时间位置和后面最近的卡点时间与0.3的关系
  1119. //保存丢卡点处理后的卡点信息推荐开始到最后倒数第二个
  1120. let stuckPointsTemp:Array<Float>
  1121. //最后真正要使用推荐结束时长
  1122. var realUsedMusicDuration:Float = 0.0
  1123. switch model {
  1124. case .createStickersModelPoint://跳跃卡点
  1125. stuckPointsTemp = getUsedStuckPoint(seed: stuckPointMusicData?.speed ?? 0)
  1126. // L/(n+1) L -原视觉素材总时长 n-抛留倍数
  1127. realUsedMusicDuration = Float(selectedTotalDuration) / (Float(maxSpeed) + 1)
  1128. //要拼接的段数
  1129. var clipNum:Int = 0
  1130. //所有段的时长总和
  1131. var tempTime:Float = 0.0
  1132. var i:Int = 0
  1133. while tempTime < Float(selectedTotalDuration) {
  1134. //回环从头取
  1135. if(i+2 >= stuckPointsTemp.count){
  1136. i = 0
  1137. }
  1138. //快速段
  1139. let LA = (stuckPointsTemp[i+1] - stuckPointsTemp[i])
  1140. tempTime = tempTime + Float(LA)
  1141. if(tempTime > Float(selectedTotalDuration)){ break}
  1142. //慢速段
  1143. let LB = (stuckPointsTemp[i+2] - stuckPointsTemp[i+1])
  1144. tempTime = tempTime + Float(LB)
  1145. i = i + 1
  1146. clipNum = clipNum + 1
  1147. }
  1148. //拼接要使用的卡点信息
  1149. for i in 0...clipNum {
  1150. finallyStuckPoints.append(stuckPointsTemp[i % stuckPointsTemp.count] + Float(i / stuckPointsTemp.count) * (stuckPointsTemp.first ?? 0))
  1151. }
  1152. break
  1153. case .createStickersModelSpeed://快慢速
  1154. // 快慢速 (2:快节奏,3:适中,4:慢节奏)
  1155. stuckPointsTemp = getUsedStuckPoint(seed: (stuckPointMusicData?.speed ?? 0) + 1)
  1156. /*
  1157. - A-视频中的快速片段
  1158. - B-视频中的慢速片段
  1159. - d-在一档下音乐每个点位时长
  1160. - n-不同音乐档位对应的d倍数,快节奏时,n=1;适中时,n=3;慢节奏时,n=5
  1161. - L-原视觉素材时长
  1162. - x-视频在A片段的播放倍速
  1163. - y-视频在B片段的播放倍速
  1164. */
  1165. //LA=x*n*d,LB=y*n*d (n=1/3/5) 注:视频经过快慢速处理后的总时长约=L*2/(x+y)
  1166. BFLog(message: "Ax快速为:\(maxSpeed) By慢速为:\(minSpeed) 档位 N为:\(stuckPointMusicData?.speed ?? 0) 使用的卡点总数:\(stuckPointsTemp.count)")
  1167. //1,计算 AB 段的个数
  1168. var i:Int = 0
  1169. var LACount:Int = 0
  1170. var LBCount:Int = 0
  1171. while realUsedMusicDuration < Float(selectedTotalDuration) && selectedTotalDuration != 0 {
  1172. //回环从头取
  1173. if(i+2 > stuckPointsTemp.count){
  1174. i = 0
  1175. }
  1176. //快速段
  1177. let LA = maxSpeed * (stuckPointsTemp[i+1] - stuckPointsTemp[i])
  1178. realUsedMusicDuration = realUsedMusicDuration + Float(LA)
  1179. if(realUsedMusicDuration > Float(selectedTotalDuration)){ break}
  1180. LACount = LACount + 1
  1181. BFLog(message: "快速段段段段段段后LACount:\(LACount) realUsedMusicDuration:\(realUsedMusicDuration)")
  1182. //慢速段
  1183. let LB = minSpeed * (stuckPointsTemp[i+2] - stuckPointsTemp[i+1])
  1184. realUsedMusicDuration = realUsedMusicDuration + Float(LB)
  1185. LBCount = LBCount + 1
  1186. BFLog(message: "慢速段段段段段段后LACount:\(LACount) realUsedMusicDuration:\(realUsedMusicDuration)")
  1187. i = i + 1
  1188. }
  1189. //2,拼接要使用的卡点信息
  1190. for (index,point) in stuckPointsTemp.enumerated() {
  1191. if(index < LACount + LBCount){
  1192. finallyStuckPoints.append(point)
  1193. }
  1194. }
  1195. //2.1不足所有卡点回环拼接
  1196. if( LACount + LBCount > stuckPointsTemp.count ){
  1197. for (index,point) in stuckPointsTemp.enumerated() {
  1198. if(index <= LACount + LBCount - stuckPointsTemp.count){
  1199. finallyStuckPoints.append(Float((stuckPointsTemp.last ?? 0) + point))
  1200. }
  1201. }
  1202. }
  1203. BFLog(message: "计算后的\(realUsedMusicDuration) LACount:\(LACount) LBCount \(LBCount) 估算时长为:\(selectedTotalDuration * 2 / Float64(maxSpeed + minSpeed))")
  1204. break
  1205. case .createStickersModelOnlyMusic://仅音乐
  1206. stuckPointsTemp = getUsedStuckPoint(seed: 1)
  1207. //要拼接的段数
  1208. var clipNum:Int = 0
  1209. //所有段的时长总和
  1210. var tempTime:Float = 0.0
  1211. var i:Int = 0
  1212. while tempTime < Float(selectedTotalDuration) {
  1213. //回环从头取
  1214. if(i+2 >= stuckPointsTemp.count){
  1215. i = 0
  1216. }
  1217. //快速段
  1218. let LA = (stuckPointsTemp[i+1] - stuckPointsTemp[i])
  1219. tempTime = tempTime + Float(LA)
  1220. clipNum = clipNum + 1
  1221. if(tempTime > Float(selectedTotalDuration)){ break}
  1222. //慢速段
  1223. let LB = (stuckPointsTemp[i+2] - stuckPointsTemp[i+1])
  1224. tempTime = tempTime + Float(LB)
  1225. i = i + 1
  1226. clipNum = clipNum + 1
  1227. }
  1228. //拼接要使用的卡点信息
  1229. for i in 0...clipNum {
  1230. let a = stuckPointsTemp[i % stuckPointsTemp.count]
  1231. let b = Float(i / stuckPointsTemp.count) * (stuckPointsTemp.first ?? 0)
  1232. finallyStuckPoints.append(a + b)
  1233. }
  1234. break
  1235. }
  1236. //拼接图片所使用的时长
  1237. //二,最一个卡点在原数组中的位置,从这个位置开始拼接图片所要使用的卡点
  1238. if(selectedImageDataCount > 0){
  1239. var pointIndex = stuckPointsTemp.firstIndex(where: { (point) -> Bool in
  1240. (point == (finallyStuckPoints.last ?? 0).truncatingRemainder(dividingBy: (stuckPointsTemp.last ?? 0)))
  1241. })
  1242. pointIndex = (pointIndex ?? 0) + 1
  1243. for i in 0...selectedImageDataCount {
  1244. let a = stuckPointsTemp[i % stuckPointsTemp.count]
  1245. let b = Float(i / stuckPointsTemp.count) * (stuckPointsTemp.first ?? 0)
  1246. finallyStuckPoints.append(a + b)
  1247. }
  1248. selectedTotalDuration = Float64(((finallyStuckPoints.last ?? 0) - (finallyStuckPoints.first ?? 0)))
  1249. }
  1250. //3)素材全是图片时如果时长 < 10s要做轮播处理
  1251. if(selectedDataCount == selectedImageDataCount && selectedTotalDuration < 10 ){
  1252. //已经设置过循环次数
  1253. if(lastCyclesSelectIndex != 0){
  1254. for i in 0...lastCyclesSelectIndex {
  1255. var pointIndex = stuckPointsTemp.firstIndex(where: { (point) -> Bool in
  1256. (point == (finallyStuckPoints.last ?? 0).truncatingRemainder(dividingBy: (stuckPointsTemp.last ?? 0)))
  1257. })
  1258. pointIndex = (pointIndex ?? 0) + 1
  1259. for i in 0...selectedImageDataCount {
  1260. let a = stuckPointsTemp[i % stuckPointsTemp.count]
  1261. let b = Float(i / stuckPointsTemp.count) * (stuckPointsTemp.first ?? 0)
  1262. finallyStuckPoints.append(a + b)
  1263. }
  1264. }
  1265. }else{
  1266. lastCyclesSelectIndex = 0
  1267. while selectedTotalDuration < 10{
  1268. //不够10S 时 一次加图片数量的卡点数
  1269. var pointIndex = stuckPointsTemp.firstIndex(where: { (point) -> Bool in
  1270. (point == (finallyStuckPoints.last ?? 0).truncatingRemainder(dividingBy: (stuckPointsTemp.last ?? 0)))
  1271. })
  1272. pointIndex = (pointIndex ?? 0) + 1
  1273. for i in 0...selectedImageDataCount {
  1274. let a = stuckPointsTemp[(i + Int(pointIndex ?? 0)) % stuckPointsTemp.count]
  1275. let b = Float((i + Int(pointIndex ?? 0)) / stuckPointsTemp.count) * (stuckPointsTemp.first ?? 0)
  1276. finallyStuckPoints.append(a + b)
  1277. }
  1278. selectedTotalDuration = Float64((finallyStuckPoints.last ?? 0) - (finallyStuckPoints.first ?? 0))
  1279. lastCyclesSelectIndex = lastCyclesSelectIndex + 1
  1280. }
  1281. speedSettingView.setSelectItem(index: lastCyclesSelectIndex)
  1282. }
  1283. }
  1284. //三,计算最后使用的音频时长, 如果不用拼接音频时长度是卡点的倒数第二位时间
  1285. let asset = AVURLAsset(url: URL(fileURLWithPath: documensDirectory + (stuckPointMusicData?.localPath ?? "")), options: nil)
  1286. //原推荐卡点的倒数第二位时间
  1287. let lastSecondPoint = Float((stuckPointMusicData!.rhythmSdata[0].pointTimes[stuckPointMusicData!.rhythmSdata[0].pointTimes.count - 2] )) / Float(BASE_FILTER_TIMESCALE)
  1288. finallyUserAudioTime = Float(lastSecondPoint)
  1289. if((finallyStuckPoints.last ?? 0) > Float(CMTimeGetSeconds(asset.duration))){
  1290. finallyUserAudioTime = Float(finallyStuckPoints.last ?? 0) + (Float(CMTimeGetSeconds(asset.duration)) - Float(lastSecondPoint))
  1291. }
  1292. playeTimeRange = CMTimeRange(start: CMTime(value: CMTimeValue( Float(stuckPointMusicData?.startTime ?? 0) * Float(BASE_FILTER_TIMESCALE)), timescale: BASE_FILTER_TIMESCALE), end: CMTime(value: CMTimeValue((Float(finallyStuckPoints.last ?? 0)) * Float(BASE_FILTER_TIMESCALE)), timescale: BASE_FILTER_TIMESCALE))
  1293. // 更新裁剪时间条的的ui数据
  1294. stuckPointCuttingView.videoDuration = CGFloat(finallyUserAudioTime)
  1295. stuckPointCuttingView.updateEndTime(startTime:CGFloat(CMTimeGetSeconds( playeTimeRange.start)), endTime: CGFloat( CMTimeGetSeconds( playeTimeRange.end)))
  1296. }
  1297. }
  1298. // MARK: - 同步/下载素材相关
  1299. /// 同步/下载素材相关
  1300. extension PQStuckPointEditerController {
  1301. /// 同步音乐相关数据
  1302. /// - Returns: <#description#>
  1303. func synchroMusicInfoData() {
  1304. if (stuckPointMusicData?.rhythmSdata.count ?? 0) <= 0 {
  1305. if synchroMarskView.superview == nil {
  1306. UIApplication.shared.keyWindow?.addSubview(synchroMarskView)
  1307. }
  1308. PQStuckPointViewModel.stuckPointMusicDetailData(musicId: stuckPointMusicData?.musicId ?? "", originType: stuckPointMusicData?.originType ?? 1) { [weak self] newMusicData, _ in
  1309. if newMusicData != nil, (newMusicData?.rhythmSdata.count ?? 0) > 0 {
  1310. self?.isStuckPointDataSuccess = true
  1311. self?.stuckPointMusicData?.rhythmSdata = newMusicData?.rhythmSdata ?? []
  1312. self?.stuckPointMusicData?.startTime = newMusicData?.startTime ?? 0
  1313. self?.stuckPointMusicData?.endTime = newMusicData?.endTime ?? 0
  1314. if newMusicData?.speed != nil {
  1315. self?.stuckPointMusicData?.speed = newMusicData?.speed ?? 2
  1316. }
  1317. if (self?.stuckPointMusicData?.rhythmSdata.count ?? 0) > 0 && (((self?.selectedDataCount ?? 0) - (self?.selectedImageDataCount ?? 0)) > 0 || (self?.selectedImageDataCount ?? 0) > 0 || (self?.selectedTotalDuration ?? 0) > 0) {
  1318. // self?.stuckPointMusicData?.endTime = (self?.stuckPointMusicData?.startTime ?? 0) + (self?.stuckPointMusicData?.stuckPointCuttingTime(videoCount: (self?.selectedDataCount ?? 0) - (self?.selectedImageDataCount ?? 0), imageCount: self?.selectedImageDataCount ?? 0, totalDuration: self?.selectedTotalDuration ?? 0) ?? 0)
  1319. }
  1320. self?.stuckPointCuttingView.updateEndTime(startTime: CGFloat(self?.stuckPointMusicData?.startTime ?? 0), endTime: CGFloat(self?.stuckPointMusicData?.endTime ?? 0))
  1321. if self?.stuckPointMusicData?.localPath == nil || (self?.stuckPointMusicData?.localPath?.count ?? 0) > 0 {
  1322. PQDownloadManager.downLoadFile(url: self?.stuckPointMusicData?.musicPath ?? "") { [weak self] filePath, error in
  1323. if error == nil, filePath != nil {
  1324. self?.isSynchroMusicInfoSuccess = true
  1325. self?.stuckPointMusicData?.localPath = filePath?.replacingOccurrences(of: documensDirectory, with: "")
  1326. // 处理所有数据完成
  1327. self?.dealWithDataSuccess()
  1328. }
  1329. if self?.synchroMarskView.superview != nil {
  1330. self?.synchroMarskView.removeMarskView()
  1331. }
  1332. }
  1333. } else {
  1334. self?.isSynchroMusicInfoSuccess = true
  1335. // 处理所有数据完成
  1336. self?.dealWithDataSuccess()
  1337. }
  1338. // 添加子视图
  1339. // if(playerView )
  1340. self?.addSubViews()
  1341. } else {
  1342. if self?.synchroMarskView.superview != nil {
  1343. self?.synchroMarskView.removeMarskView()
  1344. }
  1345. }
  1346. }
  1347. } else if stuckPointMusicData?.localPath == nil || (stuckPointMusicData?.localPath?.count ?? 0) > 0 {
  1348. if synchroMarskView.superview == nil {
  1349. UIApplication.shared.keyWindow?.addSubview(synchroMarskView)
  1350. }
  1351. isStuckPointDataSuccess = true
  1352. PQDownloadManager.downLoadFile(url: stuckPointMusicData?.musicPath ?? "") { [weak self] filePath, error in
  1353. if error == nil, filePath != nil {
  1354. self?.isSynchroMusicInfoSuccess = true
  1355. self?.stuckPointMusicData?.localPath = filePath?.replacingOccurrences(of: documensDirectory, with: "")
  1356. // 处理所有数据完成
  1357. self?.dealWithDataSuccess()
  1358. } else {
  1359. if self?.synchroMarskView.superview != nil {
  1360. self?.synchroMarskView.removeMarskView()
  1361. }
  1362. // PQUploadRemindView.showUploadRemindView(title: nil, attributedTitle: NSAttributedString(string: "加载音乐失败,请重新选择音乐"), summary: "", confirmTitle: nil) { [weak self] _, _ in
  1363. // self?.navigationController?.popViewController(animated: true)
  1364. // }
  1365. }
  1366. }
  1367. } else {
  1368. isStuckPointDataSuccess = true
  1369. // 处理所有数据完成
  1370. dealWithDataSuccess()
  1371. }
  1372. }
  1373. /// 导出相册数据
  1374. /// - Returns: <#description#>
  1375. func exportPhotoData() {
  1376. // 取消所有的导出
  1377. PQSingletoMemoryUtil.shared.allExportSession.forEach { _, exportSession in
  1378. exportSession.cancelExport()
  1379. }
  1380. var isHaveVideo: Bool = false
  1381. if selectedPhotoData != nil, (selectedPhotoData?.count ?? 0) > 0 {
  1382. if synchroMarskView.superview == nil {
  1383. UIApplication.shared.keyWindow?.addSubview(synchroMarskView)
  1384. }
  1385. let dispatchGroup = DispatchGroup()
  1386. for photo in selectedPhotoData! {
  1387. if photo.asset != nil, photo.asset?.mediaType == .video {
  1388. if !isHaveVideo {
  1389. isHaveVideo = true
  1390. }
  1391. dispatchGroup.enter()
  1392. PQPHAssetVideoParaseUtil.parasToAVAsset(phAsset: photo.asset!) { avAsset, _, _, _ in
  1393. if avAsset is AVURLAsset {
  1394. // 创建目录
  1395. let fileName = (avAsset as! AVURLAsset).url.absoluteString
  1396. BFLog(message: "video fileName is\(fileName)")
  1397. let tempPhoto = self.selectedPhotoData?.first(where: { material in
  1398. material.asset == photo.asset
  1399. })
  1400. if (fileName.count) > 0 {
  1401. createDirectory(path: photoLibraryDirectory)
  1402. let outFilePath = photoLibraryDirectory + fileName.md5 + ".mp4"
  1403. // 文件存在先删除老文件
  1404. if FileManager.default.fileExists(atPath: outFilePath) {
  1405. do {
  1406. try FileManager.default.removeItem(at: NSURL.fileURL(withPath: outFilePath))
  1407. } catch {
  1408. BFLog(message: "导出相册视频-error == \(error)")
  1409. }
  1410. }
  1411. do {
  1412. try FileManager.default.copyItem(atPath: fileName.replacingOccurrences(of: "file:///", with: ""), toPath: outFilePath)
  1413. print("Success to copy file.")
  1414. } catch {
  1415. print("Failed to copy file.")
  1416. }
  1417. tempPhoto?.locationPath = outFilePath.replacingOccurrences(of: documensDirectory, with: "")
  1418. BFLog(message: "导出视频相册地址为 \(String(describing: tempPhoto?.locationPath))")
  1419. dispatchGroup.leave()
  1420. }
  1421. }
  1422. }
  1423. }
  1424. }
  1425. dispatchGroup.notify(queue: DispatchQueue.main) { [weak self] in
  1426. self?.isExportVideosSuccess = true
  1427. BFLog(message: "所有相册视频导出成功")
  1428. // 处理所有数据完成
  1429. if isHaveVideo {
  1430. self?.dealWithDataSuccess()
  1431. }
  1432. }
  1433. // 只有图片
  1434. if !isHaveVideo {
  1435. isExportVideosSuccess = true
  1436. BFLog(message: "所有相册视频导出成功")
  1437. dealWithDataSuccess()
  1438. }
  1439. }
  1440. }
  1441. /// 处理所有数据完成
  1442. /// - Returns: <#description#>
  1443. func dealWithDataSuccess() {
  1444. BFLog(message: "三组参数:\(isSynchroMusicInfoSuccess) \(isStuckPointDataSuccess) \(isExportVideosSuccess)")
  1445. if !isSynchroMusicInfoSuccess || !isStuckPointDataSuccess || !isExportVideosSuccess {
  1446. return
  1447. }
  1448. createPorjectData()
  1449. BFLog(message: "界面编辑界面时参数 选择素材时长:\(selectedTotalDuration) 选择素材总数:\(selectedDataCount) 选择图片总数\(selectedImageDataCount)")
  1450. //1 生成默认参数值
  1451. /*
  1452. - 当素材总时长∈[0-6)s 时,提示推荐仅配乐模式 or 快慢速模式
  1453. - 当素材总时长∈[6-80)s 时,卡点抛留倍数为1x,即留1抛1
  1454. - 当素材总时长∈[80,120)s 时,卡点抛留倍数为2x,即留1抛2
  1455. - 当素材总时长∈[120,160)s 时,卡点抛留倍数为3x,即留1抛3
  1456. - 当素材总时长∈[160,200)s 时,卡点抛留倍数为4x,即留1抛4
  1457. - 当素材总时长∈[200,240)s 时,卡点抛留倍数为5x,即留1抛5
  1458. - 当素材总时长∈[240,∞)s 时,卡点抛留倍数为5x,即留1抛5
  1459. */
  1460. //1.1) 生成跳跃卡点的默认值
  1461. if(selectedTotalDuration >= 6 && selectedTotalDuration < 80){
  1462. lastJumpSpeedSelectIndex = 0
  1463. }else if(selectedTotalDuration >= 80 && selectedTotalDuration < 120){
  1464. lastJumpSpeedSelectIndex = 1
  1465. }else if(selectedTotalDuration >= 120 && selectedTotalDuration < 160){
  1466. lastJumpSpeedSelectIndex = 2
  1467. }else if(selectedTotalDuration >= 160 && selectedTotalDuration < 200){
  1468. lastJumpSpeedSelectIndex = 3
  1469. }else if(selectedTotalDuration >= 200 && selectedTotalDuration < 240){
  1470. lastJumpSpeedSelectIndex = 4
  1471. }else if(selectedTotalDuration >= 240){
  1472. lastJumpSpeedSelectIndex = 5
  1473. }
  1474. /*默认进入快慢速模式
  1475. - 当素材总时长∈[120,144]s 时,快慢速处理方式:快速为 6x,慢速为 1.2x,效果是快&快
  1476. - 当素材总时长∈[70,120)s 时,快慢速处理方式:快速为5x,慢速为 1x,效果是快&正常
  1477. - 当素材总时长∈[56,70)s 时,快慢速处理方式:快速为3x,慢速为 0.5x,效果是快&慢
  1478. - 当素材总时长∈[17.5,56)s 时,快慢速处理方式:快速为 2.4x,慢速为 0.4x,效果是快&慢
  1479. - 当素材总时长∈[10.5,17.5)s 时,快慢速处理方式:快速为 1.8x,慢速为 0.3x,效果是快&慢
  1480. - 当素材总时长∈(0,10.5)s 时,快慢速处理方式:快速为 1x,慢速为 0.2x,效果是正常&慢
  1481. */
  1482. //1.2)生成快慢速的默认值
  1483. if(selectedTotalDuration >= 120 && selectedTotalDuration <= 144){
  1484. lastSpeedSelectIndex = 0
  1485. }else if(selectedTotalDuration >= 70 && selectedTotalDuration < 120){
  1486. lastSpeedSelectIndex = 1
  1487. }else if(selectedTotalDuration >= 56 && selectedTotalDuration < 70){
  1488. lastSpeedSelectIndex = 2
  1489. }else if(selectedTotalDuration >= 17.5 && selectedTotalDuration < 56){
  1490. lastSpeedSelectIndex = 3
  1491. }else if(selectedTotalDuration >= 10.5 && selectedTotalDuration < 17.5){
  1492. lastSpeedSelectIndex = 4
  1493. }else if(selectedTotalDuration > 0 && selectedTotalDuration < 10.5){
  1494. lastSpeedSelectIndex = 5
  1495. }
  1496. /*
  1497. 文档规则 https://w42nne6hzg.feishu.cn/docs/doccnQZm1uCfkU4QtJb5fLxYk4d#
  1498. */
  1499. //2,根据所选择所有素材时长进入默认模式
  1500. //全是图片
  1501. if(selectedDataCount == selectedImageDataCount){
  1502. BFLog(message: "全是图片 \(selectedDataCount) \(selectedImageDataCount)")
  1503. //默认进入跳跃卡点模式
  1504. editModelClick(sender: jumpPointBtn)
  1505. // speedSettingView.setSelectItem(index: lastJumpSpeedSelectIndex)
  1506. }else{
  1507. //默认进入快慢速模式
  1508. if(selectedTotalDuration > 0 && selectedTotalDuration <= 144){
  1509. editModelClick(sender: speedStuckBtn)
  1510. // speedSettingView.setSelectItem(index: lastSpeedSelectIndex)
  1511. }else{
  1512. //默认进入卡点模式
  1513. editModelClick(sender: jumpPointBtn)
  1514. // speedSettingView.setSelectItem(index: lastJumpSpeedSelectIndex)
  1515. }
  1516. }
  1517. }
  1518. }