PQStuckPointEditerController.swift 91 KB

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