PQStuckPointEditerController.swift 93 KB

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