PQStuckPointEditerController.swift 94 KB

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